diff --git a/cmd/drone-helm/main.go b/cmd/drone-helm/main.go index e42cb1e..a6a4d09 100644 --- a/cmd/drone-helm/main.go +++ b/cmd/drone-helm/main.go @@ -8,15 +8,15 @@ import ( ) 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()) return } // Make the plan - plan, err := helm.NewPlan(c) + plan, err := helm.NewPlan(*cfg) if err != nil { fmt.Fprintf(os.Stderr, "%w\n", err) os.Exit(1) diff --git a/internal/helm/config.go b/internal/helm/config.go index 9e88dab..54d0d42 100644 --- a/internal/helm/config.go +++ b/internal/helm/config.go @@ -35,23 +35,24 @@ type Config struct { Force bool `` // Pass --force to applicable helm commands } -// Populate reads environment variables into the Config, accounting for several possible formats. -func (cfg *Config) Populate() error { - if err := envconfig.Process("plugin", cfg); err != nil { - return err +// NewConfig creates a Config and reads environment variables into it, accounting for several possible formats. +func NewConfig() (*Config, error) { + cfg := Config{} + if err := envconfig.Process("plugin", &cfg); err != nil { + return nil, err } prefix := cfg.Prefix - if err := envconfig.Process("", cfg); err != nil { - return err + if err := envconfig.Process("", &cfg); err != nil { + return nil, err } if prefix != "" { - if err := envconfig.Process(cfg.Prefix, cfg); err != nil { - return err + if err := envconfig.Process(cfg.Prefix, &cfg); err != nil { + return nil, err } } - return nil + return &cfg, nil } diff --git a/internal/helm/config_test.go b/internal/helm/config_test.go index db556e8..88d1933 100644 --- a/internal/helm/config_test.go +++ b/internal/helm/config_test.go @@ -17,7 +17,7 @@ func TestConfigTestSuite(t *testing.T) { suite.Run(t, new(ConfigTestSuite)) } -func (suite *ConfigTestSuite) TestPopulateWithPluginPrefix() { +func (suite *ConfigTestSuite) TestNewConfigWithPluginPrefix() { suite.unsetenv("PLUGIN_PREFIX") suite.unsetenv("HELM_COMMAND") suite.unsetenv("UPDATE_DEPENDENCIES") @@ -27,15 +27,15 @@ func (suite *ConfigTestSuite) TestPopulateWithPluginPrefix() { suite.setenv("PLUGIN_UPDATE_DEPENDENCIES", "true") suite.setenv("PLUGIN_DEBUG", "true") - cfg := Config{} - suite.Require().NoError(cfg.Populate()) + cfg, err := NewConfig() + suite.Require().NoError(err) suite.Equal("execute order 66", cfg.Command) suite.True(cfg.UpdateDependencies) suite.True(cfg.Debug) } -func (suite *ConfigTestSuite) TestPopulateWithNoPrefix() { +func (suite *ConfigTestSuite) TestNewConfigWithNoPrefix() { suite.unsetenv("PLUGIN_PREFIX") suite.unsetenv("PLUGIN_HELM_COMMAND") suite.unsetenv("PLUGIN_UPDATE_DEPENDENCIES") @@ -45,23 +45,23 @@ func (suite *ConfigTestSuite) TestPopulateWithNoPrefix() { suite.setenv("UPDATE_DEPENDENCIES", "true") suite.setenv("DEBUG", "true") - cfg := Config{} - suite.Require().NoError(cfg.Populate()) + cfg, err := NewConfig() + suite.Require().NoError(err) suite.Equal("execute order 66", cfg.Command) suite.True(cfg.UpdateDependencies) suite.True(cfg.Debug) } -func (suite *ConfigTestSuite) TestPopulateWithConfigurablePrefix() { +func (suite *ConfigTestSuite) TestNewConfigWithConfigurablePrefix() { 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()) + cfg, err := NewConfig() + suite.Require().NoError(err) suite.Equal("prix_fixe", cfg.Prefix) 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("IXFREP_PLUGIN_HELM_COMMAND", "send me to sleep inside") - cfg := Config{} - suite.Require().NoError(cfg.Populate()) + cfg, err := NewConfig() + suite.Require().NoError(err) suite.Equal("wake me up", cfg.Command) } @@ -84,13 +84,13 @@ func (suite *ConfigTestSuite) TestPrefixSettingMustHavePluginPrefix() { suite.setenv("HELM_COMMAND", "gimme more") suite.setenv("REFPIX_HELM_COMMAND", "gimme less") - cfg := Config{} - suite.Require().NoError(cfg.Populate()) + cfg, err := NewConfig() + suite.Require().NoError(err) suite.Equal("gimme more", cfg.Command) } -func (suite *ConfigTestSuite) TestPopulateWithConflictingVariables() { +func (suite *ConfigTestSuite) TestNewConfigWithConflictingVariables() { suite.setenv("PLUGIN_HELM_COMMAND", "execute order 66") 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("PROD_TIMEOUT", "2m30s") // values from prefixed env vars override those from non-prefixed ones - cfg := Config{} - suite.Require().NoError(cfg.Populate()) + cfg, err := NewConfig() + suite.Require().NoError(err) suite.Equal("defend the jedi", cfg.Command) suite.Equal("2m30s", cfg.Timeout)