Implement module sort by position (no sort)

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
This commit is contained in:
Khosrow Moossavi
2021-03-15 16:51:15 -04:00
parent 2109520db0
commit ac5a29c6f6
46 changed files with 280 additions and 175 deletions

View File

@@ -58,17 +58,17 @@ data "aws_caller_identity" "ident" {
resource "null_resource" "foo" {}
module "foo" {
source = "bar"
version = "1.2.3"
}
module "bar" {
source = "baz"
version = "4.5.6"
}
module "foo" {
source = "bar"
version = "1.2.3"
}
module "baz" {
source = "baz"
version = "4.5.6"
}
}

2
go.mod
View File

@@ -11,7 +11,7 @@ require (
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/terraform-docs/plugin-sdk v0.1.1-0.20210315181351-354f95b748c2
github.com/terraform-docs/plugin-sdk v0.1.1-0.20210315210824-bb03f50252e3
github.com/terraform-docs/terraform-config-inspect v0.0.0-20210126151735-6ef25af8884f
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
honnef.co/go/tools v0.1.2

4
go.sum
View File

@@ -221,8 +221,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/terraform-docs/plugin-sdk v0.1.1-0.20210315181351-354f95b748c2 h1:sRiFTk/RE2vdLu4/Qm7NN0wNvIuj7vFblBfYDOuQ7qg=
github.com/terraform-docs/plugin-sdk v0.1.1-0.20210315181351-354f95b748c2/go.mod h1:3G+0nZTeaMF1c5CZh8cOEYeNq0kUL6+DlQOVcxK7eCQ=
github.com/terraform-docs/plugin-sdk v0.1.1-0.20210315210824-bb03f50252e3 h1:3X3lgb0DSmmJrsMIvIkkF/PTueQ+qkBz31Y6ARIx2AY=
github.com/terraform-docs/plugin-sdk v0.1.1-0.20210315210824-bb03f50252e3/go.mod h1:3G+0nZTeaMF1c5CZh8cOEYeNq0kUL6+DlQOVcxK7eCQ=
github.com/terraform-docs/terraform-config-inspect v0.0.0-20210126151735-6ef25af8884f h1:WXgHENMC8JOyj6aRpOlnAnJkZ3ZAkPBOhqYrJ5GOhDE=
github.com/terraform-docs/terraform-config-inspect v0.0.0-20210126151735-6ef25af8884f/go.mod h1:GtanFwTsRRXScYHOMb5h4K18XQBFeS2tXat9/LrPtPc=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=

View File

@@ -62,18 +62,18 @@ The following providers are used by this module:
The following Modules are called:
=== foo
Source: bar
Version: 1.2.3
=== bar
Source: baz
Version: 4.5.6
=== foo
Source: bar
Version: 1.2.3
=== baz
Source: baz

View File

@@ -62,18 +62,18 @@ The following providers are used by this module:
The following Modules are called:
===== foo
Source: bar
Version: 1.2.3
===== bar
Source: baz
Version: 4.5.6
===== foo
Source: bar
Version: 1.2.3
===== baz
Source: baz

View File

@@ -2,18 +2,18 @@
The following Modules are called:
=== foo
Source: bar
Version: 1.2.3
=== bar
Source: baz
Version: 4.5.6
=== foo
Source: bar
Version: 1.2.3
=== baz
Source: baz

View File

@@ -62,18 +62,18 @@ The following providers are used by this module:
The following Modules are called:
=== [[module_foo]] <<module_foo,foo>>
Source: bar
Version: 1.2.3
=== [[module_bar]] <<module_bar,bar>>
Source: baz
Version: 4.5.6
=== [[module_foo]] <<module_foo,foo>>
Source: bar
Version: 1.2.3
=== [[module_baz]] <<module_baz,baz>>
Source: baz

View File

@@ -62,18 +62,18 @@ The following providers are used by this module:
The following Modules are called:
=== foo
Source: bar
Version: 1.2.3
=== bar
Source: baz
Version: 4.5.6
=== foo
Source: bar
Version: 1.2.3
=== baz
Source: baz

View File

@@ -62,8 +62,8 @@ followed by another line of text.
[cols="a,a,a",options="header,autowidth"]
|===
|Name|Source|Version|
|foo|bar|1.2.3
|bar|baz|4.5.6
|foo|bar|1.2.3
|baz|baz|4.5.6
|===

View File

@@ -62,8 +62,8 @@ followed by another line of text.
[cols="a,a,a",options="header,autowidth"]
|===
|Name|Source|Version|
|foo|bar|1.2.3
|bar|baz|4.5.6
|foo|bar|1.2.3
|baz|baz|4.5.6
|===

View File

@@ -3,7 +3,7 @@
[cols="a,a,a",options="header,autowidth"]
|===
|Name|Source|Version|
|foo|bar|1.2.3
|bar|baz|4.5.6
|foo|bar|1.2.3
|baz|baz|4.5.6
|===

View File

@@ -62,8 +62,8 @@ followed by another line of text.
[cols="a,a,a",options="header,autowidth"]
|===
|Name|Source|Version|
|[[module_foo]] <<module_foo,foo>>|bar|1.2.3
|[[module_bar]] <<module_bar,bar>>|baz|4.5.6
|[[module_foo]] <<module_foo,foo>>|bar|1.2.3
|[[module_baz]] <<module_baz,baz>>|baz|4.5.6
|===

View File

@@ -62,8 +62,8 @@ followed by another line of text.
[cols="a,a,a",options="header,autowidth"]
|===
|Name|Source|Version|
|foo|bar|1.2.3
|bar|baz|4.5.6
|foo|bar|1.2.3
|baz|baz|4.5.6
|===

View File

@@ -33,8 +33,8 @@
"object_default_empty"
],
"modules": [
"foo-bar",
"bar-baz",
"foo-bar",
"baz-baz"
],
"outputs": [

View File

@@ -246,16 +246,16 @@
}
],
"modules": [
{
"name": "foo",
"source": "bar",
"version": "1.2.3"
},
{
"name": "bar",
"source": "baz",
"version": "4.5.6"
},
{
"name": "foo",
"source": "bar",
"version": "1.2.3"
},
{
"name": "baz",
"source": "baz",
@@ -350,4 +350,4 @@
"version": "latest"
}
]
}
}

View File

@@ -246,16 +246,16 @@
}
],
"modules": [
{
"name": "foo",
"source": "bar",
"version": "1.2.3"
},
{
"name": "bar",
"source": "baz",
"version": "4.5.6"
},
{
"name": "foo",
"source": "bar",
"version": "1.2.3"
},
{
"name": "baz",
"source": "baz",
@@ -350,4 +350,4 @@
"version": "latest"
}
]
}
}

View File

@@ -3,16 +3,16 @@
"footer": "",
"inputs": [],
"modules": [
{
"name": "foo",
"source": "bar",
"version": "1.2.3"
},
{
"name": "bar",
"source": "baz",
"version": "4.5.6"
},
{
"name": "foo",
"source": "bar",
"version": "1.2.3"
},
{
"name": "baz",
"source": "baz",

View File

@@ -40,4 +40,4 @@
"version": "latest"
}
]
}
}

View File

@@ -62,18 +62,18 @@ The following providers are used by this module:
The following Modules are called:
### foo
Source: bar
Version: 1.2.3
### bar
Source: baz
Version: 4.5.6
### foo
Source: bar
Version: 1.2.3
### baz
Source: baz

View File

@@ -62,18 +62,18 @@ The following providers are used by this module:
The following Modules are called:
### foo
Source: bar
Version: 1.2.3
### bar
Source: baz
Version: 4.5.6
### foo
Source: bar
Version: 1.2.3
### baz
Source: baz

View File

@@ -62,18 +62,18 @@ The following providers are used by this module:
The following Modules are called:
##### foo
Source: bar
Version: 1.2.3
##### bar
Source: baz
Version: 4.5.6
##### foo
Source: bar
Version: 1.2.3
##### baz
Source: baz

View File

@@ -2,18 +2,18 @@
The following Modules are called:
### foo
Source: bar
Version: 1.2.3
### bar
Source: baz
Version: 4.5.6
### foo
Source: bar
Version: 1.2.3
### baz
Source: baz

View File

@@ -62,18 +62,18 @@ The following providers are used by this module:
The following Modules are called:
### <a name="module_foo"></a> [foo](#module_foo)
Source: bar
Version: 1.2.3
### <a name="module_bar"></a> [bar](#module_bar)
Source: baz
Version: 4.5.6
### <a name="module_foo"></a> [foo](#module_foo)
Source: bar
Version: 1.2.3
### <a name="module_baz"></a> [baz](#module_baz)
Source: baz

View File

@@ -62,18 +62,18 @@ The following providers are used by this module:
The following Modules are called:
### foo
Source: bar
Version: 1.2.3
### bar
Source: baz
Version: 4.5.6
### foo
Source: bar
Version: 1.2.3
### baz
Source: baz

View File

@@ -57,8 +57,8 @@ followed by another line of text.
| Name | Source | Version |
|------|--------|---------|
| foo | bar | 1.2.3 |
| bar | baz | 4.5.6 |
| foo | bar | 1.2.3 |
| baz | baz | 4.5.6 |
## Resources

View File

@@ -57,8 +57,8 @@ followed by another line of text.
| Name | Source | Version |
|------|--------|---------|
| foo | bar | 1.2.3 |
| bar | baz | 4.5.6 |
| foo | bar | 1.2.3 |
| baz | baz | 4.5.6 |
## Resources

View File

@@ -57,8 +57,8 @@ followed by another line of text.
| Name | Source | Version |
|------|--------|---------|
| foo | bar | 1.2.3 |
| bar | baz | 4.5.6 |
| foo | bar | 1.2.3 |
| baz | baz | 4.5.6 |
#### Resources

View File

@@ -2,6 +2,6 @@
| Name | Source | Version |
|------|--------|---------|
| foo | bar | 1.2.3 |
| bar | baz | 4.5.6 |
| foo | bar | 1.2.3 |
| baz | baz | 4.5.6 |

View File

@@ -57,8 +57,8 @@ followed by another line of text.
| Name | Source | Version |
|------|--------|---------|
| <a name="module_foo"></a> [foo](#module_foo) | bar | 1.2.3 |
| <a name="module_bar"></a> [bar](#module_bar) | baz | 4.5.6 |
| <a name="module_foo"></a> [foo](#module_foo) | bar | 1.2.3 |
| <a name="module_baz"></a> [baz](#module_baz) | baz | 4.5.6 |
## Resources

View File

@@ -57,8 +57,8 @@ followed by another line of text.
| Name | Source | Version |
|------|--------|---------|
| foo | bar | 1.2.3 |
| bar | baz | 4.5.6 |
| foo | bar | 1.2.3 |
| baz | baz | 4.5.6 |
## Resources

View File

@@ -48,8 +48,8 @@ provider.aws.ident (>= 2.15.0)
provider.null
modulecall.foo (bar,1.2.3)
modulecall.bar (baz,4.5.6)
modulecall.foo (bar,1.2.3)
modulecall.baz (baz,4.5.6)
@@ -200,4 +200,4 @@ terraform 0.12 only
## This is an example of a footer
It looks exactly like a header, but is placed at the end of the document
It looks exactly like a header, but is placed at the end of the document

View File

@@ -1,3 +1,3 @@
modulecall.foo (bar,1.2.3)
modulecall.bar (baz,4.5.6)
modulecall.baz (baz,4.5.6)
modulecall.foo (bar,1.2.3)
modulecall.baz (baz,4.5.6)

View File

@@ -48,8 +48,8 @@ followed by another line of text.
provider.null
modulecall.foo (bar,1.2.3)
modulecall.bar (baz,4.5.6)
modulecall.foo (bar,1.2.3)
modulecall.baz (baz,4.5.6)
@@ -200,4 +200,4 @@ It spans over multiple lines.
## This is an example of a footer
It looks exactly like a header, but is placed at the end of the document
It looks exactly like a header, but is placed at the end of the document

View File

@@ -230,16 +230,16 @@ footer = "## This is an example of a footer\n\nIt looks exactly like a header, b
required = false
[inputs.default]
[[modules]]
Name = "foo"
Source = "bar"
Version = "1.2.3"
[[modules]]
Name = "bar"
Source = "baz"
Version = "4.5.6"
[[modules]]
Name = "foo"
Source = "bar"
Version = "1.2.3"
[[modules]]
Name = "baz"
Source = "baz"
@@ -323,4 +323,4 @@ footer = "## This is an example of a footer\n\nIt looks exactly like a header, b
providerName = "aws"
providerSource = "hashicorp/aws"
mode = "data"
version = "latest"
version = "latest"

View File

@@ -6,17 +6,17 @@ providers = []
requirements = []
resources = []
[[modules]]
Name = "foo"
Source = "bar"
Version = "1.2.3"
[[modules]]
Name = "bar"
Source = "baz"
Version = "4.5.6"
[[modules]]
Name = "foo"
Source = "bar"
Version = "1.2.3"
[[modules]]
Name = "baz"
Source = "baz"
Version = "4.5.6"
Version = "4.5.6"

View File

@@ -16,4 +16,4 @@ resources = []
[[requirements]]
Name = "random"
Version = ">= 2.2.0"
Version = ">= 2.2.0"

View File

@@ -36,4 +36,4 @@ requirements = []
providerName = "aws"
providerSource = "hashicorp/aws"
mode = "data"
version = "latest"
version = "latest"

View File

@@ -246,16 +246,16 @@
</input>
</inputs>
<modules>
<module>
<Name>foo</Name>
<Source>bar</Source>
<Version>1.2.3</Version>
</module>
<module>
<Name>bar</Name>
<Source>baz</Source>
<Version>4.5.6</Version>
</module>
<module>
<Name>foo</Name>
<Source>bar</Source>
<Version>1.2.3</Version>
</module>
<module>
<Name>baz</Name>
<Source>baz</Source>
@@ -350,4 +350,4 @@
<version>latest</version>
</resource>
</resources>
</module>
</module>

View File

@@ -3,16 +3,16 @@
<footer></footer>
<inputs></inputs>
<modules>
<module>
<Name>foo</Name>
<Source>bar</Source>
<Version>1.2.3</Version>
</module>
<module>
<Name>bar</Name>
<Source>baz</Source>
<Version>4.5.6</Version>
</module>
<module>
<Name>foo</Name>
<Source>bar</Source>
<Version>1.2.3</Version>
</module>
<module>
<Name>baz</Name>
<Source>baz</Source>
@@ -23,4 +23,4 @@
<providers></providers>
<requirements></requirements>
<resources></resources>
</module>
</module>

View File

@@ -40,4 +40,4 @@
<version>latest</version>
</resource>
</resources>
</module>
</module>

View File

@@ -225,12 +225,12 @@ inputs:
default: {}
required: false
modules:
- name: foo
source: bar
version: 1.2.3
- name: bar
source: baz
version: 4.5.6
- name: foo
source: bar
version: 1.2.3
- name: baz
source: baz
version: 4.5.6
@@ -287,4 +287,4 @@ resources:
providerName: aws
providerSource: hashicorp/aws
mode: data
version: latest
version: latest

View File

@@ -2,12 +2,12 @@ header: ""
footer: ""
inputs: []
modules:
- name: foo
source: bar
version: 1.2.3
- name: bar
source: baz
version: 4.5.6
- name: foo
source: bar
version: 1.2.3
- name: baz
source: baz
version: 4.5.6

View File

@@ -29,4 +29,4 @@ resources:
providerName: aws
providerSource: hashicorp/aws
mode: data
version: latest
version: latest

View File

@@ -292,15 +292,19 @@ func loadInputs(tfmodule *tfconfig.Module) ([]*Input, []*Input, []*Input) {
}
func loadModulecalls(tfmodule *tfconfig.Module) []*ModuleCall {
var modulecalls = make([]*ModuleCall, 0)
for _, modulecall := range tfmodule.ModuleCalls {
modulecalls = append(modulecalls, &ModuleCall{
Name: modulecall.Name,
Source: modulecall.Source,
Version: modulecall.Version,
var modules = make([]*ModuleCall, 0)
for _, m := range tfmodule.ModuleCalls {
modules = append(modules, &ModuleCall{
Name: m.Name,
Source: m.Source,
Version: m.Version,
Position: Position{
Filename: m.Pos.Filename,
Line: m.Pos.Line,
},
})
}
return modulecalls
return modules
}
func loadOutputs(tfmodule *tfconfig.Module, options *Options) ([]*Output, error) {
@@ -534,7 +538,9 @@ func sortItems(tfmodule *Module, sortby *SortBy) {
// modules
if sortby.Name || sortby.Required {
sort.Sort(modulecallsSortedByName(tfmodule.ModuleCalls))
} else {
} else if sortby.Type {
sort.Sort(modulecallsSortedBySource(tfmodule.ModuleCalls))
} else {
sort.Sort(modulecallsSortedByPosition(tfmodule.ModuleCalls))
}
}

View File

@@ -18,9 +18,10 @@ import (
// ModuleCall represents a submodule called by Terraform module.
type ModuleCall struct {
Name string `json:"name"`
Source string `json:"source"`
Version string `json:"version,omitempty"`
Name string `json:"name"`
Source string `json:"source"`
Version string `json:"version,omitempty"`
Position Position `json:"-" toml:"-" xml:"-" yaml:"-"`
}
// FullName returns full name of the modulecall, with version if available
@@ -33,11 +34,9 @@ func (mc *ModuleCall) FullName() string {
type modulecallsSortedByName []*ModuleCall
func (a modulecallsSortedByName) Len() int { return len(a) }
func (a modulecallsSortedByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a modulecallsSortedByName) Less(i, j int) bool {
return a[i].Name < a[j].Name
}
func (a modulecallsSortedByName) Len() int { return len(a) }
func (a modulecallsSortedByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a modulecallsSortedByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
type modulecallsSortedBySource []*ModuleCall
@@ -50,6 +49,14 @@ func (a modulecallsSortedBySource) Less(i, j int) bool {
return a[i].Source < a[j].Source
}
type modulecallsSortedByPosition []*ModuleCall
func (a modulecallsSortedByPosition) Len() int { return len(a) }
func (a modulecallsSortedByPosition) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a modulecallsSortedByPosition) Less(i, j int) bool {
return a[i].Position.Filename < a[j].Position.Filename || a[i].Position.Line < a[j].Position.Line
}
type modulecalls []*ModuleCall
func (mm modulecalls) convert() []*terraformsdk.ModuleCall {
@@ -59,6 +66,10 @@ func (mm modulecalls) convert() []*terraformsdk.ModuleCall {
Name: m.Name,
Source: m.Source,
Version: m.Version,
Position: terraformsdk.Position{
Filename: m.Position.Filename,
Line: m.Position.Line,
},
})
}
return list

View File

@@ -11,26 +11,114 @@ the root directory of this source tree.
package terraform
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestModulecallNameWithoutVersion(t *testing.T) {
assert := assert.New(t)
modulecall := ModuleCall{
Name: "provider",
Source: "bar",
func TestModulecallName(t *testing.T) {
tests := map[string]struct {
module ModuleCall
expected string
}{
"WithoutVersion": {
module: ModuleCall{
Name: "provider",
Source: "bar",
},
expected: "bar",
},
"WithVersion": {
module: ModuleCall{
Name: "provider",
Source: "bar",
Version: "1.2.3",
},
expected: "bar,1.2.3",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
assert.Equal(tt.expected, tt.module.FullName())
})
}
assert.Equal("bar", modulecall.FullName())
}
func TestModulecallNameWithVersion(t *testing.T) {
assert := assert.New(t)
modulecall := ModuleCall{
Name: "provider",
Source: "bar",
Version: "1.2.3",
func TestModulecallSort(t *testing.T) {
modules := sampleModulecalls()
tests := map[string]struct {
sortType sort.Interface
expected []string
}{
"ByName": {
sortType: modulecallsSortedByName(modules),
expected: []string{"a", "b", "c", "d", "e", "f"},
},
"BySource": {
sortType: modulecallsSortedBySource(modules),
expected: []string{"f", "d", "c", "e", "a", "b"},
},
"ByPosition": {
sortType: modulecallsSortedByPosition(modules),
expected: []string{"b", "c", "a", "e", "d", "f"},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
sort.Sort(tt.sortType)
actual := make([]string, len(modules))
for k, i := range modules {
actual[k] = i.Name
}
assert.Equal(tt.expected, actual)
})
}
}
func sampleModulecalls() []*ModuleCall {
return []*ModuleCall{
{
Name: "a",
Source: "z",
Version: "1.2.3",
Position: Position{Filename: "foo/main.tf", Line: 35},
},
{
Name: "b",
Source: "z",
Version: "1.2.3",
Position: Position{Filename: "foo/main.tf", Line: 10},
},
{
Name: "c",
Source: "m",
Version: "1.2.3",
Position: Position{Filename: "foo/main.tf", Line: 23},
},
{
Name: "e",
Source: "x",
Version: "1.2.3",
Position: Position{Filename: "foo/main.tf", Line: 42},
},
{
Name: "d",
Source: "l",
Version: "1.2.3",
Position: Position{Filename: "foo/main.tf", Line: 51},
},
{
Name: "f",
Source: "a",
Version: "1.2.3",
Position: Position{Filename: "foo/main.tf", Line: 59},
},
}
assert.Equal("bar,1.2.3", modulecall.FullName())
}