Skip to content

Commit

Permalink
feat: Terraform version detected from project config
Browse files Browse the repository at this point in the history
old projects are backward compatible
  • Loading branch information
wobondar committed Nov 30, 2022
1 parent 975e4d5 commit f57370f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea
src/build/
src/build*/
config.tf
environment.tf
.env
bin
bin
7 changes: 7 additions & 0 deletions src/command_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"gopkg.in/alecthomas/kingpin.v2"
"path/filepath"
"strconv"
"strings"
"text/template"
)
Expand Down Expand Up @@ -36,6 +37,7 @@ type BackendConfig struct {
TerraformStateKey string
TerraformLockTable string
KmsKeyArn string
TerraformVersion int
}

type dotEnv struct {
Expand Down Expand Up @@ -161,6 +163,10 @@ func (c *BackendCommand) applyInvoker(config *BackendConfig) {
c.log.ShowOpts("Invoker", "enabled")
config.TerraformStateKey = strings.Join([]string{config.TerraformStateKey, defaultTerraformInvokerSuffix}, "-")
}

if config.TerraformVersion >= 2 {
config.TerraformStateKey = config.TerraformStateKey + "/v" + strconv.Itoa(config.TerraformVersion)
}
}

func (c *BackendCommand) dotEnvMapper(env *dotEnv) *BackendConfig {
Expand All @@ -172,6 +178,7 @@ func (c *BackendCommand) dotEnvMapper(env *dotEnv) *BackendConfig {
KmsKeyArn: env.environment["KMS_KEY_ARN"],
TerraformStateKey: env.project["TERRAFORM_STATE_KEY"],
Domain: env.project["DOMAIN"],
TerraformVersion: c.app.IntResolver(env.project["TERRAFORM_VERSION"]),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

const Version = "v0.5.0"
const Version = "v0.5.1"

const CiEnvVar = "CI"
const EnvVersionVar = "TF_ENV_VERSION"
Expand Down
9 changes: 9 additions & 0 deletions src/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/joho/godotenv"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
)
Expand Down Expand Up @@ -125,6 +126,14 @@ func (a *App) BoolResolver(text string) bool {
return false
}

func (a *App) IntResolver(text string) int {
if strings.EqualFold(text, "") || strings.EqualFold(text, "1") {
return 1
}
intVar, _ := strconv.Atoi(text)
return intVar
}

func (a *App) isNewEnvVersion() bool {
if strings.EqualFold(a.envVersion, "2") {
return true
Expand Down

0 comments on commit f57370f

Please sign in to comment.