From f4f558004a07dceaf11cceb73b94bca40d904fc0 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Sun, 12 Apr 2020 22:51:48 -0700 Subject: [PATCH] Tidy up colors, help msgs, TODOs Signed-off-by: Ahmet Alp Balkan --- cmd/kubectx/help.go | 38 +++++++++++++++++++++++++------------ cmd/kubectx/kubeconfig.go | 3 +-- cmd/kubectx/main.go | 1 - cmd/kubectx/switch.go | 4 ++-- cmd/kubectx/unset.go | 4 ++-- internal/printer/printer.go | 33 +++++++++++++++++--------------- 6 files changed, 49 insertions(+), 34 deletions(-) diff --git a/cmd/kubectx/help.go b/cmd/kubectx/help.go index 51a99f4..316d34b 100644 --- a/cmd/kubectx/help.go +++ b/cmd/kubectx/help.go @@ -3,6 +3,9 @@ package main import ( "fmt" "io" + "os" + "path/filepath" + "strings" "github.com/pkg/errors" ) @@ -16,19 +19,30 @@ func (_ HelpOp) Run(stdout, _ io.Writer) error { func printUsage(out io.Writer) error { 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` + %PROG% : list the contexts + %PROG% : switch to context + %PROG% - : switch to the previous context + %PROG% -c, --current : show the current context name + %PROG% = : rename context to + %PROG% =. : rename current-context to + %PROG% -u, --unset : unset the current context + %PROG% -d [] : delete context ('.' for current-context) + %SPAC% (this command won't delete the user/cluster entry + %SPAC% referenced by the context entry) + %PROG% -h,--help : show this message` + help = strings.ReplaceAll(help, "%PROG%", selfName()) + help = strings.ReplaceAll(help, "%SPAC%", strings.Repeat(" ", len(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 { + me := filepath.Base(os.Args[0]) + pluginPrefix := "kubectl-" + if strings.HasPrefix(me, pluginPrefix) { + return "kubectl " + strings.TrimPrefix(me, pluginPrefix) + } + return "kubectx" +} diff --git a/cmd/kubectx/kubeconfig.go b/cmd/kubectx/kubeconfig.go index 3bcf9f7..beac789 100644 --- a/cmd/kubectx/kubeconfig.go +++ b/cmd/kubectx/kubeconfig.go @@ -51,16 +51,15 @@ func kubeconfigPath() (string, error) { return v, nil } + // default path home := homeDir() if home == "" { return "", errors.New("HOME or USERPROFILE environment variable not set") } - // return default path return filepath.Join(home, ".kube", "config"), nil } func homeDir() string { - // TODO move tests for this out of kubeconfigPath to TestHomeDir() if v := os.Getenv("XDG_CACHE_HOME"); v != "" { return v } diff --git a/cmd/kubectx/main.go b/cmd/kubectx/main.go index 7564434..fcfc75d 100644 --- a/cmd/kubectx/main.go +++ b/cmd/kubectx/main.go @@ -25,4 +25,3 @@ func main() { defer os.Exit(1) } } - diff --git a/cmd/kubectx/switch.go b/cmd/kubectx/switch.go index b6e0981..fa744eb 100644 --- a/cmd/kubectx/switch.go +++ b/cmd/kubectx/switch.go @@ -25,8 +25,8 @@ func (op SwitchOp) Run(_, stderr io.Writer) error { if err != nil { return errors.Wrap(err, "failed to switch context") } - printer.Success(stderr, "Switched to context %q.", newCtx) - return nil + err = printer.Success(stderr, "Switched to context %q.", newCtx) + return errors.Wrap(err, "print error") } // switchContext switches to specified context name. diff --git a/cmd/kubectx/unset.go b/cmd/kubectx/unset.go index ee97037..4d94f7c 100644 --- a/cmd/kubectx/unset.go +++ b/cmd/kubectx/unset.go @@ -1,12 +1,12 @@ package main import ( - "fmt" "io" "github.com/pkg/errors" "github.com/ahmetb/kubectx/internal/kubeconfig" + "github.com/ahmetb/kubectx/internal/printer" ) // UnsetOp indicates intention to remove current-context preference. @@ -26,6 +26,6 @@ func (_ UnsetOp) Run(_, stderr io.Writer) error { return errors.Wrap(err, "failed to save kubeconfig file after modification") } - _, err := fmt.Fprintln(stderr, "Successfully unset the active context for kubectl.") + err := printer.Success(stderr, "Active context unset for kubectl.") return errors.Wrap(err, "write error") } diff --git a/internal/printer/printer.go b/internal/printer/printer.go index 7fcc7c9..54452dc 100644 --- a/internal/printer/printer.go +++ b/internal/printer/printer.go @@ -8,9 +8,9 @@ import ( ) var ( - errorColor = color.New(color.FgRed, color.Bold) - warningColor = color.New(color.FgYellow, color.Bold) - successColor = color.New(color.FgGreen) + ErrorColor = color.New(color.FgRed, color.Bold) + WarningColor = color.New(color.FgYellow, color.Bold) + SuccessColor = color.New(color.FgGreen) ) func init() { @@ -19,24 +19,27 @@ func init() { return } if *colors { - errorColor.EnableColor() - warningColor.EnableColor() - successColor.EnableColor() + ErrorColor.EnableColor() + WarningColor.EnableColor() + SuccessColor.EnableColor() } else { - errorColor.DisableColor() - warningColor.DisableColor() - successColor.DisableColor() + ErrorColor.DisableColor() + WarningColor.DisableColor() + SuccessColor.DisableColor() } } -func Error(w io.Writer, format string, args ...interface{}) { - fmt.Fprintf(w, color.RedString("error: ")+format+"\n", args...) +func Error(w io.Writer, format string, args ...interface{}) error { + _, err := fmt.Fprintf(w, ErrorColor.Sprint("error: ")+format+"\n", args...) + return err } -func Warning(w io.Writer, format string, args ...interface{}) { - fmt.Fprintf(w, color.YellowString("warning: ")+format+"\n", args...) +func Warning(w io.Writer, format string, args ...interface{}) error { + _, err := fmt.Fprintf(w, WarningColor.Sprint("warning: ")+format+"\n", args...) + return err } -func Success(w io.Writer, format string, args ...interface{}) { - fmt.Fprintf(w, color.GreenString(fmt.Sprintf(format+"\n", args...))) +func Success(w io.Writer, format string, args ...interface{}) error { + _, err := fmt.Fprintf(w, SuccessColor.Sprint("✔ ")+fmt.Sprintf(format+"\n", args...)) + return err }