kubens: Start implementing stubs

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan
2020-04-18 13:09:59 -07:00
parent 342d21683b
commit 1982becb15
16 changed files with 239 additions and 30 deletions

9
cmd/kubens/current.go Normal file
View File

@@ -0,0 +1,9 @@
package main
import "io"
type CurrentOp struct{}
func (c CurrentOp) Run(stdout, stderr io.Writer) error {
panic("implement me")
}

40
cmd/kubens/flags.go Normal file
View File

@@ -0,0 +1,40 @@
package main
import (
"fmt"
"io"
"strings"
)
// UnsupportedOp indicates an unsupported flag.
type UnsupportedOp struct{ Err error }
func (op UnsupportedOp) Run(_, _ io.Writer) error {
return op.Err
}
// 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 {
//if env.IsInteractiveMode(os.Stdout) {
// return InteractiveSwitchOp{SelfCmd: os.Args[0]}
//}
return ListOp{}
}
if len(argv) == 1 {
v := argv[0]
if v == "--help" || v == "-h" {
return HelpOp{}
}
if v == "--current" || v == "-c" {
return CurrentOp{}
}
if strings.HasPrefix(v, "-") && v != "-" {
return UnsupportedOp{Err: fmt.Errorf("unsupported option '%s'", v)}
}
return SwitchOp{Target: argv[0]}
}
return UnsupportedOp{Err: fmt.Errorf("too many arguments")}
}

63
cmd/kubens/flags_test.go Normal file
View File

@@ -0,0 +1,63 @@
package main
import (
"fmt"
"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: "current shorthand",
args: []string{"-c"},
want: CurrentOp{}},
{name: "current long form",
args: []string{"--current"},
want: CurrentOp{}},
{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: UnsupportedOp{Err: fmt.Errorf("unsupported option '-x'")}},
{name: "too many args",
args: []string{"a", "b", "c"},
want: UnsupportedOp{Err: fmt.Errorf("too many arguments")}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseArgs(tt.args)
var opts cmp.Options
if _, ok := tt.want.(UnsupportedOp); ok {
opts = append(opts, cmp.Comparer(func(x, y UnsupportedOp) bool {
return (x.Err == nil && y.Err == nil) || (x.Err.Error() == y.Err.Error())
}))
}
if diff := cmp.Diff(got, tt.want, opts...); diff != "" {
t.Errorf("parseArgs(%#v) diff: %s", tt.args, diff)
}
})
}
}

44
cmd/kubens/help.go Normal file
View File

@@ -0,0 +1,44 @@
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// HelpOp describes printing help.
type HelpOp struct{}
func (_ HelpOp) Run(stdout, _ io.Writer) error {
return printUsage(stdout)
}
func printUsage(out io.Writer) error {
help := `USAGE:
%PROG% : list the namespaces in the current context
%PROG% <NAME> : change the active namespace of current context
%PROG% - : switch to the previous namespace in this context
%PROG% -c, --current : show the current namespace
%PROG% -h,--help : show this message
`
// TODO this replace logic is duplicated between this and kubectx
help = strings.ReplaceAll(help, "%PROG%", selfName())
_, err := fmt.Fprintf(out, "%s\n", help)
return errors.Wrap(err, "write error")
}
// selfName guesses how the user invoked the program.
func selfName() string {
// TODO this method is duplicated between this and kubectx
me := filepath.Base(os.Args[0])
pluginPrefix := "kubectl-"
if strings.HasPrefix(me, pluginPrefix) {
return "kubectl " + strings.TrimPrefix(me, pluginPrefix)
}
return "kubectx"
}

11
cmd/kubens/list.go Normal file
View File

@@ -0,0 +1,11 @@
package main
import (
"io"
)
type ListOp struct{}
func (op ListOp) Run(stdout, stderr io.Writer) error {
panic("implement me")
}

27
cmd/kubens/main.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import (
"fmt"
"io"
"os"
"github.com/ahmetb/kubectx/internal/env"
"github.com/ahmetb/kubectx/internal/printer"
)
type Op interface {
Run(stdout, stderr io.Writer) error
}
func main() {
op := parseArgs(os.Args[1:])
if err := op.Run(os.Stdout, os.Stderr); err != nil {
printer.Error(os.Stderr, err.Error())
if _, ok := os.LookupEnv(env.EnvDebug); ok {
// print stack trace in verbose mode
fmt.Fprintf(os.Stderr, "[DEBUG] error: %+v\n", err)
}
defer os.Exit(1)
}
}

11
cmd/kubens/switch.go Normal file
View File

@@ -0,0 +1,11 @@
package main
import (
"io"
)
type SwitchOp struct{ Target string }
func (s SwitchOp) Run(stdout, stderr io.Writer) error {
panic("implement me")
}