Stabilize the logic for setting-alias conflicts [#66]

This includes a refactor to the way aliases are processed. I had been
thinking in terms of locking down the aliases names pretty tightly, in
order to provide an error if there are conflicts. After discussion with
@josmo, though, it seems like we can do it the same way we do for
"PLUGIN_"/non-prefixed variables, i.e. quietly override them.
This commit is contained in:
Erin Call 2020-01-08 09:31:22 -08:00
parent 1d1117ba49
commit 6aa1d79d56
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
2 changed files with 36 additions and 37 deletions

View file

@ -53,7 +53,25 @@ type Config struct {
// NewConfig creates a Config and reads environment variables into it, accounting for several possible formats. // NewConfig creates a Config and reads environment variables into it, accounting for several possible formats.
func NewConfig(stdout, stderr io.Writer) (*Config, error) { func NewConfig(stdout, stderr io.Writer) (*Config, error) {
var aliases settingAliases
if err := envconfig.Process("plugin", &aliases); err != nil {
return nil, err
}
if err := envconfig.Process("", &aliases); err != nil {
return nil, err
}
cfg := Config{ cfg := Config{
Command: aliases.Command,
AddRepos: aliases.AddRepos,
APIServer: aliases.APIServer,
ServiceAccount: aliases.ServiceAccount,
Wait: aliases.Wait,
Force: aliases.Force,
KubeToken: aliases.KubeToken,
Certificate: aliases.Certificate,
Stdout: stdout, Stdout: stdout,
Stderr: stderr, Stderr: stderr,
} }
@ -65,35 +83,6 @@ 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 aliases.KubeToken != nil {
cfg.KubeToken = *aliases.KubeToken
}
if aliases.Certificate != nil {
cfg.Certificate = *aliases.Certificate
}
if justNumbers.MatchString(cfg.Timeout) { if justNumbers.MatchString(cfg.Timeout) {
cfg.Timeout = fmt.Sprintf("%ss", cfg.Timeout) cfg.Timeout = fmt.Sprintf("%ss", cfg.Timeout)
} }
@ -125,12 +114,12 @@ func (cfg *Config) deprecationWarn() {
} }
type settingAliases struct { type settingAliases struct {
Command *string `envconfig:"mode"` Command string `envconfig:"mode"`
AddRepos *[]string `envconfig:"add_repos"` AddRepos []string `envconfig:"add_repos"`
APIServer *string `envconfig:"kube_api_server"` APIServer string `envconfig:"kube_api_server"`
ServiceAccount *string `envconfig:"kube_service_account"` ServiceAccount string `envconfig:"kube_service_account"`
Wait *bool `envconfig:"wait_for_upgrade"` Wait bool `envconfig:"wait_for_upgrade"`
Force *bool `envconfig:"force_upgrade"` Force bool `envconfig:"force_upgrade"`
KubeToken *string `envconfig:"kube_token"` KubeToken string `envconfig:"kube_token"`
Certificate *string `envconfig:"kube_certificate"` Certificate string `envconfig:"kube_certificate"`
} }

View file

@ -105,6 +105,16 @@ func (suite *ConfigTestSuite) TestNewConfigWithAliases() {
suite.Equal("d2l0aCBpdHMgaGVhZA==", cfg.Certificate, "Certificate should be aliased") suite.Equal("d2l0aCBpdHMgaGVhZA==", cfg.Certificate, "Certificate should be aliased")
} }
func (suite *ConfigTestSuite) TestNewConfigWithAliasConflicts() {
suite.unsetenv("FORCE")
suite.setenv("PLUGIN_FORCE_UPGRADE", "true")
suite.setenv("PLUGIN_FORCE", "false") // should override even when set to the zero value
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
suite.NoError(err)
suite.False(cfg.Force, "official names should override alias names")
}
func (suite *ConfigTestSuite) TestNewConfigSetsWriters() { func (suite *ConfigTestSuite) TestNewConfigSetsWriters() {
stdout := &strings.Builder{} stdout := &strings.Builder{}
stderr := &strings.Builder{} stderr := &strings.Builder{}