Support help op, add color to error

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2020-04-10 12:07:58 -07:00
parent 1b2fc5961a
commit d2267aa60c
No known key found for this signature in database
GPG Key ID: 441833503E604E2C
5 changed files with 67 additions and 3 deletions

24
cmd/kubectx/help.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"fmt"
"io"
)
func printHelp(out io.Writer) {
help := `USAGE:
kubectx : list the contexts
kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context
kubectx -c, --current : show the current context name
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' 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)
}

21
cmd/kubectx/help_test.go Normal file
View File

@ -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)
}
}

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"github.com/fatih/color"
) )
func main() { func main() {
@ -12,6 +14,8 @@ func main() {
op = parseArgs(os.Args[1:]) op = parseArgs(os.Args[1:])
switch v := op.(type) { switch v := op.(type) {
case HelpOp:
printHelp(os.Stdout)
case ListOp: case ListOp:
// TODO implement // TODO implement
panic("not implemented") panic("not implemented")
@ -19,8 +23,10 @@ func main() {
// TODO implement // TODO implement
panic("not implemented") panic("not implemented")
case UnknownOp: case UnknownOp:
fmt.Printf("error: unsupported operation: %s\n", strings.Join(v.Args, " ")) fmt.Printf("%s unsupported operation: %s\n",
// TODO print --help string color.RedString("error:"),
strings.Join(v.Args, " "))
printHelp(os.Stdout)
os.Exit(1) os.Exit(1)
default: default:
fmt.Printf("internal error: operation type %T not handled", op) fmt.Printf("internal error: operation type %T not handled", op)

5
go.mod
View File

@ -2,4 +2,7 @@ module github.com/ahmetb/kubectx
go 1.14 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
)

10
go.sum
View File

@ -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 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 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= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=