diff --git a/backend/s3/parsercall.go b/backend/s3/parsercall.go index 793379d..0dec2b7 100644 --- a/backend/s3/parsercall.go +++ b/backend/s3/parsercall.go @@ -13,29 +13,25 @@ func callParserForBackendParameters(in interface{}, out *stateConfig) error { if err != nil { return err } - out.Bucket = bucket + out.Bucket = helper.GetStringAfterSettingPlaceholderValues(bucket) dynamodbTable, _, err := varParser.GetBackendParameterString("state_dynamodb_table", false) if err != nil { return err } - out.DynamoDBTable = dynamodbTable + out.DynamoDBTable = helper.GetStringAfterSettingPlaceholderValues(dynamodbTable) stateKey, _, err := varParser.GetBackendParameterString("state_key", false) if err != nil { return err } - err, stateKey = helper.ReplacePlaceholderInStateKey(stateKey) - if err != nil { - return err - } - out.Key = stateKey + out.Key = helper.GetStringAfterSettingPlaceholderValues(stateKey) region, _, err := varParser.GetBackendParameterString("region", false) if err != nil { return err } - out.Region = region + out.Region = helper.GetStringAfterSettingPlaceholderValues(region) return nil } diff --git a/backend/s3/parsercall_test.go b/backend/s3/parsercall_test.go index 6bde077..4adf31d 100644 --- a/backend/s3/parsercall_test.go +++ b/backend/s3/parsercall_test.go @@ -120,10 +120,6 @@ func TestCallParserForBackendParametersInvalidKey(t *testing.T) { testMap["state_dynamodb_table"] = "test_bucket_table" testMap["state_key"] = "test/terraform.tfstate" testMap["region"] = "eu-central-1" - - err = callParserForBackendParameters(testMap, &stateConfigResult) - assert.Error(t, err, "Expected error") - assert.Equal(t, errors.New("{{current.dir}} is missing the state_key parameter"), err) } func TestCallParserForBackendParametersMissingRegion(t *testing.T) { diff --git a/cmd/destroy.go b/cmd/destroy.go new file mode 100644 index 0000000..3b5db31 --- /dev/null +++ b/cmd/destroy.go @@ -0,0 +1,50 @@ +/* +Copyright © 2020 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "fmt" + + "github.com/fatih/color" + "github.com/spf13/cobra" +) + +// destroyCmd represents the destroy command +var destroyCmd = &cobra.Command{ + Use: "destroy", + Short: "Run terraform destroy through terrastate, also executes terrastate and terraform init", + Run: func(cmd *cobra.Command, args []string) { + // Terrastate + fmt.Printf("Running terrastate \n\n") + rootCmd.Run(cmd, args) + + // Terraform init + if err := getTerraformExecCmdForSubcommand("init", varFile).Run(); err != nil { + color.Red("terraform init returned the following error code: " + err.Error()) + return + } + + // Terraform destroy + if err := getTerraformExecCmdForSubcommand("destroy", varFile).Run(); err != nil { + color.Red("terraform destroy returned the following error code: " + err.Error()) + return + } + }, +} + +func init() { + rootCmd.AddCommand(destroyCmd) +} diff --git a/helper/placeholder.go b/helper/placeholder.go new file mode 100644 index 0000000..28a6f4f --- /dev/null +++ b/helper/placeholder.go @@ -0,0 +1,43 @@ +package helper + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/fatih/color" +) + +type placeholder struct { + key string + value string +} + +var placeholders = []placeholder{ + { + key: "current.dir", + value: getCurrentDir(), + }, +} + +func getCurrentDir() string { + path, err := os.Getwd() + if err != nil { + color.Red(err.Error()) + } + return filepath.Base(path) +} + +func GetStringAfterSettingPlaceholderValues(input string) string { + output := strings.ReplaceAll(input, " ", "") + + for _, placeholder := range placeholders { + if strings.Contains(output, "{{"+placeholder.key+"}}") { + fmt.Println("PLACEHOLDER - replacing '{{" + placeholder.key + "}}' in '" + output + "' with '" + placeholder.value + "'") + } + output = strings.ReplaceAll(output, "{{"+placeholder.key+"}}", placeholder.value) + } + + return output +} diff --git a/helper/statekey.go b/helper/statekey.go deleted file mode 100644 index add2794..0000000 --- a/helper/statekey.go +++ /dev/null @@ -1,34 +0,0 @@ -package helper - -import ( - "errors" - "fmt" - "os" - "path/filepath" - "strings" - - "github.com/fatih/color" -) - -func ReplacePlaceholderInStateKey(stateKey string) (error, string) { - // Remove all spaces - key := strings.ReplaceAll(stateKey, " ", "") - - // Check if the key string contains current.dir - if !strings.Contains(key, "{{current.dir}}") { - err := errors.New("{{current.dir}} is missing the state_key parameter") - color.Red(err.Error()) - return err, "" - } - - // Replace placeholder with current dir - path, err := os.Getwd() - if err != nil { - color.Red(err.Error()) - return err, "" - } - dir := filepath.Base(path) - fmt.Println("Current Directory = " + dir) - key = strings.ReplaceAll(key, "{{current.dir}}", dir) - return nil, key -}