feat: add/expose cli flags (#771)

* feat: add/expose cli flags

Signed-off-by: Hans Song <hans.m.song@gmail.com>

* fix tests

Signed-off-by: Hans Song <hans.m.song@gmail.com>

* remove skipdeps from subcommand options

Signed-off-by: Hans Song <hans.m.song@gmail.com>

* remove skip-deps from subcommand flags

Signed-off-by: Hans Song <hans.m.song@gmail.com>

* remove SkipDeps from subcommand implementations

Signed-off-by: Hans Song <hans.m.song@gmail.com>

* update doco with new flags

Signed-off-by: Hans Song <hans.m.song@gmail.com>

---------

Signed-off-by: Hans Song <hans.m.song@gmail.com>
This commit is contained in:
Hans Song
2023-04-02 14:53:52 +08:00
committed by GitHub
parent fe1eba60db
commit 1d0ba72b47
32 changed files with 98 additions and 103 deletions
+15 -6
View File
@@ -28,9 +28,14 @@ type decryptedSecret struct {
err error
}
type HelmExecOptions struct {
EnableLiveOutput bool
DisableForceUpdate bool
}
type execer struct {
helmBinary string
enableLiveOutput bool
options HelmExecOptions
version *semver.Version
runner Runner
logger *zap.SugaredLogger
@@ -110,7 +115,7 @@ func redactedURL(chart string) string {
}
// New for running helm commands
func New(helmBinary string, enableLiveOutput bool, logger *zap.SugaredLogger, kubeContext string, runner Runner) *execer {
func New(helmBinary string, options HelmExecOptions, logger *zap.SugaredLogger, kubeContext string, runner Runner) *execer {
// TODO: proper error handling
version, err := GetHelmVersion(helmBinary, runner)
if err != nil {
@@ -118,7 +123,7 @@ func New(helmBinary string, enableLiveOutput bool, logger *zap.SugaredLogger, ku
}
return &execer{
helmBinary: helmBinary,
enableLiveOutput: enableLiveOutput,
options: options,
version: version,
logger: logger,
kubeContext: kubeContext,
@@ -136,7 +141,11 @@ func (helm *execer) SetHelmBinary(bin string) {
}
func (helm *execer) SetEnableLiveOutput(enableLiveOutput bool) {
helm.enableLiveOutput = enableLiveOutput
helm.options.EnableLiveOutput = enableLiveOutput
}
func (helm *execer) SetDisableForceUpdate(forceUpdate bool) {
helm.options.DisableForceUpdate = forceUpdate
}
func (helm *execer) AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify string) error {
@@ -156,7 +165,7 @@ func (helm *execer) AddRepo(name, repository, cafile, certfile, keyfile, usernam
// See https://github.com/helm/helm/pull/8777
if cons, err := semver.NewConstraint(">= 3.3.2"); err == nil {
if cons.Check(helm.version) {
if !helm.options.DisableForceUpdate && cons.Check(helm.version) {
args = append(args, "--force-update")
}
} else {
@@ -525,7 +534,7 @@ func (helm *execer) exec(args []string, env map[string]string, overrideEnableLiv
}
cmd := fmt.Sprintf("exec: %s %s", helm.helmBinary, strings.Join(cmdargs, " "))
helm.logger.Debug(cmd)
enableLiveOutput := helm.enableLiveOutput
enableLiveOutput := helm.options.EnableLiveOutput
if overrideEnableLiveOutput != nil {
enableLiveOutput = *overrideEnableLiveOutput
}
+47 -12
View File
@@ -36,7 +36,7 @@ func (mock *mockRunner) Execute(cmd string, args []string, env map[string]string
}
func MockExecer(logger *zap.SugaredLogger, kubeContext string) *execer {
execer := New("helm", false, logger, kubeContext, &mockRunner{})
execer := New("helm", HelmExecOptions{}, logger, kubeContext, &mockRunner{})
return execer
}
@@ -85,12 +85,23 @@ func Test_SetHelmBinary(t *testing.T) {
func Test_SetEnableLiveOutput(t *testing.T) {
helm := MockExecer(NewLogger(os.Stdout, "info"), "dev")
if helm.enableLiveOutput {
t.Error("helmexec.enableLiveOutput should not be enabled by default")
if helm.options.EnableLiveOutput {
t.Error("helmexec.options.EnableLiveOutput should not be enabled by default")
}
helm.SetEnableLiveOutput(true)
if !helm.enableLiveOutput {
t.Errorf("helmexec.SetEnableLiveOutput() - actual = %t expect = true", helm.enableLiveOutput)
if !helm.options.EnableLiveOutput {
t.Errorf("helmexec.SetEnableLiveOutput() - actual = %t expect = true", helm.options.EnableLiveOutput)
}
}
func Test_SetDisableForceUpdate(t *testing.T) {
helm := MockExecer(NewLogger(os.Stdout, "info"), "dev")
if helm.options.DisableForceUpdate {
t.Error("helmexec.options.ForceUpdate should not be enabled by default")
}
helm.SetDisableForceUpdate(true)
if !helm.options.DisableForceUpdate {
t.Errorf("helmexec.SetDisableForceUpdate() - actual = %t expect = true", helm.options.DisableForceUpdate)
}
}
@@ -117,6 +128,30 @@ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --force-
}
}
func Test_AddRepo_Helm_3_3_2_NoForceUpdate(t *testing.T) {
var buffer bytes.Buffer
logger := NewLogger(&buffer, "debug")
helm := &execer{
helmBinary: "helm",
options: HelmExecOptions{DisableForceUpdate: true},
version: semver.MustParse("3.3.2"),
logger: logger,
kubeContext: "dev",
runner: &mockRunner{},
}
err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "cert.pem", "key.pem", "", "", "", "", "")
expected := `Adding repo myRepo https://repo.example.com/
exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --cert-file cert.pem --key-file key.pem
`
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if buffer.String() != expected {
t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
}
}
func Test_AddRepo(t *testing.T) {
var buffer bytes.Buffer
logger := NewLogger(&buffer, "debug")
@@ -322,7 +357,7 @@ func Test_BuildDeps(t *testing.T) {
var buffer bytes.Buffer
logger := NewLogger(&buffer, "debug")
helm3Runner := mockRunner{output: []byte("v3.2.4+ge29ce2a")}
helm := New("helm", false, logger, "dev", &helm3Runner)
helm := New("helm", HelmExecOptions{}, logger, "dev", &helm3Runner)
err := helm.BuildDeps("foo", "./chart/foo", []string{"--skip-refresh"}...)
expected := `Building dependency release=foo, chart=./chart/foo
exec: helm --kube-context dev dependency build ./chart/foo --skip-refresh
@@ -364,7 +399,7 @@ v3.2.4+ge29ce2a
buffer.Reset()
helm2Runner := mockRunner{output: []byte("Client: v2.16.1+ge13bc94")}
helm = New("helm", false, logger, "dev", &helm2Runner)
helm = New("helm", HelmExecOptions{}, logger, "dev", &helm2Runner)
err = helm.BuildDeps("foo", "./chart/foo")
expected = `Building dependency release=foo, chart=./chart/foo
exec: helm --kube-context dev dependency build ./chart/foo
@@ -870,13 +905,13 @@ exec: helm --kube-context dev template release https://example_user:example_pass
func Test_IsHelm3(t *testing.T) {
helm2Runner := mockRunner{output: []byte("Client: v2.16.0+ge13bc94\n")}
helm := New("helm", false, NewLogger(os.Stdout, "info"), "dev", &helm2Runner)
helm := New("helm", HelmExecOptions{}, NewLogger(os.Stdout, "info"), "dev", &helm2Runner)
if helm.IsHelm3() {
t.Error("helmexec.IsHelm3() - Detected Helm 3 with Helm 2 version")
}
helm3Runner := mockRunner{output: []byte("v3.0.0+ge29ce2a\n")}
helm = New("helm", false, NewLogger(os.Stdout, "info"), "dev", &helm3Runner)
helm = New("helm", HelmExecOptions{}, NewLogger(os.Stdout, "info"), "dev", &helm3Runner)
if !helm.IsHelm3() {
t.Error("helmexec.IsHelm3() - Failed to detect Helm 3")
}
@@ -907,14 +942,14 @@ func Test_GetPluginVersion(t *testing.T) {
func Test_GetVersion(t *testing.T) {
helm2Runner := mockRunner{output: []byte("Client: v2.16.1+ge13bc94\n")}
helm := New("helm", false, NewLogger(os.Stdout, "info"), "dev", &helm2Runner)
helm := New("helm", HelmExecOptions{}, NewLogger(os.Stdout, "info"), "dev", &helm2Runner)
ver := helm.GetVersion()
if ver.Major != 2 || ver.Minor != 16 || ver.Patch != 1 {
t.Errorf("helmexec.GetVersion - did not detect correct Helm2 version; it was: %+v", ver)
}
helm3Runner := mockRunner{output: []byte("v3.2.4+ge29ce2a\n")}
helm = New("helm", false, NewLogger(os.Stdout, "info"), "dev", &helm3Runner)
helm = New("helm", HelmExecOptions{}, NewLogger(os.Stdout, "info"), "dev", &helm3Runner)
ver = helm.GetVersion()
if ver.Major != 3 || ver.Minor != 2 || ver.Patch != 4 {
t.Errorf("helmexec.GetVersion - did not detect correct Helm3 version; it was: %+v", ver)
@@ -923,7 +958,7 @@ func Test_GetVersion(t *testing.T) {
func Test_IsVersionAtLeast(t *testing.T) {
helm2Runner := mockRunner{output: []byte("Client: v2.16.1+ge13bc94\n")}
helm := New("helm", false, NewLogger(os.Stdout, "info"), "dev", &helm2Runner)
helm := New("helm", HelmExecOptions{}, NewLogger(os.Stdout, "info"), "dev", &helm2Runner)
if !helm.IsVersionAtLeast("2.1.0") {
t.Error("helmexec.IsVersionAtLeast - 2.16.1 not atleast 2.1")
}
+1
View File
@@ -14,6 +14,7 @@ type Interface interface {
SetExtraArgs(args ...string)
SetHelmBinary(bin string)
SetEnableLiveOutput(enableLiveOutput bool)
SetDisableForceUpdate(forceUpdate bool)
AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify string) error
UpdateRepo() error