Start porting to Go: parse flags

Parse help/list/swap command line flags.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2020-04-10 11:54:01 -07:00
parent 37c765684f
commit 6c9273e582
No known key found for this signature in database
GPG Key ID: 441833503E604E2C
5 changed files with 122 additions and 0 deletions

46
cmd/kubectx/flags.go Normal file
View File

@ -0,0 +1,46 @@
package main
import "strings"
type Op interface{}
// HelpOp describes printing help.
type HelpOp struct{}
// ListOp describes listing contexts.
type ListOp struct{}
// SwitchOp indicates intention to switch contexts.
type SwitchOp struct {
Target string // '-' for back and forth, or NAME
}
// UnknownOp indicates an unsupported flag.
type UnknownOp struct{ Args []string }
// parseArgs looks at flags (excl. executable name, i.e. argv[0])
// and decides which operation should be taken.
func parseArgs(argv []string) Op {
if len(argv) == 0 {
return ListOp{}
}
if len(argv) == 1 {
v := argv[0]
if v == "--help" || v == "-h" {
return HelpOp{}
}
if strings.HasPrefix(v, "-") && v != "-" {
return UnknownOp{argv}
}
// TODO handle -d
// TODO handle -u/--unset
// TODO handle -c/--current
return SwitchOp{Target: argv[0]}
}
// TODO handle too many arguments e.g. "kubectx a b c"
return UnknownOp{}
}

52
cmd/kubectx/flags_test.go Normal file
View File

@ -0,0 +1,52 @@
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func Test_parseArgs_new(t *testing.T) {
tests := []struct {
name string
args []string
want Op
}{
{name: "nil Args",
args: nil,
want: ListOp{}},
{name: "empty Args",
args: []string{},
want: ListOp{}},
{name: "help shorthand",
args: []string{"-h"},
want: HelpOp{}},
{name: "help long form",
args: []string{"--help"},
want: HelpOp{}},
{name: "switch by name",
args: []string{"foo"},
want: SwitchOp{Target: "foo"}},
{name: "switch by swap",
args: []string{"-"},
want: SwitchOp{Target: "-"}},
{name: "unrecognized flag",
args: []string{"-x"},
want: UnknownOp{Args: []string{"-x"}}},
// TODO add more UnknownOp cases
// TODO consider these cases
// - kubectx foo --help
// - kubectx -h --help
// - kubectx -d foo --h
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseArgs(tt.args)
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("parseArgs(%#v) diff: %s", tt.args, diff)
}
})
}
}

16
cmd/kubectx/main.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"os"
)
func main() {
// parse command-line flags
argv := os.Args[1:]
fmt.Printf("%#v\n", argv)
//var op Op
//op _= parseArgs(argv) // -> DeleteOp RenameOp HelpOp UnrecognizedFlags
}

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/ahmetb/kubectx
go 1.14
require github.com/google/go-cmp v0.4.0

3
go.sum Normal file
View File

@ -0,0 +1,3 @@
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=