Rough draft of aliased settings [#66]

This commit is contained in:
Erin Call 2020-01-07 15:25:54 -08:00
parent 7cfe20db1f
commit 04de280821
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
2 changed files with 61 additions and 0 deletions

View file

@ -65,6 +65,29 @@ func NewConfig(stdout, stderr io.Writer) (*Config, error) {
return nil, err 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) { if justNumbers.MatchString(cfg.Timeout) {
cfg.Timeout = fmt.Sprintf("%ss", 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"`
}

View file

@ -70,6 +70,35 @@ func (suite *ConfigTestSuite) TestNewConfigInfersNumbersAreSeconds() {
suite.Equal("42s", cfg.Timeout) 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() { func (suite *ConfigTestSuite) TestNewConfigSetsWriters() {
stdout := &strings.Builder{} stdout := &strings.Builder{}
stderr := &strings.Builder{} stderr := &strings.Builder{}