Skip to content

Commit

Permalink
Merge pull request #7 from janritter/fix/issue-5
Browse files Browse the repository at this point in the history
Issue #5 and #6
  • Loading branch information
janritter authored Mar 24, 2019
2 parents 587f600 + a8ecd87 commit a9ff90e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 22 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion backend/iface/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package iface

type BackendAPI interface {
GenerateStatefileForBackend(in interface{}) error
GenerateConfigurationForBackend(in interface{}) error
}
2 changes: 1 addition & 1 deletion backend/s3/api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package s3

type S3BackendAPI interface {
GenerateStatefileForBackend(in interface{}) error
GenerateConfigurationForBackend(in interface{}) error
}

type S3Backend struct{}
Expand Down
46 changes: 29 additions & 17 deletions backend/s3/create.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
9 changes: 7 additions & 2 deletions backend/s3/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ to quickly create a Cobra application.`,
BackendAPI: backendInterface,
}

backendBase.GenerateStatefileForBackend(decoded)
backendBase.GenerateConfigurationForBackend(decoded)
},
}

Expand Down
50 changes: 50 additions & 0 deletions helper/terraform.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a9ff90e

Please sign in to comment.