From 04de280821da2b1cabc7bb12212b434e9468d02a Mon Sep 17 00:00:00 2001 From: Erin Call Date: Tue, 7 Jan 2020 15:25:54 -0800 Subject: [PATCH] Rough draft of aliased settings [#66] --- internal/helm/config.go | 32 ++++++++++++++++++++++++++++++++ internal/helm/config_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/internal/helm/config.go b/internal/helm/config.go index 1c8a393..2eda281 100644 --- a/internal/helm/config.go +++ b/internal/helm/config.go @@ -65,6 +65,29 @@ func NewConfig(stdout, stderr io.Writer) (*Config, error) { return nil, err } + var aliases settingAliases + if err := envconfig.Process("plugin", &aliases); err != nil { + return nil, err + } + if aliases.Command != nil { + cfg.Command = *aliases.Command + } + if aliases.AddRepos != nil { + cfg.AddRepos = *aliases.AddRepos + } + if aliases.APIServer != nil { + cfg.APIServer = *aliases.APIServer + } + if aliases.ServiceAccount != nil { + cfg.ServiceAccount = *aliases.ServiceAccount + } + if aliases.Wait != nil { + cfg.Wait = *aliases.Wait + } + if aliases.Force != nil { + cfg.Force = *aliases.Force + } + if justNumbers.MatchString(cfg.Timeout) { cfg.Timeout = fmt.Sprintf("%ss", cfg.Timeout) } @@ -94,3 +117,12 @@ func (cfg *Config) deprecationWarn() { } } } + +type settingAliases struct { + Command *string `envconfig:"mode"` + AddRepos *[]string `envconfig:"add_repos"` + APIServer *string `envconfig:"kubernetes_api_server"` + ServiceAccount *string `envconfig:"kubernetes_service_account"` + Wait *bool `envconfig:"wait_for_upgrade"` + Force *bool `envconfig:"force_upgrade"` +} diff --git a/internal/helm/config_test.go b/internal/helm/config_test.go index 6cad789..cb4c3ee 100644 --- a/internal/helm/config_test.go +++ b/internal/helm/config_test.go @@ -70,6 +70,35 @@ func (suite *ConfigTestSuite) TestNewConfigInfersNumbersAreSeconds() { suite.Equal("42s", cfg.Timeout) } +func (suite *ConfigTestSuite) TestNewConfigWithAliases() { + for _, varname := range []string{ + "HELM_COMMAND", + "HELM_REPOS", + "API_SERVER", + "SERVICE_ACCOUNT", + "WAIT", + "FORCE", + } { + suite.unsetenv(varname) + suite.unsetenv("PLUGIN_" + varname) + } + suite.setenv("PLUGIN_MODE", "iambic") + suite.setenv("PLUGIN_ADD_REPOS", "chortle=http://calloo.callay/frabjous/day") + suite.setenv("PLUGIN_KUBERNETES_API_SERVER", "http://tumtum.tree") + suite.setenv("PLUGIN_KUBERNETES_SERVICE_ACCOUNT", "tulgey") + suite.setenv("PLUGIN_WAIT_FOR_UPGRADE", "true") + suite.setenv("PLUGIN_FORCE_UPGRADE", "true") + + cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{}) + suite.Require().NoError(err) + suite.Equal("iambic", cfg.Command) + suite.Equal([]string{"chortle=http://calloo.callay/frabjous/day"}, cfg.AddRepos) + suite.Equal("http://tumtum.tree", cfg.APIServer) + suite.Equal("tulgey", cfg.ServiceAccount) + suite.True(cfg.Wait, "Wait should be aliased") + suite.True(cfg.Force, "Force should be aliased") +} + func (suite *ConfigTestSuite) TestNewConfigSetsWriters() { stdout := &strings.Builder{} stderr := &strings.Builder{}