Extract the debug/namespace flags into run.Config [#67]

This is a general-purpose cleanup commit; every step except InitKube had
the same six "add the --debug and --namespace flags if applicable" code.
This commit is contained in:
Erin Call 2020-01-17 11:12:53 -08:00
parent a21848484b
commit 79532e7635
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
13 changed files with 31 additions and 258 deletions

View file

@ -39,15 +39,7 @@ func (a *AddRepo) Prepare() error {
name := split[0] name := split[0]
url := split[1] url := split[1]
args := make([]string, 0) args := a.globalFlags()
if a.namespace != "" {
args = append(args, "--namespace", a.namespace)
}
if a.debug {
args = append(args, "--debug")
}
args = append(args, "repo", "add", name, url) args = append(args, "repo", "add", name, url)
a.cmd = command(helmBin, args...) a.cmd = command(helmBin, args...)

View file

@ -1,7 +1,6 @@
package run package run
import ( import (
"fmt"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/pelotech/drone-helm3/internal/env" "github.com/pelotech/drone-helm3/internal/env"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -97,42 +96,3 @@ func (suite *AddRepoTestSuite) TestPrepareWithEqualSignInURL() {
suite.NoError(a.Prepare()) suite.NoError(a.Prepare())
suite.Contains(suite.commandArgs, "https://github.com/arthur_claypool/samaritan?version=2.1") suite.Contains(suite.commandArgs, "https://github.com/arthur_claypool/samaritan?version=2.1")
} }
func (suite *AddRepoTestSuite) TestNamespaceFlag() {
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
cfg := env.Config{
Namespace: "alliteration",
}
a := NewAddRepo(cfg, "edeath=https://github.com/theater_guy/e-death")
suite.NoError(a.Prepare())
suite.Equal(suite.commandPath, helmBin)
suite.Equal(suite.commandArgs, []string{"--namespace", "alliteration",
"repo", "add", "edeath", "https://github.com/theater_guy/e-death"})
}
func (suite *AddRepoTestSuite) TestDebugFlag() {
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
stderr := strings.Builder{}
command = func(path string, args ...string) cmd {
suite.mockCmd.EXPECT().
String().
Return(fmt.Sprintf("%s %s", path, strings.Join(args, " ")))
return suite.mockCmd
}
cfg := env.Config{
Debug: true,
Stderr: &stderr,
}
a := NewAddRepo(cfg, "edeath=https://github.com/the_bug/e-death")
suite.Require().NoError(a.Prepare())
suite.Equal(fmt.Sprintf("Generated command: '%s --debug "+
"repo add edeath https://github.com/the_bug/e-death'\n", helmBin), stderr.String())
}

View file

@ -20,3 +20,14 @@ func newConfig(cfg env.Config) *config {
stderr: cfg.Stderr, stderr: cfg.Stderr,
} }
} }
func (cfg *config) globalFlags() []string {
flags := []string{}
if cfg.debug {
flags = append(flags, "--debug")
}
if cfg.namespace != "" {
flags = append(flags, "--namespace", cfg.namespace)
}
return flags
}

View file

@ -33,3 +33,16 @@ func (suite *ConfigTestSuite) TestNewConfig() {
stderr: stderr, stderr: stderr,
}, cfg) }, cfg)
} }
func (suite *ConfigTestSuite) TestGlobalFlags() {
cfg := config{
debug: true,
namespace: "public",
}
flags := cfg.globalFlags()
suite.Equal([]string{"--debug", "--namespace", "public"}, flags)
cfg = config{}
flags = cfg.globalFlags()
suite.Equal([]string{}, flags)
}

View file

@ -31,15 +31,7 @@ func (d *DepUpdate) Prepare() error {
return fmt.Errorf("chart is required") return fmt.Errorf("chart is required")
} }
args := make([]string, 0) args := d.globalFlags()
if d.namespace != "" {
args = append(args, "--namespace", d.namespace)
}
if d.debug {
args = append(args, "--debug")
}
args = append(args, "dependency", "update", d.chart) args = append(args, "dependency", "update", d.chart)
d.cmd = command(helmBin, args...) d.cmd = command(helmBin, args...)

View file

@ -1,7 +1,6 @@
package run package run
import ( import (
"fmt"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/pelotech/drone-helm3/internal/env" "github.com/pelotech/drone-helm3/internal/env"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -71,58 +70,6 @@ func (suite *DepUpdateTestSuite) TestPrepareAndExecute() {
suite.NoError(d.Execute()) suite.NoError(d.Execute())
} }
func (suite *DepUpdateTestSuite) TestPrepareNamespaceFlag() {
defer suite.ctrl.Finish()
cfg := env.Config{
Namespace: "spotify",
Chart: "your_top_songs_2019",
}
command = func(path string, args ...string) cmd {
suite.Equal([]string{"--namespace", "spotify", "dependency", "update", "your_top_songs_2019"}, args)
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
d := NewDepUpdate(cfg)
suite.Require().NoError(d.Prepare())
}
func (suite *DepUpdateTestSuite) TestPrepareDebugFlag() {
defer suite.ctrl.Finish()
stdout := strings.Builder{}
stderr := strings.Builder{}
cfg := env.Config{
Chart: "your_top_songs_2019",
Debug: true,
Stdout: &stdout,
Stderr: &stderr,
}
command = func(path string, args ...string) cmd {
suite.mockCmd.EXPECT().
String().
Return(fmt.Sprintf("%s %s", path, strings.Join(args, " ")))
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
d := NewDepUpdate(cfg)
suite.Require().NoError(d.Prepare())
want := fmt.Sprintf("Generated command: '%s --debug dependency update your_top_songs_2019'\n", helmBin)
suite.Equal(want, stderr.String())
suite.Equal("", stdout.String())
}
func (suite *DepUpdateTestSuite) TestPrepareChartRequired() { func (suite *DepUpdateTestSuite) TestPrepareChartRequired() {
d := NewDepUpdate(env.Config{}) d := NewDepUpdate(env.Config{})

View file

@ -34,10 +34,8 @@ func (h *Help) Execute() error {
// Prepare gets the Help ready to execute. // Prepare gets the Help ready to execute.
func (h *Help) Prepare() error { func (h *Help) Prepare() error {
args := []string{"help"} args := h.globalFlags()
if h.debug { args = append(args, "help")
args = append([]string{"--debug"}, args...)
}
h.cmd = command(helmBin, args...) h.cmd = command(helmBin, args...)
h.cmd.Stdout(h.stdout) h.cmd.Stdout(h.stdout)

View file

@ -1,7 +1,6 @@
package run package run
import ( import (
"fmt"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/pelotech/drone-helm3/internal/env" "github.com/pelotech/drone-helm3/internal/env"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -75,20 +74,3 @@ func (suite *HelpTestSuite) TestExecute() {
help.helmCommand = "get down on friday" help.helmCommand = "get down on friday"
suite.EqualError(help.Execute(), "unknown command 'get down on friday'") suite.EqualError(help.Execute(), "unknown command 'get down on friday'")
} }
func (suite *HelpTestSuite) TestPrepareDebugFlag() {
stdout := strings.Builder{}
stderr := strings.Builder{}
cfg := env.Config{
Debug: true,
Stdout: &stdout,
Stderr: &stderr,
}
help := NewHelp(cfg)
help.Prepare()
want := fmt.Sprintf("Generated command: '%s --debug help'\n", helmBin)
suite.Equal(want, stderr.String())
suite.Equal("", stdout.String())
}

View file

@ -39,15 +39,7 @@ func (l *Lint) Prepare() error {
return fmt.Errorf("chart is required") return fmt.Errorf("chart is required")
} }
args := make([]string, 0) args := l.globalFlags()
if l.namespace != "" {
args = append(args, "--namespace", l.namespace)
}
if l.debug {
args = append(args, "--debug")
}
args = append(args, "lint") args = append(args, "lint")
if l.values != "" { if l.values != "" {

View file

@ -1,7 +1,6 @@
package run package run
import ( import (
"fmt"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/pelotech/drone-helm3/internal/env" "github.com/pelotech/drone-helm3/internal/env"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -126,58 +125,3 @@ func (suite *LintTestSuite) TestPrepareWithLintFlags() {
err := l.Prepare() err := l.Prepare()
suite.Require().Nil(err) suite.Require().Nil(err)
} }
func (suite *LintTestSuite) TestPrepareWithDebugFlag() {
defer suite.ctrl.Finish()
stderr := strings.Builder{}
cfg := env.Config{
Debug: true,
Stderr: &stderr,
Chart: "./scotland/top_40",
}
l := NewLint(cfg)
command = func(path string, args ...string) cmd {
suite.mockCmd.EXPECT().
String().
Return(fmt.Sprintf("%s %s", path, strings.Join(args, " ")))
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any())
suite.mockCmd.EXPECT().Stderr(&stderr)
err := l.Prepare()
suite.Require().Nil(err)
want := fmt.Sprintf("Generated command: '%s --debug lint ./scotland/top_40'\n", helmBin)
suite.Equal(want, stderr.String())
}
func (suite *LintTestSuite) TestPrepareWithNamespaceFlag() {
defer suite.ctrl.Finish()
cfg := env.Config{
Namespace: "table-service",
Chart: "./wales/top_40",
}
l := NewLint(cfg)
actual := []string{}
command = func(path string, args ...string) cmd {
actual = args
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
err := l.Prepare()
suite.Require().Nil(err)
expected := []string{"--namespace", "table-service", "lint", "./wales/top_40"}
suite.Equal(expected, actual)
}

View file

@ -35,15 +35,7 @@ func (u *Uninstall) Prepare() error {
return fmt.Errorf("release is required") return fmt.Errorf("release is required")
} }
args := make([]string, 0) args := u.globalFlags()
if u.namespace != "" {
args = append(args, "--namespace", u.namespace)
}
if u.debug {
args = append(args, "--debug")
}
args = append(args, "uninstall") args = append(args, "uninstall")
if u.dryRun { if u.dryRun {

View file

@ -1,11 +1,9 @@
package run package run
import ( import (
"fmt"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/pelotech/drone-helm3/internal/env" "github.com/pelotech/drone-helm3/internal/env"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"strings"
"testing" "testing"
) )
@ -110,46 +108,6 @@ func (suite *UninstallTestSuite) TestPrepareKeepHistoryFlag() {
suite.Equal(expected, suite.actualArgs) suite.Equal(expected, suite.actualArgs)
} }
func (suite *UninstallTestSuite) TestPrepareNamespaceFlag() {
cfg := env.Config{
Release: "carly_simon_run_away_with_me",
Namespace: "emotion",
}
u := NewUninstall(cfg)
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
suite.NoError(u.Prepare())
expected := []string{"--namespace", "emotion", "uninstall", "carly_simon_run_away_with_me"}
suite.Equal(expected, suite.actualArgs)
}
func (suite *UninstallTestSuite) TestPrepareDebugFlag() {
stderr := strings.Builder{}
cfg := env.Config{
Release: "just_a_band_huff_and_puff",
Debug: true,
Stderr: &stderr,
}
u := NewUninstall(cfg)
command = func(path string, args ...string) cmd {
suite.mockCmd.EXPECT().
String().
Return(fmt.Sprintf("%s %s", path, strings.Join(args, " ")))
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(&stderr).AnyTimes()
suite.NoError(u.Prepare())
suite.Equal(fmt.Sprintf("Generated command: '%s --debug "+
"uninstall just_a_band_huff_and_puff'\n", helmBin), stderr.String())
}
func (suite *UninstallTestSuite) TestPrepareRequiresRelease() { func (suite *UninstallTestSuite) TestPrepareRequiresRelease() {
// These aren't really expected, but allowing them gives clearer test-failure messages // These aren't really expected, but allowing them gives clearer test-failure messages
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes() suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()

View file

@ -60,15 +60,7 @@ func (u *Upgrade) Prepare() error {
return fmt.Errorf("release is required") return fmt.Errorf("release is required")
} }
args := make([]string, 0) args := u.globalFlags()
if u.namespace != "" {
args = append(args, "--namespace", u.namespace)
}
if u.debug {
args = append(args, "--debug")
}
args = append(args, "upgrade", "--install") args = append(args, "upgrade", "--install")
if u.chartVersion != "" { if u.chartVersion != "" {