From 179f3ead2f0d6882e2d0721a274befd07d2b14fc Mon Sep 17 00:00:00 2001 From: "alexey.zh" Date: Mon, 30 Dec 2024 14:59:35 +0500 Subject: [PATCH] cmdflags.go --- pkg/util/cmdflags.go | 46 ++++++++++++++++ pkg/util/cmdflags_test.go | 111 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 pkg/util/cmdflags.go create mode 100644 pkg/util/cmdflags_test.go diff --git a/pkg/util/cmdflags.go b/pkg/util/cmdflags.go new file mode 100644 index 0000000..e8eeb21 --- /dev/null +++ b/pkg/util/cmdflags.go @@ -0,0 +1,46 @@ +package util + +import ( + "fmt" +) + +// CmdFlagsProxy holds interested for plugin args +type CmdFlagsProxy struct { + Filenames []string + Namespace string + Others []string +} + +func ParseCmdFlags(args []string) (*CmdFlagsProxy, error) { + res := &CmdFlagsProxy{ + Filenames: []string{}, + Namespace: "default", + Others: []string{}, + } + + for i := 0; i < len(args); i++ { + switch args[i] { + case "--filename", "-f": + if i+1 < len(args) { + res.Filenames = append(res.Filenames, args[i+1]) + i++ + } else { + return nil, fmt.Errorf("flag --filename requires a value") + } + + case "--namespace", "-n": + if i+1 < len(args) { + res.Namespace = args[i+1] + i++ + } else { + return nil, fmt.Errorf("flag --namespace requires a value") + } + + default: + res.Others = append(res.Others, args[i]) + } + + } + + return res, nil +} diff --git a/pkg/util/cmdflags_test.go b/pkg/util/cmdflags_test.go new file mode 100644 index 0000000..ec49f25 --- /dev/null +++ b/pkg/util/cmdflags_test.go @@ -0,0 +1,111 @@ +package util + +import ( + "reflect" + "testing" +) + +func TestParseCmdFlags(t *testing.T) { + tests := []struct { + name string + args []string + want *CmdFlagsProxy + wantErr bool + }{ + { + name: "No arguments, default namespace", + args: []string{}, + want: &CmdFlagsProxy{ + Filenames: []string{}, + Namespace: "default", + Others: []string{}, + }, + wantErr: false, + }, + { + name: "Single filename and namespace", + args: []string{"--filename", "file1.yaml", "--namespace", "test-namespace"}, + want: &CmdFlagsProxy{ + Filenames: []string{"file1.yaml"}, + Namespace: "test-namespace", + Others: []string{}, + }, + wantErr: false, + }, + { + name: "Multiple filenames", + args: []string{"-f", "file1.yaml", "-f", "file2.yaml"}, + want: &CmdFlagsProxy{ + Filenames: []string{"file1.yaml", "file2.yaml"}, + Namespace: "default", + Others: []string{}, + }, + wantErr: false, + }, + { + name: "Unrecognized arguments", + args: []string{"--filename", "file1.yaml", "extra1", "extra2"}, + want: &CmdFlagsProxy{ + Filenames: []string{"file1.yaml"}, + Namespace: "default", + Others: []string{"extra1", "extra2"}, + }, + wantErr: false, + }, + { + name: "Missing value for filename", + args: []string{"--filename"}, + want: nil, + wantErr: true, + }, + { + name: "Missing value for namespace", + args: []string{"--namespace"}, + want: nil, + wantErr: true, + }, + { + name: "Mixed flags and extra arguments-1", + args: []string{"--filename", "file1.yaml", "-n", "test-namespace", "extra1"}, + want: &CmdFlagsProxy{ + Filenames: []string{"file1.yaml"}, + Namespace: "test-namespace", + Others: []string{"extra1"}, + }, + wantErr: false, + }, + { + name: "Mixed flags and extra arguments-2", + args: []string{"--filename", "file1.yaml", "-n", "test-namespace", "extra1", "--dry-run=client", "-oyaml"}, + want: &CmdFlagsProxy{ + Filenames: []string{"file1.yaml"}, + Namespace: "test-namespace", + Others: []string{"extra1", "--dry-run=client", "-oyaml"}, + }, + wantErr: false, + }, + { + name: "Namespace override", + args: []string{"--filename", "file1.yaml", "-n", "test-namespace", "-n", "tmp", "extra1", "--dry-run=client", "-oyaml"}, + want: &CmdFlagsProxy{ + Filenames: []string{"file1.yaml"}, + Namespace: "tmp", + Others: []string{"extra1", "--dry-run=client", "-oyaml"}, + }, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := ParseCmdFlags(tt.args) + if (err != nil) != tt.wantErr { + t.Errorf("ParseCmdFlags() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("ParseCmdFlags() = %v, want %v", got, tt.want) + } + }) + } +}