Skip to content

Commit

Permalink
cmdflags.go
Browse files Browse the repository at this point in the history
  • Loading branch information
hashmap-kz committed Dec 30, 2024
1 parent 897e569 commit 179f3ea
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/util/cmdflags.go
Original file line number Diff line number Diff line change
@@ -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
}
111 changes: 111 additions & 0 deletions pkg/util/cmdflags_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 179f3ea

Please sign in to comment.