mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-18 16:21:44 +02:00
* fix #1095 Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
+3
-24
@@ -3,8 +3,6 @@ package argparser
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/helmfile/helmfile/pkg/state"
|
||||
)
|
||||
|
||||
type keyVal struct {
|
||||
@@ -16,9 +14,6 @@ type argMap struct {
|
||||
m map[string][]*keyVal
|
||||
flags []string
|
||||
}
|
||||
type GetArgsOptions struct {
|
||||
WithDiffArgs bool
|
||||
}
|
||||
|
||||
// isNewFlag checks if the given arg is a new flag
|
||||
func isNewFlag(flag string) bool {
|
||||
@@ -86,23 +81,10 @@ func analyzeArgs(am *argMap, args string) {
|
||||
}
|
||||
}
|
||||
|
||||
func GetArgs(args string, state *state.HelmState, opts *GetArgsOptions) []string {
|
||||
func CollectArgs(args string) []string {
|
||||
argsMap := newArgMap()
|
||||
|
||||
if len(args) > 0 {
|
||||
analyzeArgs(argsMap, args)
|
||||
}
|
||||
|
||||
if len(state.HelmDefaults.Args) > 0 {
|
||||
analyzeArgs(argsMap, strings.Join(state.HelmDefaults.Args, " "))
|
||||
}
|
||||
|
||||
if len(state.HelmDefaults.DiffArgs) > 0 && opts != nil && opts.WithDiffArgs {
|
||||
analyzeArgs(argsMap, strings.Join(state.HelmDefaults.DiffArgs, " "))
|
||||
}
|
||||
|
||||
analyzeArgs(argsMap, args)
|
||||
var argArr []string
|
||||
|
||||
for _, flag := range argsMap.flags {
|
||||
val := argsMap.m[flag]
|
||||
|
||||
@@ -118,8 +100,5 @@ func GetArgs(args string, state *state.HelmState, opts *GetArgsOptions) []string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state.HelmDefaults.Args = argArr
|
||||
|
||||
return state.HelmDefaults.Args
|
||||
return argArr
|
||||
}
|
||||
|
||||
@@ -1,61 +1,11 @@
|
||||
package argparser
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/helmfile/helmfile/pkg/state"
|
||||
)
|
||||
|
||||
// TestGetArgs tests the GetArgs function
|
||||
func TestGetArgs(t *testing.T) {
|
||||
tests := []struct {
|
||||
args string
|
||||
expected string
|
||||
defaultArgs []string
|
||||
defaultDiffArgs []string
|
||||
opts *GetArgsOptions
|
||||
}{
|
||||
{
|
||||
args: "-f a.yaml -f b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false",
|
||||
defaultArgs: []string{"--recreate-pods", "--force"},
|
||||
defaultDiffArgs: []string{"--suppress", "Deployment"},
|
||||
opts: &GetArgsOptions{WithDiffArgs: true},
|
||||
expected: "-f a.yaml -f b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false --recreate-pods --force --suppress Deployment",
|
||||
},
|
||||
{
|
||||
args: "-e a.yaml -d b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false",
|
||||
defaultArgs: []string{"-q www", "-w"},
|
||||
defaultDiffArgs: []string{},
|
||||
expected: "-e a.yaml -d b.yaml -i --set app1.bootstrap=true --set app2.bootstrap=false -q www -w",
|
||||
},
|
||||
{
|
||||
args: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false",
|
||||
expected: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false",
|
||||
},
|
||||
{
|
||||
args: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true",
|
||||
defaultArgs: []string{"--recreate-pods", "--force"},
|
||||
defaultDiffArgs: []string{"--suppress", "Deployment"},
|
||||
opts: &GetArgsOptions{},
|
||||
expected: "--timeout=3600 --set app1.bootstrap=true --set app2.bootstrap=false,app3.bootstrap=true --recreate-pods --force",
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
Helmdefaults := state.HelmSpec{KubeContext: "test", Args: test.defaultArgs, DiffArgs: test.defaultDiffArgs}
|
||||
testState := &state.HelmState{
|
||||
ReleaseSetSpec: state.ReleaseSetSpec{
|
||||
HelmDefaults: Helmdefaults,
|
||||
},
|
||||
}
|
||||
receivedArgs := GetArgs(test.args, testState, test.opts)
|
||||
|
||||
require.Equalf(t, test.expected, strings.Join(receivedArgs, " "), "expected args %s, received args %s", test.expected, strings.Join(receivedArgs, " "))
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsNewFlag tests the isNewFlag function
|
||||
func TestIsNewFlag(t *testing.T) {
|
||||
tests := []struct {
|
||||
|
||||
Reference in New Issue
Block a user