Remove the cfg argument from Step.Execute [#67]

This is the first step toward removing run.Config entirely. InitKube was
the only Step that even used cfg in its Execute function; the rest just
discarded it.
This commit is contained in:
Erin Call 2020-01-16 15:30:21 -08:00
parent 88bb8085b0
commit 231138563c
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
17 changed files with 43 additions and 35 deletions

View file

@ -48,15 +48,15 @@ func (mr *MockStepMockRecorder) Prepare(arg0 interface{}) *gomock.Call {
} }
// Execute mocks base method // Execute mocks base method
func (m *MockStep) Execute(arg0 run.Config) error { func (m *MockStep) Execute() error {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Execute", arg0) ret := m.ctrl.Call(m, "Execute")
ret0, _ := ret[0].(error) ret0, _ := ret[0].(error)
return ret0 return ret0
} }
// Execute indicates an expected call of Execute // Execute indicates an expected call of Execute
func (mr *MockStepMockRecorder) Execute(arg0 interface{}) *gomock.Call { func (mr *MockStepMockRecorder) Execute() *gomock.Call {
mr.mock.ctrl.T.Helper() mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Execute", reflect.TypeOf((*MockStep)(nil).Execute), arg0) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Execute", reflect.TypeOf((*MockStep)(nil).Execute))
} }

View file

@ -15,7 +15,7 @@ const (
// A Step is one step in the plan. // A Step is one step in the plan.
type Step interface { type Step interface {
Prepare(run.Config) error Prepare(run.Config) error
Execute(run.Config) error Execute() error
} }
// A Plan is a series of steps to perform. // A Plan is a series of steps to perform.
@ -84,7 +84,7 @@ func (p *Plan) Execute() error {
fmt.Fprintf(p.cfg.Stderr, "calling %T.Execute (step %d)\n", step, i) fmt.Fprintf(p.cfg.Stderr, "calling %T.Execute (step %d)\n", step, i)
} }
if err := step.Execute(p.runCfg); err != nil { if err := step.Execute(); err != nil {
return fmt.Errorf("while executing %T step: %w", step, err) return fmt.Errorf("while executing %T step: %w", step, err)
} }
} }

View file

@ -98,10 +98,10 @@ func (suite *PlanTestSuite) TestExecute() {
} }
stepOne.EXPECT(). stepOne.EXPECT().
Execute(runCfg). Execute().
Times(1) Times(1)
stepTwo.EXPECT(). stepTwo.EXPECT().
Execute(runCfg). Execute().
Times(1) Times(1)
suite.NoError(plan.Execute()) suite.NoError(plan.Execute())
@ -121,7 +121,7 @@ func (suite *PlanTestSuite) TestExecuteAbortsOnError() {
} }
stepOne.EXPECT(). stepOne.EXPECT().
Execute(runCfg). Execute().
Times(1). Times(1).
Return(fmt.Errorf("oh, he'll gnaw")) Return(fmt.Errorf("oh, he'll gnaw"))

View file

@ -19,7 +19,7 @@ func NewAddRepo(repo string) *AddRepo {
} }
// Execute executes the `helm repo add` command. // Execute executes the `helm repo add` command.
func (a *AddRepo) Execute(_ Config) error { func (a *AddRepo) Execute() error {
return a.cmd.Run() return a.cmd.Run()
} }

View file

@ -70,7 +70,7 @@ func (suite *AddRepoTestSuite) TestPrepareAndExecute() {
Run(). Run().
Times(1) Times(1)
suite.Require().NoError(a.Execute(cfg)) suite.Require().NoError(a.Execute())
} }

View file

@ -19,7 +19,7 @@ func NewDepUpdate(cfg env.Config) *DepUpdate {
} }
// Execute executes the `helm upgrade` command. // Execute executes the `helm upgrade` command.
func (d *DepUpdate) Execute(_ Config) error { func (d *DepUpdate) Execute() error {
return d.cmd.Run() return d.cmd.Run()
} }

View file

@ -69,7 +69,7 @@ func (suite *DepUpdateTestSuite) TestPrepareAndExecute() {
} }
suite.Require().NoError(d.Prepare(cfg)) suite.Require().NoError(d.Prepare(cfg))
suite.NoError(d.Execute(cfg)) suite.NoError(d.Execute())
} }
func (suite *DepUpdateTestSuite) TestPrepareNamespaceFlag() { func (suite *DepUpdateTestSuite) TestPrepareNamespaceFlag() {

View file

@ -19,7 +19,7 @@ func NewHelp(cfg env.Config) *Help {
} }
// Execute executes the `helm help` command. // Execute executes the `helm help` command.
func (h *Help) Execute(cfg Config) error { func (h *Help) Execute() error {
if err := h.cmd.Run(); err != nil { if err := h.cmd.Run(); err != nil {
return fmt.Errorf("while running '%s': %w", h.cmd.String(), err) return fmt.Errorf("while running '%s': %w", h.cmd.String(), err)
} }

View file

@ -73,15 +73,14 @@ func (suite *HelpTestSuite) TestExecute() {
Run(). Run().
Times(2) Times(2)
cfg := Config{}
help := Help{ help := Help{
HelmCommand: "help", HelmCommand: "help",
cmd: mCmd, cmd: mCmd,
} }
suite.NoError(help.Execute(cfg)) suite.NoError(help.Execute())
help.HelmCommand = "get down on friday" help.HelmCommand = "get down on friday"
suite.EqualError(help.Execute(cfg), "unknown command 'get down on friday'") suite.EqualError(help.Execute(), "unknown command 'get down on friday'")
} }
func (suite *HelpTestSuite) TestPrepareDebugFlag() { func (suite *HelpTestSuite) TestPrepareDebugFlag() {

View file

@ -13,6 +13,8 @@ import (
type InitKube struct { type InitKube struct {
templateFilename string templateFilename string
configFilename string configFilename string
debug bool
stderr io.Writer
template *template.Template template *template.Template
configFile io.WriteCloser configFile io.WriteCloser
values kubeValues values kubeValues
@ -40,13 +42,15 @@ func NewInitKube(cfg env.Config, templateFile, configFile string) *InitKube {
}, },
templateFilename: templateFile, templateFilename: templateFile,
configFilename: configFile, configFilename: configFile,
debug: cfg.Debug,
stderr: cfg.Stderr,
} }
} }
// Execute generates a kubernetes config file from drone-helm3's template. // Execute generates a kubernetes config file from drone-helm3's template.
func (i *InitKube) Execute(cfg Config) error { func (i *InitKube) Execute() error {
if cfg.Debug { if i.debug {
fmt.Fprintf(cfg.Stderr, "writing kubeconfig file to %s\n", i.configFilename) fmt.Fprintf(i.stderr, "writing kubeconfig file to %s\n", i.configFilename)
} }
defer i.configFile.Close() defer i.configFile.Close()
return i.template.Execute(i.configFile, i.values) return i.template.Execute(i.configFile, i.values)
@ -67,22 +71,22 @@ func (i *InitKube) Prepare(cfg Config) error {
i.values.ServiceAccount = "helm" i.values.ServiceAccount = "helm"
} }
if cfg.Debug { if i.debug {
fmt.Fprintf(cfg.Stderr, "loading kubeconfig template from %s\n", i.templateFilename) fmt.Fprintf(i.stderr, "loading kubeconfig template from %s\n", i.templateFilename)
} }
i.template, err = template.ParseFiles(i.templateFilename) i.template, err = template.ParseFiles(i.templateFilename)
if err != nil { if err != nil {
return fmt.Errorf("could not load kubeconfig template: %w", err) return fmt.Errorf("could not load kubeconfig template: %w", err)
} }
if cfg.Debug { if i.debug {
if _, err := os.Stat(i.configFilename); err != nil { if _, err := os.Stat(i.configFilename); err != nil {
// non-nil err here isn't an actual error state; the kubeconfig just doesn't exist // non-nil err here isn't an actual error state; the kubeconfig just doesn't exist
fmt.Fprint(cfg.Stderr, "creating ") fmt.Fprint(i.stderr, "creating ")
} else { } else {
fmt.Fprint(cfg.Stderr, "truncating ") fmt.Fprint(i.stderr, "truncating ")
} }
fmt.Fprintf(cfg.Stderr, "kubeconfig file at %s\n", i.configFilename) fmt.Fprintf(i.stderr, "kubeconfig file at %s\n", i.configFilename)
} }
i.configFile, err = os.Create(i.configFilename) i.configFile, err = os.Create(i.configFilename)

View file

@ -6,6 +6,7 @@ import (
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"testing" "testing"
"text/template" "text/template"
) )
@ -25,6 +26,8 @@ func (suite *InitKubeTestSuite) TestNewInitKube() {
APIServer: "98.765.43.21", APIServer: "98.765.43.21",
ServiceAccount: "greathelm", ServiceAccount: "greathelm",
KubeToken: "b2YgbXkgYWZmZWN0aW9u", KubeToken: "b2YgbXkgYWZmZWN0aW9u",
Stderr: &strings.Builder{},
Debug: true,
} }
init := NewInitKube(cfg, "conf.tpl", "conf.yml") init := NewInitKube(cfg, "conf.tpl", "conf.yml")
@ -38,6 +41,8 @@ func (suite *InitKubeTestSuite) TestNewInitKube() {
}, },
templateFilename: "conf.tpl", templateFilename: "conf.tpl",
configFilename: "conf.yml", configFilename: "conf.yml",
debug: true,
stderr: cfg.Stderr,
}, init) }, init)
} }
@ -70,7 +75,7 @@ namespace: {{ .Namespace }}
suite.IsType(&template.Template{}, init.template) suite.IsType(&template.Template{}, init.template)
suite.NotNil(init.configFile) suite.NotNil(init.configFile)
err = init.Execute(cfg) err = init.Execute()
suite.Require().Nil(err) suite.Require().Nil(err)
conf, err := ioutil.ReadFile(configFile.Name()) conf, err := ioutil.ReadFile(configFile.Name())
@ -101,7 +106,7 @@ func (suite *InitKubeTestSuite) TestExecuteGeneratesConfig() {
}, },
} }
suite.Require().NoError(init.Prepare(cfg)) suite.Require().NoError(init.Prepare(cfg))
suite.Require().NoError(init.Execute(cfg)) suite.Require().NoError(init.Execute())
contents, err := ioutil.ReadFile(configFile.Name()) contents, err := ioutil.ReadFile(configFile.Name())
suite.Require().NoError(err) suite.Require().NoError(err)
@ -128,7 +133,7 @@ func (suite *InitKubeTestSuite) TestExecuteGeneratesConfig() {
init.values.Certificate = "" init.values.Certificate = ""
suite.Require().NoError(init.Prepare(cfg)) suite.Require().NoError(init.Prepare(cfg))
suite.Require().NoError(init.Execute(cfg)) suite.Require().NoError(init.Execute())
contents, err = ioutil.ReadFile(configFile.Name()) contents, err = ioutil.ReadFile(configFile.Name())
suite.Require().NoError(err) suite.Require().NoError(err)
suite.Contains(string(contents), "insecure-skip-tls-verify: true") suite.Contains(string(contents), "insecure-skip-tls-verify: true")

View file

@ -27,7 +27,7 @@ func NewLint(cfg env.Config) *Lint {
} }
// Execute executes the `helm lint` command. // Execute executes the `helm lint` command.
func (l *Lint) Execute(_ Config) error { func (l *Lint) Execute() error {
return l.cmd.Run() return l.cmd.Run()
} }

View file

@ -82,7 +82,7 @@ func (suite *LintTestSuite) TestPrepareAndExecute() {
err := l.Prepare(cfg) err := l.Prepare(cfg)
suite.Require().Nil(err) suite.Require().Nil(err)
l.Execute(cfg) l.Execute()
} }
func (suite *LintTestSuite) TestPrepareRequiresChart() { func (suite *LintTestSuite) TestPrepareRequiresChart() {

View file

@ -23,7 +23,7 @@ func NewUninstall(cfg env.Config) *Uninstall {
} }
// Execute executes the `helm uninstall` command. // Execute executes the `helm uninstall` command.
func (u *Uninstall) Execute(_ Config) error { func (u *Uninstall) Execute() error {
return u.cmd.Run() return u.cmd.Run()
} }

View file

@ -78,7 +78,7 @@ func (suite *UninstallTestSuite) TestPrepareAndExecute() {
expected := []string{"uninstall", "zayde_wølf_king"} expected := []string{"uninstall", "zayde_wølf_king"}
suite.Equal(expected, actual) suite.Equal(expected, actual)
u.Execute(cfg) u.Execute()
} }
func (suite *UninstallTestSuite) TestPrepareDryRunFlag() { func (suite *UninstallTestSuite) TestPrepareDryRunFlag() {

View file

@ -45,7 +45,7 @@ func NewUpgrade(cfg env.Config) *Upgrade {
} }
// Execute executes the `helm upgrade` command. // Execute executes the `helm upgrade` command.
func (u *Upgrade) Execute(_ Config) error { func (u *Upgrade) Execute() error {
return u.cmd.Run() return u.cmd.Run()
} }

View file

@ -93,7 +93,7 @@ func (suite *UpgradeTestSuite) TestPrepareAndExecute() {
cfg := Config{} cfg := Config{}
err := u.Prepare(cfg) err := u.Prepare(cfg)
suite.Require().Nil(err) suite.Require().Nil(err)
u.Execute(cfg) u.Execute()
} }
func (suite *UpgradeTestSuite) TestPrepareNamespaceFlag() { func (suite *UpgradeTestSuite) TestPrepareNamespaceFlag() {