From 9d5bf5d771f5882931e71feb6d3c32402079e132 Mon Sep 17 00:00:00 2001 From: Aliaksandr Panasiuk Date: Wed, 24 Jul 2024 18:16:13 +0200 Subject: [PATCH 1/2] #34 - add support Terraform outputs of boolean types --- config/config.go | 23 +++++++++++-------- .../exported-environment-variables.md | 2 +- docs/release-notes.md | 1 + 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 092f228..09d5de6 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,7 @@ import ( "path/filepath" "reflect" "runtime" + "strconv" "strings" "text/template" @@ -51,8 +52,8 @@ type Config struct { } type ExportedVars struct { - TerraformOutput map[string]string `yaml:"terraform-output,omitempty"` - Env map[string]string `yaml:"env,omitempty"` + TerraformOutput map[string]interface{} `yaml:"terraform-output,omitempty"` + Env map[string]string `yaml:"env,omitempty"` } type HookMapping struct { @@ -117,7 +118,7 @@ func (conf *Config) InitConfig(terraformOutput bool) *Config { } conf.ExportedVars = ExportedVars{ - TerraformOutput: make(map[string]string), + TerraformOutput: make(map[string]interface{}), Env: make(map[string]string), } @@ -165,8 +166,8 @@ func (conf *Config) GetConfigs(all bool) error { func (conf *Config) SetRootDomain(c *cli.Context, gitSpecID string) error { hostedZoneVar := system.TerraformVarsPrefix + system.TerraformVarHostedZoneName if !c.IsSet("root-domain") { - if hostedZoneName, ok := conf.TerraformOutput[hostedZoneVar]; ok && len(hostedZoneName) > 0 { - if err := c.Set("root-domain", hostedZoneName); err != nil { + if hostedZoneName, ok := conf.TerraformOutput[hostedZoneVar]; ok && len(hostedZoneName.(string)) > 0 { + if err := c.Set("root-domain", hostedZoneName.(string)); err != nil { return err } } else { @@ -225,12 +226,16 @@ func (conf *Config) GetTerraformOutputs() error { return err } - if reflect.TypeOf(getVar.Type).Kind() == reflect.String { - conf.TerraformOutput[key] = getVar.Value.(string) + switch { + case reflect.TypeOf(getVar.Value).Kind() == reflect.String && getVar.Type == reflect.String.String(): + conf.TerraformOutput[key] = getVar.Value conf.Env[strings.ToUpper(strings.ReplaceAll(key, system.TerraformVarsPrefix, ""))] = getVar.Value.(string) - } else { + case reflect.TypeOf(getVar.Value).Kind() == reflect.Bool && getVar.Type == reflect.Bool.String(): + conf.TerraformOutput[key] = getVar.Value + conf.Env[strings.ToUpper(strings.ReplaceAll(key, system.TerraformVarsPrefix, ""))] = strconv.FormatBool(getVar.Value.(bool)) + default: zap.S().Warnf("Terraform output variable %s will not be exported as environment variable, "+ - "does not match the string type, current type: %s", key, getVar.Type) + "does not match the string or boolean types, current type: %s", key, getVar.Type) } } } diff --git a/docs/configuration/cluster-management/exported-environment-variables.md b/docs/configuration/cluster-management/exported-environment-variables.md index b04b0c2..3432bd0 100644 --- a/docs/configuration/cluster-management/exported-environment-variables.md +++ b/docs/configuration/cluster-management/exported-environment-variables.md @@ -36,7 +36,7 @@ exported-vars: The variables listed in the `env` section will be used when running the `rmk release ...` commands. -> Only the `string` output variables are supported. +> Only the `string` or `boolean` types output variables are supported. ## Default exported variables diff --git a/docs/release-notes.md b/docs/release-notes.md index 00dead0..ab37bc9 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1 +1,2 @@ - #33 - Added a new GitHub action for RMK commands documentation generation. +- #34 - Added support Terraform outputs of boolean types. From dc972124f8a4856bd5a1f22ab19a43061b1873e9 Mon Sep 17 00:00:00 2001 From: Aliaksandr Panasiuk Date: Thu, 25 Jul 2024 08:27:05 +0200 Subject: [PATCH 2/2] #34 - refactoring --- config/config.go | 8 +++++--- docs/release-notes.md | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 09d5de6..b6b5a1b 100644 --- a/config/config.go +++ b/config/config.go @@ -226,16 +226,18 @@ func (conf *Config) GetTerraformOutputs() error { return err } + envKey := strings.ToUpper(strings.ReplaceAll(key, system.TerraformVarsPrefix, "")) + switch { case reflect.TypeOf(getVar.Value).Kind() == reflect.String && getVar.Type == reflect.String.String(): conf.TerraformOutput[key] = getVar.Value - conf.Env[strings.ToUpper(strings.ReplaceAll(key, system.TerraformVarsPrefix, ""))] = getVar.Value.(string) + conf.Env[envKey] = getVar.Value.(string) case reflect.TypeOf(getVar.Value).Kind() == reflect.Bool && getVar.Type == reflect.Bool.String(): conf.TerraformOutput[key] = getVar.Value - conf.Env[strings.ToUpper(strings.ReplaceAll(key, system.TerraformVarsPrefix, ""))] = strconv.FormatBool(getVar.Value.(bool)) + conf.Env[envKey] = strconv.FormatBool(getVar.Value.(bool)) default: zap.S().Warnf("Terraform output variable %s will not be exported as environment variable, "+ - "does not match the string or boolean types, current type: %s", key, getVar.Type) + "does not match string or boolean types, current type: %s", key, getVar.Type) } } } diff --git a/docs/release-notes.md b/docs/release-notes.md index ab37bc9..cbd77a9 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1,2 +1,2 @@ - #33 - Added a new GitHub action for RMK commands documentation generation. -- #34 - Added support Terraform outputs of boolean types. +- #34 - Added support for Terraform outputs of a boolean type.