Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Clean flag added #14

Merged
merged 28 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b697a5d
Added destination option to configuration, so each module could be ad…
ivan-kostko Jan 19, 2023
0c7e7c8
Adjusted tests, so they are extended, not changed
ivan-kostko Jan 20, 2023
48af9b2
Copy clonned modules concurrently
ivan-kostko Jan 20, 2023
cf89a45
Explicitly passing parameters to routines
ivan-kostko Jan 20, 2023
573b79a
Destination is appended with ModulePath
ivan-kostko Jan 20, 2023
b7f4cda
updates readme, initially fetches module to temp dir
mr00wka Feb 3, 2023
a13397b
Clean-up destinations upon distributing version
ivan-kostko Feb 8, 2023
c753483
Cleanup and fixed firstDestination
ivan-kostko Feb 10, 2023
4ee1013
Refactored dependency to deprecated ioutil -> os
ivan-kostko Feb 10, 2023
95367a6
Remove test artifacts upon re-run
ivan-kostko Feb 10, 2023
ec032e1
updates readme file
mr00wka Feb 10, 2023
28ec4f6
Tests adjusted to represent documentation case
ivan-kostko Feb 13, 2023
1582e7e
Merge branch 'iak/destinations' of github.com:ivan-kostko/terrafile i…
ivan-kostko Feb 13, 2023
a961af9
Better log output fro cleanup
ivan-kostko Feb 13, 2023
fcccbd0
Removed dev environment
ivan-kostko Feb 13, 2023
2d99974
Apply suggestions from code review fixing typos
ivan-kostko Feb 14, 2023
b5dfbf0
CR Fixes
ivan-kostko Feb 14, 2023
bc012e5
Apply suggestions from code review to documentation
ivan-kostko Feb 14, 2023
b7302fc
Apply suggestions from code review fixing typos
ivan-kostko Feb 15, 2023
5e35d78
Implemention of Clean flag
ivan-kostko Feb 15, 2023
c1b35db
Bumped dependencies versions
ivan-kostko Feb 15, 2023
faf6dc4
Tests for Clean flag
ivan-kostko Feb 15, 2023
59d3fa4
Vendor packages update
ivan-kostko Feb 15, 2023
1b135c8
Vendor packages update
ivan-kostko Feb 15, 2023
63061bc
Merge master
ivan-kostko Feb 15, 2023
44cf13d
Merge master (cleanup)
ivan-kostko Feb 15, 2023
ed66d1c
Apply suggestions from code review
ivan-kostko Feb 16, 2023
a8f43cd
CR suggestions:
ivan-kostko Feb 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ require (
github.com/nritholtz/stdemuxerhook v0.0.0-20181016194454-2c86ca05d211
github.com/rendon/testcli v0.0.0-20161027181003-6283090d169f
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.2.2
github.com/stretchr/testify v1.8.1
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 11 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
Expand All @@ -10,8 +11,14 @@ github.com/rendon/testcli v0.0.0-20161027181003-6283090d169f h1:onGP+qmYmjKs7pkm
github.com/rendon/testcli v0.0.0-20161027181003-6283090d169f/go.mod h1:cq57a4l475CeMvE7RRpSui1MEqCmhirIt1E7kl8BC2Q=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68 h1:z8Hj/bl9cOV2grsOpEaQFUaly0JWN3i97mo3jXKJNp0=
Expand All @@ -20,3 +27,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
46 changes: 46 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var opts struct {
ModulePath string `short:"p" long:"module_path" default:"./vendor/modules" description:"File path to install generated terraform modules, if not overridden by 'destinations:' field"`

TerrafilePath string `short:"f" long:"terrafile_file" default:"./Terrafile" description:"File path to the Terrafile file"`

Clean bool `short:"c" long:"clean" description:"Remove all artifacts from destinations and module path upon fetching module(s)"`
nritholtz marked this conversation as resolved.
Show resolved Hide resolved
}

// To be set by goreleaser on build
Expand Down Expand Up @@ -93,6 +95,10 @@ func main() {
log.Fatalf("failed to parse yaml file due to error: %s", err)
}

if opts.Clean {
cleanDestinations(getUniqueDestinations(config))
nritholtz marked this conversation as resolved.
Show resolved Hide resolved
}

// Clone modules
var wg sync.WaitGroup
_ = os.RemoveAll(opts.ModulePath)
Expand Down Expand Up @@ -157,3 +163,43 @@ func main() {

wg.Wait()
}

func getUniqueDestinations(config map[string]module) []string {

// Map filters duplicates
ivan-kostko marked this conversation as resolved.
Show resolved Hide resolved
uniqueDestinationsM := make(map[string]string)
ivan-kostko marked this conversation as resolved.
Show resolved Hide resolved

// Range over config and gather all uniquieDestinations
ivan-kostko marked this conversation as resolved.
Show resolved Hide resolved
for _, m := range config {
if len(m.Destinations) == 0 {
uniqueDestinationsM[opts.ModulePath] = opts.ModulePath
ivan-kostko marked this conversation as resolved.
Show resolved Hide resolved
continue
}

// range over Destinations and put them into map
for _, dst := range m.Destinations {
// Destination supposed to be conjunction of destination defined in file with module path
d := filepath.Join(dst, opts.ModulePath)
uniqueDestinationsM[d] = d
}
}

uniqueDestinations := make([]string, len(uniqueDestinationsM))
nritholtz marked this conversation as resolved.
Show resolved Hide resolved
i := 0
for dst := range uniqueDestinationsM {
uniqueDestinations[i] = dst
i++
}

return uniqueDestinations
}

func cleanDestinations(destinations []string) {
for _, dst := range destinations {

log.Infof("[*] Removing artifacts from %s", dst)
if err := os.RemoveAll(dst); err != nil {
log.Errorf("Failed to remove artifacts from %s due to error: %s", dst, err)
}
}
}
111 changes: 111 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ func TestTerraformWithTerrafilePath(t *testing.T) {
} {
assert.Contains(t, testcli.Stdout(), output)
}

// Assert output does not contain the following
for _, output := range []string{
"[*] Removing artifacts from ./vendor/modules",
"[*] Removing artifacts from testdata/networking/vendor/modules",
"[*] Removing artifacts from testdata/some-other-stack/vendor/modules",
"[*] Removing artifacts from testdata/iam/vendor/modules",
"[*] Removing artifacts from testdata/onboarding/vendor/modules",
} {
assert.NotContains(t, testcli.Stdout(), output)
}

// Assert folder exist with default destination
for _, moduleName := range []string{
"tf-aws-vpc",
Expand Down Expand Up @@ -174,6 +186,81 @@ func TestTerraformWithTerrafilePath(t *testing.T) {
}
}

func TestTerraformWithTerrafileClean(t *testing.T) {
folder, back := setup(t)
defer back()

// Run original Terrafile with path
testcli.Run(terrafileBinaryPath, "-f", fmt.Sprint(folder, "/Terrafile"))

defer println(testcli.Stdout())
defer println(testcli.Stderr())

defer func() {
assert.NoError(t, os.RemoveAll("testdata/"))
}()

// Create Terrafile2.yaml
createTerrafile2(t, folder)

// Run terrafile with path to Terrafile2 and clean flag
testcli.Run(terrafileBinaryPath, "-f", fmt.Sprint(folder, "/Terrafile2"), "-c")

// Print out output of second run
defer println(testcli.Stdout())
defer println(testcli.Stderr())

if !testcli.Success() {
t.Fatalf("Expected to succeed, but failed: %q with message: %q", testcli.Error(), testcli.Stderr())
}

// Assert output
for _, output := range []string{
"[*] Removing artifacts from ./vendor/modules",
"[*] Removing artifacts from testdata/networking/vendor/modules",
"[*] Removing artifacts from testdata/some-other-stack/vendor/modules",
"[*] Removing artifacts from testdata/iam/vendor/modules",
"[*] Removing artifacts from testdata/onboarding/vendor/modules",
} {
assert.Contains(t, testcli.Stdout(), output)
}

// Assert files exist with default destination
for _, moduleName := range []string{
"tf-aws-vpc/main.tf",
} {
assert.FileExists(t, path.Join(workingDirectory, "vendor/modules", moduleName))
}

// Assert files does not exist with default destination
for _, moduleName := range []string{
"tf-aws-vpc-experimental/main.tf",
} {
assert.NoFileExists(t, path.Join(workingDirectory, "vendor/modules", moduleName))
}

// Assert files exist with non-default destinations
for _, moduleName := range []string{
"testdata/networking/vendor/modules/tf-aws-vpn-gateway/main.tf",

// terraform-aws-modules/terraform-aws-iam doesn't have main.tf, as it represents set of modules
// However, some terraform-aws-modules/terraform-aws-iam/modules have, e.g.:
"testdata/iam/vendor/modules/tf-aws-iam/modules/iam-account/main.tf",
"testdata/onboarding/vendor/modules/tf-aws-s3-bucket/main.tf",
"testdata/some-other-stack/vendor/modules/tf-aws-vpn-gateway/main.tf",
} {
assert.FileExists(t, path.Join(workingDirectory, moduleName))
}

// Assert files do not exist with non-default destinations
for _, moduleName := range []string{
"testdata/networking/vendor/modules/tf-aws-s3-bucket/main.tf",
"testdata/some-other-stack/vendor/modules/tf-aws-s3-bucket/main.tf",
} {
assert.NoFileExists(t, path.Join(workingDirectory, moduleName))
}
}

func setup(t *testing.T) (current string, back func()) {
folder, err := os.MkdirTemp("", "")
assert.NoError(t, err)
Expand Down Expand Up @@ -214,3 +301,27 @@ tf-aws-s3-bucket:
`
createFile(t, path.Join(folder, "Terrafile"), yaml)
}

func createTerrafile2(t *testing.T, folder string) {
var yaml = `tf-aws-vpc:
source: "[email protected]:terraform-aws-modules/terraform-aws-vpc"
version: "v1.46.0"
tf-aws-vpn-gateway:
source: "[email protected]:terraform-aws-modules/terraform-aws-vpn-gateway"
version: "v3.2.0"
destinations:
- testdata/networking
- testdata/some-other-stack
tf-aws-iam:
source: "[email protected]:terraform-aws-modules/terraform-aws-iam"
version: "v5.11.1"
destinations:
- testdata/iam
tf-aws-s3-bucket:
source: "[email protected]:terraform-aws-modules/terraform-aws-s3-bucket"
version: "v3.6.1"
destinations:
- testdata/onboarding
`
createFile(t, path.Join(folder, "Terrafile2"), yaml)
}
35 changes: 17 additions & 18 deletions vendor/github.com/stretchr/testify/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading