Allow a configurable env var prefix [#19]

I'd like to keep Prefix's scope fairly limited, because it has potential
to spiral into something magnificently complex. You get one prefix
setting, it goes in `settings` not `environment`, end of feature.
This commit is contained in:
Erin Call 2019-12-23 16:07:49 -08:00
parent db87bd0507
commit 285e9d98a4
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
2 changed files with 53 additions and 1 deletions

View file

@ -42,9 +42,17 @@ func (cfg *Config) Populate() error {
return err
}
prefix := cfg.Prefix
if err := envconfig.Process("", cfg); err != nil {
return err
}
if prefix != "" {
if err := envconfig.Process(cfg.Prefix, cfg); err != nil {
return err
}
}
return nil
}

View file

@ -18,6 +18,7 @@ func TestConfigTestSuite(t *testing.T) {
}
func (suite *ConfigTestSuite) TestPopulateWithPluginPrefix() {
suite.unsetenv("PLUGIN_PREFIX")
suite.unsetenv("HELM_COMMAND")
suite.unsetenv("UPDATE_DEPENDENCIES")
suite.unsetenv("DEBUG")
@ -35,6 +36,7 @@ func (suite *ConfigTestSuite) TestPopulateWithPluginPrefix() {
}
func (suite *ConfigTestSuite) TestPopulateWithNoPrefix() {
suite.unsetenv("PLUGIN_PREFIX")
suite.unsetenv("PLUGIN_HELM_COMMAND")
suite.unsetenv("PLUGIN_UPDATE_DEPENDENCIES")
suite.unsetenv("PLUGIN_DEBUG")
@ -51,14 +53,56 @@ func (suite *ConfigTestSuite) TestPopulateWithNoPrefix() {
suite.True(cfg.Debug)
}
func (suite *ConfigTestSuite) TestPopulateWithConfigurablePrefix() {
suite.unsetenv("API_SERVER")
suite.unsetenv("PLUGIN_API_SERVER")
suite.setenv("PLUGIN_PREFIX", "prix_fixe")
suite.setenv("PRIX_FIXE_API_SERVER", "your waiter this evening")
cfg := Config{}
suite.Require().NoError(cfg.Populate())
suite.Equal("prix_fixe", cfg.Prefix)
suite.Equal("your waiter this evening", cfg.APIServer)
}
func (suite *ConfigTestSuite) TestPrefixSettingDoesNotAffectPluginPrefix() {
suite.setenv("PLUGIN_PREFIX", "IXFREP")
suite.setenv("PLUGIN_HELM_COMMAND", "wake me up")
suite.setenv("IXFREP_PLUGIN_HELM_COMMAND", "send me to sleep inside")
cfg := Config{}
suite.Require().NoError(cfg.Populate())
suite.Equal("wake me up", cfg.Command)
}
func (suite *ConfigTestSuite) TestPrefixSettingMustHavePluginPrefix() {
suite.unsetenv("PLUGIN_PREFIX")
suite.setenv("PREFIX", "refpix")
suite.setenv("HELM_COMMAND", "gimme more")
suite.setenv("REFPIX_HELM_COMMAND", "gimme less")
cfg := Config{}
suite.Require().NoError(cfg.Populate())
suite.Equal("gimme more", cfg.Command)
}
func (suite *ConfigTestSuite) TestPopulateWithConflictingVariables() {
suite.setenv("PLUGIN_HELM_COMMAND", "execute order 66")
suite.setenv("HELM_COMMAND", "defend the jedi")
suite.setenv("HELM_COMMAND", "defend the jedi") // values from the `environment` block override those from `settings`
suite.setenv("PLUGIN_PREFIX", "prod")
suite.setenv("TIMEOUT", "5m0s")
suite.setenv("PROD_TIMEOUT", "2m30s") // values from prefixed env vars override those from non-prefixed ones
cfg := Config{}
suite.Require().NoError(cfg.Populate())
suite.Equal("defend the jedi", cfg.Command)
suite.Equal("2m30s", cfg.Timeout)
}
func (suite *ConfigTestSuite) setenv(key, val string) {