diff --git a/cmd/kubectx/help.go b/cmd/kubectx/help.go new file mode 100644 index 0000000..38a41cc --- /dev/null +++ b/cmd/kubectx/help.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "io" +) + +func printHelp(out io.Writer) { + help := `USAGE: + kubectx : list the contexts + kubectx : switch to context + kubectx - : switch to the previous context + kubectx -c, --current : show the current context name + kubectx = : rename context to + kubectx =. : rename current-context to + kubectx -d [] : delete context ('.' for current-context) + (this command won't delete the user/cluster entry + that is used by the context) + kubectx -u, --unset : unset the current context + + kubectx -h,--help : show this message` + + fmt.Fprintf(out, "%s\n", help) +} diff --git a/cmd/kubectx/help_test.go b/cmd/kubectx/help_test.go new file mode 100644 index 0000000..22a4ebb --- /dev/null +++ b/cmd/kubectx/help_test.go @@ -0,0 +1,21 @@ +package main + +import ( + "bytes" + "strings" + "testing" +) + +func TestPrintHelp(t *testing.T) { + var buf bytes.Buffer + printHelp(&buf) + + out := buf.String() + if !strings.Contains(out, "USAGE:") { + t.Errorf("help string doesn't contain USAGE: ; output=%q", out) + } + + if !strings.HasSuffix(out, "\n") { + t.Errorf("does not end with new line; output=%q", out) + } +} diff --git a/cmd/kubectx/main.go b/cmd/kubectx/main.go index a7d0c5a..6568fbb 100644 --- a/cmd/kubectx/main.go +++ b/cmd/kubectx/main.go @@ -4,6 +4,8 @@ import ( "fmt" "os" "strings" + + "github.com/fatih/color" ) func main() { @@ -12,6 +14,8 @@ func main() { op = parseArgs(os.Args[1:]) switch v := op.(type) { + case HelpOp: + printHelp(os.Stdout) case ListOp: // TODO implement panic("not implemented") @@ -19,8 +23,10 @@ func main() { // TODO implement panic("not implemented") case UnknownOp: - fmt.Printf("error: unsupported operation: %s\n", strings.Join(v.Args, " ")) - // TODO print --help string + fmt.Printf("%s unsupported operation: %s\n", + color.RedString("error:"), + strings.Join(v.Args, " ")) + printHelp(os.Stdout) os.Exit(1) default: fmt.Printf("internal error: operation type %T not handled", op) diff --git a/go.mod b/go.mod index eba3a2e..bd32419 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/ahmetb/kubectx go 1.14 -require github.com/google/go-cmp v0.4.0 +require ( + github.com/fatih/color v1.9.0 + github.com/google/go-cmp v0.4.0 +) diff --git a/go.sum b/go.sum index 4430646..5a81e34 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,13 @@ +github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= +github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= 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= +github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= +github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=