Skip to content

Commit

Permalink
Merge pull request #24 from janritter/feat/more-templating
Browse files Browse the repository at this point in the history
Feat/more templating
  • Loading branch information
janritter authored May 11, 2020
2 parents d7ac994 + 1a06a2e commit e329002
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 46 deletions.
12 changes: 4 additions & 8 deletions backend/s3/parsercall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 0 additions & 4 deletions backend/s3/parsercall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
50 changes: 50 additions & 0 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
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)
}
43 changes: 43 additions & 0 deletions helper/placeholder.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 0 additions & 34 deletions helper/statekey.go

This file was deleted.

0 comments on commit e329002

Please sign in to comment.