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:
parent
a21848484b
commit
79532e7635
|
@ -39,15 +39,7 @@ func (a *AddRepo) Prepare() error {
|
|||
name := split[0]
|
||||
url := split[1]
|
||||
|
||||
args := make([]string, 0)
|
||||
|
||||
if a.namespace != "" {
|
||||
args = append(args, "--namespace", a.namespace)
|
||||
}
|
||||
if a.debug {
|
||||
args = append(args, "--debug")
|
||||
}
|
||||
|
||||
args := a.globalFlags()
|
||||
args = append(args, "repo", "add", name, url)
|
||||
|
||||
a.cmd = command(helmBin, args...)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/pelotech/drone-helm3/internal/env"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
@ -97,42 +96,3 @@ func (suite *AddRepoTestSuite) TestPrepareWithEqualSignInURL() {
|
|||
suite.NoError(a.Prepare())
|
||||
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())
|
||||
}
|
||||
|
|
|
@ -20,3 +20,14 @@ func newConfig(cfg env.Config) *config {
|
|||
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
|
||||
}
|
||||
|
|
|
@ -33,3 +33,16 @@ func (suite *ConfigTestSuite) TestNewConfig() {
|
|||
stderr: stderr,
|
||||
}, 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)
|
||||
}
|
||||
|
|
|
@ -31,15 +31,7 @@ func (d *DepUpdate) Prepare() error {
|
|||
return fmt.Errorf("chart is required")
|
||||
}
|
||||
|
||||
args := make([]string, 0)
|
||||
|
||||
if d.namespace != "" {
|
||||
args = append(args, "--namespace", d.namespace)
|
||||
}
|
||||
if d.debug {
|
||||
args = append(args, "--debug")
|
||||
}
|
||||
|
||||
args := d.globalFlags()
|
||||
args = append(args, "dependency", "update", d.chart)
|
||||
|
||||
d.cmd = command(helmBin, args...)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/pelotech/drone-helm3/internal/env"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
@ -71,58 +70,6 @@ func (suite *DepUpdateTestSuite) TestPrepareAndExecute() {
|
|||
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() {
|
||||
d := NewDepUpdate(env.Config{})
|
||||
|
||||
|
|
|
@ -34,10 +34,8 @@ func (h *Help) Execute() error {
|
|||
|
||||
// Prepare gets the Help ready to execute.
|
||||
func (h *Help) Prepare() error {
|
||||
args := []string{"help"}
|
||||
if h.debug {
|
||||
args = append([]string{"--debug"}, args...)
|
||||
}
|
||||
args := h.globalFlags()
|
||||
args = append(args, "help")
|
||||
|
||||
h.cmd = command(helmBin, args...)
|
||||
h.cmd.Stdout(h.stdout)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/pelotech/drone-helm3/internal/env"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -75,20 +74,3 @@ func (suite *HelpTestSuite) TestExecute() {
|
|||
help.helmCommand = "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())
|
||||
}
|
||||
|
|
|
@ -39,15 +39,7 @@ func (l *Lint) Prepare() error {
|
|||
return fmt.Errorf("chart is required")
|
||||
}
|
||||
|
||||
args := make([]string, 0)
|
||||
|
||||
if l.namespace != "" {
|
||||
args = append(args, "--namespace", l.namespace)
|
||||
}
|
||||
if l.debug {
|
||||
args = append(args, "--debug")
|
||||
}
|
||||
|
||||
args := l.globalFlags()
|
||||
args = append(args, "lint")
|
||||
|
||||
if l.values != "" {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/pelotech/drone-helm3/internal/env"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
@ -126,58 +125,3 @@ func (suite *LintTestSuite) TestPrepareWithLintFlags() {
|
|||
err := l.Prepare()
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -35,15 +35,7 @@ func (u *Uninstall) Prepare() error {
|
|||
return fmt.Errorf("release is required")
|
||||
}
|
||||
|
||||
args := make([]string, 0)
|
||||
|
||||
if u.namespace != "" {
|
||||
args = append(args, "--namespace", u.namespace)
|
||||
}
|
||||
if u.debug {
|
||||
args = append(args, "--debug")
|
||||
}
|
||||
|
||||
args := u.globalFlags()
|
||||
args = append(args, "uninstall")
|
||||
|
||||
if u.dryRun {
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/pelotech/drone-helm3/internal/env"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -110,46 +108,6 @@ func (suite *UninstallTestSuite) TestPrepareKeepHistoryFlag() {
|
|||
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() {
|
||||
// These aren't really expected, but allowing them gives clearer test-failure messages
|
||||
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
||||
|
|
|
@ -60,15 +60,7 @@ func (u *Upgrade) Prepare() error {
|
|||
return fmt.Errorf("release is required")
|
||||
}
|
||||
|
||||
args := make([]string, 0)
|
||||
|
||||
if u.namespace != "" {
|
||||
args = append(args, "--namespace", u.namespace)
|
||||
}
|
||||
if u.debug {
|
||||
args = append(args, "--debug")
|
||||
}
|
||||
|
||||
args := u.globalFlags()
|
||||
args = append(args, "upgrade", "--install")
|
||||
|
||||
if u.chartVersion != "" {
|
||||
|
|
Loading…
Reference in a new issue