From 21d0a6aeeb17cbec255f3763b580af1206b6b49a Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Sun, 12 Apr 2020 12:37:04 -0700 Subject: [PATCH] add printSuccess, pass writers to print funcs Signed-off-by: Ahmet Alp Balkan --- cmd/kubectx/delete.go | 6 +++--- cmd/kubectx/flags.go | 2 -- cmd/kubectx/main.go | 14 ++++++++++---- cmd/kubectx/rename.go | 10 +++------- cmd/kubectx/switch.go | 4 +--- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/cmd/kubectx/delete.go b/cmd/kubectx/delete.go index 13c875d..1c4164b 100644 --- a/cmd/kubectx/delete.go +++ b/cmd/kubectx/delete.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io" "github.com/pkg/errors" @@ -23,9 +22,10 @@ func (op DeleteOp) Run(_, stderr io.Writer) error { } if wasActiveContext { // TODO we don't always run as kubectx (sometimes "kubectl ctx") - printWarning("You deleted the current context. use \"kubectx\" to select a different one.") + printWarning(stderr,"You deleted the current context. use \"kubectx\" to select a different one.") } - fmt.Fprintf(stderr, "deleted context %q\n", deletedName) // TODO write with printSuccess (i.e. green) + + printSuccess(stderr, "deleted context %q", deletedName) } return nil } diff --git a/cmd/kubectx/flags.go b/cmd/kubectx/flags.go index 2ce1bf0..5056a89 100644 --- a/cmd/kubectx/flags.go +++ b/cmd/kubectx/flags.go @@ -50,7 +50,5 @@ func parseArgs(argv []string) Op { } return SwitchOp{Target: argv[0]} } - - // TODO handle too many arguments e.g. "kubectx a b c" return UnsupportedOp{Err: errors.New("too many arguments")} } diff --git a/cmd/kubectx/main.go b/cmd/kubectx/main.go index 5b1127f..fed54e4 100644 --- a/cmd/kubectx/main.go +++ b/cmd/kubectx/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "os" "github.com/fatih/color" @@ -13,15 +14,20 @@ func main() { op = parseArgs(os.Args[1:]) if err := op.Run(os.Stdout, os.Stderr); err != nil { - printError(err.Error()) + printError(os.Stderr, err.Error()) os.Exit(1) } } -func printError(format string, args ...interface{}) { +func printError(w io.Writer, format string, args ...interface{}) { fmt.Fprintf(os.Stderr, color.RedString("error: ")+format+"\n", args...) } -func printWarning(format string, args ...interface{}) { - fmt.Fprintf(os.Stderr, color.YellowString("warning: ")+format+"\n", args...) +func printWarning(w io.Writer, format string, args ...interface{}) { + fmt.Fprintf(w, color.YellowString("warning: ")+format+"\n", args...) } + +func printSuccess(w io.Writer, format string, args ...interface{}) { + fmt.Fprintf(w, color.GreenString(fmt.Sprintf(format+"\n", args...))) +} + diff --git a/cmd/kubectx/rename.go b/cmd/kubectx/rename.go index c875bab..e94909f 100644 --- a/cmd/kubectx/rename.go +++ b/cmd/kubectx/rename.go @@ -1,9 +1,7 @@ package main import ( - "fmt" "io" - "os" "strings" "github.com/pkg/errors" @@ -33,7 +31,7 @@ func parseRenameSyntax(v string) (string, string, bool) { // rename changes the old (NAME or '.' for current-context) // to the "new" value. If the old refers to the current-context, // current-context preference is also updated. -func (op RenameOp) Run(_, _ io.Writer) error { +func (op RenameOp) Run(_, stderr io.Writer) error { f, rootNode, err := openKubeconfig() if err != nil { return nil @@ -50,7 +48,7 @@ func (op RenameOp) Run(_, _ io.Writer) error { } if checkContextExists(rootNode, op.New) { - printWarning("context %q exists, overwriting it.", op.New) + printWarning(stderr, "context %q exists, overwriting it.", op.New) if err := modifyDocToDeleteContext(rootNode, op.New); err != nil { return errors.Wrap(err, "failed to delete new context to overwrite it") } @@ -67,6 +65,7 @@ func (op RenameOp) Run(_, _ io.Writer) error { if err := saveKubeconfigRaw(f, rootNode); err != nil { return errors.Wrap(err, "failed to save modified kubeconfig") } + printSuccess(stderr, "Context %q renamed to %q.", op.Old, op.New) return nil } @@ -93,8 +92,5 @@ func modifyContextName(rootNode *yaml.Node, old, new string) error { if !changed { return errors.New("no changes were made") } - // TODO use printSuccess - // TODO consider moving printing logic to main - fmt.Fprintf(os.Stderr, "Context %q renamed to %q.\n", old, new) return nil } diff --git a/cmd/kubectx/switch.go b/cmd/kubectx/switch.go index 7cf5cc9..c68cb8d 100644 --- a/cmd/kubectx/switch.go +++ b/cmd/kubectx/switch.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io" "github.com/pkg/errors" @@ -24,8 +23,7 @@ func (op SwitchOp) Run(stdout, stderr io.Writer) error { if err != nil { return errors.Wrap(err, "failed to switch context") } - // TODO use printSuccess when available. - fmt.Fprintf(stderr, "Switched to context %q.\n", newCtx) + printSuccess(stderr, "Switched to context %q.", newCtx) return nil }