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 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..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 createStateFile(in stateConfig) error { - fileContent := - `terraform { - backend "s3" { - encrypt = true - bucket = "` + in.Bucket + `" - region = "` + in.Region + `" - key = "` + in.Key + `" - dynamodb_table = "` + in.DynamoDBTable + `" - } -} -` +func createBackendConfigurationFile(in stateConfig) error { + 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 } diff --git a/backend/s3/generate.go b/backend/s3/generate.go index ac004a1..97960d4 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,11 @@ func (backend *S3Backend) GenerateStatefileForBackend(in interface{}) error { helper.PrintStateValues(stateParams) - err = createStateFile(stateParams) + err = helper.RemoveDotTerraformFolder(in) + if err != nil { + return err + } + + 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) }, } 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 +}