From ae64bcb5e10043a8c9eb8cca3f11006fee1554f0 Mon Sep 17 00:00:00 2001 From: Jan Ritter Date: Sun, 24 Mar 2019 12:29:42 +0100 Subject: [PATCH 1/4] refactor: renamed statefile references to backend configuration, since statefile has a different meaning --- backend/iface/interface.go | 2 +- backend/s3/api.go | 2 +- backend/s3/create.go | 2 +- backend/s3/generate.go | 4 ++-- cmd/root.go | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/backend/iface/interface.go b/backend/iface/interface.go index d7297da..e0e7cf6 100644 --- a/backend/iface/interface.go +++ b/backend/iface/interface.go @@ -1,5 +1,5 @@ package iface type BackendAPI interface { - GenerateStatefileForBackend(in interface{}) error + GenerateConfigurationForBackend(in interface{}) error } diff --git a/backend/s3/api.go b/backend/s3/api.go index fcfcc2c..19158eb 100644 --- a/backend/s3/api.go +++ b/backend/s3/api.go @@ -1,7 +1,7 @@ package s3 type S3BackendAPI interface { - GenerateStatefileForBackend(in interface{}) error + GenerateConfigurationForBackend(in interface{}) error } type S3Backend struct{} diff --git a/backend/s3/create.go b/backend/s3/create.go index 8e1eb0e..86f8499 100644 --- a/backend/s3/create.go +++ b/backend/s3/create.go @@ -7,7 +7,7 @@ import ( "github.com/fatih/color" ) -func createStateFile(in stateConfig) error { +func createBackendConfigurationFile(in stateConfig) error { fileContent := `terraform { backend "s3" { diff --git a/backend/s3/generate.go b/backend/s3/generate.go index ac004a1..61ca56e 100644 --- a/backend/s3/generate.go +++ b/backend/s3/generate.go @@ -4,7 +4,7 @@ import ( "github.com/janritter/terrastate/helper" ) -func (backend *S3Backend) GenerateStatefileForBackend(in interface{}) error { +func (backend *S3Backend) GenerateConfigurationForBackend(in interface{}) error { stateParams := stateConfig{} err := parseBackendParameter(in, &stateParams) if err != nil { @@ -13,6 +13,6 @@ func (backend *S3Backend) GenerateStatefileForBackend(in interface{}) error { helper.PrintStateValues(stateParams) - err = createStateFile(stateParams) + err = createBackendConfigurationFile(stateParams) return err } diff --git a/cmd/root.go b/cmd/root.go index 5422425..e5c672e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -85,7 +85,7 @@ to quickly create a Cobra application.`, BackendAPI: backendInterface, } - backendBase.GenerateStatefileForBackend(decoded) + backendBase.GenerateConfigurationForBackend(decoded) }, } From 18ff8a25e10461dbb1f14fd77f5a98b1531a14dc Mon Sep 17 00:00:00 2001 From: Jan Ritter Date: Sun, 24 Mar 2019 14:21:55 +0100 Subject: [PATCH 2/4] fix: use golang template to generate backend configuration - fixes #5 --- backend/s3/create.go | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/backend/s3/create.go b/backend/s3/create.go index 86f8499..c1be472 100644 --- a/backend/s3/create.go +++ b/backend/s3/create.go @@ -1,29 +1,41 @@ package s3 import ( - "io/ioutil" - "log" + "os" + "text/template" "github.com/fatih/color" ) func createBackendConfigurationFile(in stateConfig) error { - fileContent := - `terraform { - backend "s3" { - encrypt = true - bucket = "` + in.Bucket + `" - region = "` + in.Region + `" - key = "` + in.Key + `" - dynamodb_table = "` + in.DynamoDBTable + `" - } -} -` + t, err := template.New("backend").Parse(`terraform { + backend "s3" { + encrypt = true + {{ if .Bucket }} + bucket = "{{ .Bucket }}" + {{ end }} + {{ if .Region }} + region = "{{ .Region }}" + {{ end }} + {{ if .Key }} + key = "{{ .Key }}" + {{ end }} + {{ if .DynamoDBTable }} + dynamodb_table = "{{ .DynamoDBTable }}" + {{ end }} + } + } + `) + + f, err := os.Create("terrastate.tf") + if err != nil { + color.Red(err.Error()) + return err + } - data := []byte(fileContent) - err := ioutil.WriteFile("terrastate.tf", data, 0644) + err = t.Execute(f, in) if err != nil { - log.Println(err) + color.Red(err.Error()) return err } From d2a834d9bd48b0949b3092538e66a43df0fba83f Mon Sep 17 00:00:00 2001 From: Jan Ritter Date: Sun, 24 Mar 2019 15:26:14 +0100 Subject: [PATCH 3/4] feat: added option to automaticly remove the .terraform folder when creation a new backend config file - closes #6 --- backend/s3/generate.go | 5 +++++ helper/terraform.go | 50 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 helper/terraform.go diff --git a/backend/s3/generate.go b/backend/s3/generate.go index 61ca56e..97960d4 100644 --- a/backend/s3/generate.go +++ b/backend/s3/generate.go @@ -13,6 +13,11 @@ func (backend *S3Backend) GenerateConfigurationForBackend(in interface{}) error helper.PrintStateValues(stateParams) + err = helper.RemoveDotTerraformFolder(in) + if err != nil { + return err + } + err = createBackendConfigurationFile(stateParams) return err } diff --git a/helper/terraform.go b/helper/terraform.go new file mode 100644 index 0000000..05b4197 --- /dev/null +++ b/helper/terraform.go @@ -0,0 +1,50 @@ +package helper + +import ( + "errors" + "log" + "os" + "reflect" + + "github.com/fatih/color" +) + +func RemoveDotTerraformFolder(in interface{}) error { + switch in.(type) { + case map[string]interface{}: + mapped := in.(map[string]interface{}) + if mapped["state_auto_remove_old"] == nil { + color.Blue("Skipping removing of .terraform folder") + + return nil + } + if reflect.TypeOf(mapped["state_auto_remove_old"]).String() != "bool" { + err := errors.New("state_auto_remove_old must be of type bool, was " + reflect.TypeOf(mapped["state_auto_remove_old"]).String()) + log.Println(err) + + return err + } + if !mapped["state_auto_remove_old"].(bool) { + color.Blue("Skipping removing of .terraform folder") + + return nil + } + + default: + err := errors.New("Unknown var-file format") + log.Println(err) + + return err + } + + err := os.RemoveAll(".terraform") + if err != nil { + color.Red(err.Error()) + + return err + } + + color.Green("Removed .terraform folder") + + return nil +} From a8ecd8767aeaee193f393104da757f6e8ba5f19b Mon Sep 17 00:00:00 2001 From: Jan Ritter Date: Sun, 24 Mar 2019 15:43:17 +0100 Subject: [PATCH 4/4] docs: added documentation for the .terraform folder remove feature - relates to #6 --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 30a0a15..72859d8 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,18 @@ Feel free to add a new backend and create a pull request - [How to create a new For more information about the backend specific variables click the backend in the list above. +#### Optional Terraform variables + +##### Automatic removal of the .terraform folder + +```bash +state_auto_remove_old = true +``` + +When you set the value to true, the .terraform folder in the current directory gets removed when you create a terrastate.tf backend configuration through terrastate. If you set the value to false or don't set it, then the .terraform folder will not be removed. + +This option allowes you to execute terrastate and then directly terraform init without manually removing the .terraform folder. + ### Version ``` bash