Use a go-idiomatic constructor for helm.Config [#9]
This commit is contained in:
parent
10e7e7fee5
commit
4ba1e694d9
|
@ -8,15 +8,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var c helm.Config
|
cfg, err := helm.NewConfig()
|
||||||
|
|
||||||
if err := c.Populate(); err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the plan
|
// Make the plan
|
||||||
plan, err := helm.NewPlan(c)
|
plan, err := helm.NewPlan(*cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "%w\n", err)
|
fmt.Fprintf(os.Stderr, "%w\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -35,23 +35,24 @@ type Config struct {
|
||||||
Force bool `` // Pass --force to applicable helm commands
|
Force bool `` // Pass --force to applicable helm commands
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate reads environment variables into the Config, accounting for several possible formats.
|
// NewConfig creates a Config and reads environment variables into it, accounting for several possible formats.
|
||||||
func (cfg *Config) Populate() error {
|
func NewConfig() (*Config, error) {
|
||||||
if err := envconfig.Process("plugin", cfg); err != nil {
|
cfg := Config{}
|
||||||
return err
|
if err := envconfig.Process("plugin", &cfg); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix := cfg.Prefix
|
prefix := cfg.Prefix
|
||||||
|
|
||||||
if err := envconfig.Process("", cfg); err != nil {
|
if err := envconfig.Process("", &cfg); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if prefix != "" {
|
if prefix != "" {
|
||||||
if err := envconfig.Process(cfg.Prefix, cfg); err != nil {
|
if err := envconfig.Process(cfg.Prefix, &cfg); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ func TestConfigTestSuite(t *testing.T) {
|
||||||
suite.Run(t, new(ConfigTestSuite))
|
suite.Run(t, new(ConfigTestSuite))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ConfigTestSuite) TestPopulateWithPluginPrefix() {
|
func (suite *ConfigTestSuite) TestNewConfigWithPluginPrefix() {
|
||||||
suite.unsetenv("PLUGIN_PREFIX")
|
suite.unsetenv("PLUGIN_PREFIX")
|
||||||
suite.unsetenv("HELM_COMMAND")
|
suite.unsetenv("HELM_COMMAND")
|
||||||
suite.unsetenv("UPDATE_DEPENDENCIES")
|
suite.unsetenv("UPDATE_DEPENDENCIES")
|
||||||
|
@ -27,15 +27,15 @@ func (suite *ConfigTestSuite) TestPopulateWithPluginPrefix() {
|
||||||
suite.setenv("PLUGIN_UPDATE_DEPENDENCIES", "true")
|
suite.setenv("PLUGIN_UPDATE_DEPENDENCIES", "true")
|
||||||
suite.setenv("PLUGIN_DEBUG", "true")
|
suite.setenv("PLUGIN_DEBUG", "true")
|
||||||
|
|
||||||
cfg := Config{}
|
cfg, err := NewConfig()
|
||||||
suite.Require().NoError(cfg.Populate())
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
suite.Equal("execute order 66", cfg.Command)
|
suite.Equal("execute order 66", cfg.Command)
|
||||||
suite.True(cfg.UpdateDependencies)
|
suite.True(cfg.UpdateDependencies)
|
||||||
suite.True(cfg.Debug)
|
suite.True(cfg.Debug)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ConfigTestSuite) TestPopulateWithNoPrefix() {
|
func (suite *ConfigTestSuite) TestNewConfigWithNoPrefix() {
|
||||||
suite.unsetenv("PLUGIN_PREFIX")
|
suite.unsetenv("PLUGIN_PREFIX")
|
||||||
suite.unsetenv("PLUGIN_HELM_COMMAND")
|
suite.unsetenv("PLUGIN_HELM_COMMAND")
|
||||||
suite.unsetenv("PLUGIN_UPDATE_DEPENDENCIES")
|
suite.unsetenv("PLUGIN_UPDATE_DEPENDENCIES")
|
||||||
|
@ -45,23 +45,23 @@ func (suite *ConfigTestSuite) TestPopulateWithNoPrefix() {
|
||||||
suite.setenv("UPDATE_DEPENDENCIES", "true")
|
suite.setenv("UPDATE_DEPENDENCIES", "true")
|
||||||
suite.setenv("DEBUG", "true")
|
suite.setenv("DEBUG", "true")
|
||||||
|
|
||||||
cfg := Config{}
|
cfg, err := NewConfig()
|
||||||
suite.Require().NoError(cfg.Populate())
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
suite.Equal("execute order 66", cfg.Command)
|
suite.Equal("execute order 66", cfg.Command)
|
||||||
suite.True(cfg.UpdateDependencies)
|
suite.True(cfg.UpdateDependencies)
|
||||||
suite.True(cfg.Debug)
|
suite.True(cfg.Debug)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ConfigTestSuite) TestPopulateWithConfigurablePrefix() {
|
func (suite *ConfigTestSuite) TestNewConfigWithConfigurablePrefix() {
|
||||||
suite.unsetenv("API_SERVER")
|
suite.unsetenv("API_SERVER")
|
||||||
suite.unsetenv("PLUGIN_API_SERVER")
|
suite.unsetenv("PLUGIN_API_SERVER")
|
||||||
|
|
||||||
suite.setenv("PLUGIN_PREFIX", "prix_fixe")
|
suite.setenv("PLUGIN_PREFIX", "prix_fixe")
|
||||||
suite.setenv("PRIX_FIXE_API_SERVER", "your waiter this evening")
|
suite.setenv("PRIX_FIXE_API_SERVER", "your waiter this evening")
|
||||||
|
|
||||||
cfg := Config{}
|
cfg, err := NewConfig()
|
||||||
suite.Require().NoError(cfg.Populate())
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
suite.Equal("prix_fixe", cfg.Prefix)
|
suite.Equal("prix_fixe", cfg.Prefix)
|
||||||
suite.Equal("your waiter this evening", cfg.APIServer)
|
suite.Equal("your waiter this evening", cfg.APIServer)
|
||||||
|
@ -72,8 +72,8 @@ func (suite *ConfigTestSuite) TestPrefixSettingDoesNotAffectPluginPrefix() {
|
||||||
suite.setenv("PLUGIN_HELM_COMMAND", "wake me up")
|
suite.setenv("PLUGIN_HELM_COMMAND", "wake me up")
|
||||||
suite.setenv("IXFREP_PLUGIN_HELM_COMMAND", "send me to sleep inside")
|
suite.setenv("IXFREP_PLUGIN_HELM_COMMAND", "send me to sleep inside")
|
||||||
|
|
||||||
cfg := Config{}
|
cfg, err := NewConfig()
|
||||||
suite.Require().NoError(cfg.Populate())
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
suite.Equal("wake me up", cfg.Command)
|
suite.Equal("wake me up", cfg.Command)
|
||||||
}
|
}
|
||||||
|
@ -84,13 +84,13 @@ func (suite *ConfigTestSuite) TestPrefixSettingMustHavePluginPrefix() {
|
||||||
suite.setenv("HELM_COMMAND", "gimme more")
|
suite.setenv("HELM_COMMAND", "gimme more")
|
||||||
suite.setenv("REFPIX_HELM_COMMAND", "gimme less")
|
suite.setenv("REFPIX_HELM_COMMAND", "gimme less")
|
||||||
|
|
||||||
cfg := Config{}
|
cfg, err := NewConfig()
|
||||||
suite.Require().NoError(cfg.Populate())
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
suite.Equal("gimme more", cfg.Command)
|
suite.Equal("gimme more", cfg.Command)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ConfigTestSuite) TestPopulateWithConflictingVariables() {
|
func (suite *ConfigTestSuite) TestNewConfigWithConflictingVariables() {
|
||||||
suite.setenv("PLUGIN_HELM_COMMAND", "execute order 66")
|
suite.setenv("PLUGIN_HELM_COMMAND", "execute order 66")
|
||||||
suite.setenv("HELM_COMMAND", "defend the jedi") // values from the `environment` block override those from `settings`
|
suite.setenv("HELM_COMMAND", "defend the jedi") // values from the `environment` block override those from `settings`
|
||||||
|
|
||||||
|
@ -98,8 +98,8 @@ func (suite *ConfigTestSuite) TestPopulateWithConflictingVariables() {
|
||||||
suite.setenv("TIMEOUT", "5m0s")
|
suite.setenv("TIMEOUT", "5m0s")
|
||||||
suite.setenv("PROD_TIMEOUT", "2m30s") // values from prefixed env vars override those from non-prefixed ones
|
suite.setenv("PROD_TIMEOUT", "2m30s") // values from prefixed env vars override those from non-prefixed ones
|
||||||
|
|
||||||
cfg := Config{}
|
cfg, err := NewConfig()
|
||||||
suite.Require().NoError(cfg.Populate())
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
suite.Equal("defend the jedi", cfg.Command)
|
suite.Equal("defend the jedi", cfg.Command)
|
||||||
suite.Equal("2m30s", cfg.Timeout)
|
suite.Equal("2m30s", cfg.Timeout)
|
||||||
|
|
Loading…
Reference in a new issue