Tidy up colors, help msgs, TODOs

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2020-04-12 22:51:48 -07:00
parent 7598c4d4dd
commit f4f558004a
No known key found for this signature in database
GPG Key ID: 441833503E604E2C
6 changed files with 49 additions and 34 deletions

View File

@ -3,6 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -16,19 +19,30 @@ func (_ HelpOp) Run(stdout, _ io.Writer) error {
func printUsage(out io.Writer) error { func printUsage(out io.Writer) error {
help := `USAGE: help := `USAGE:
kubectx : list the contexts %PROG% : list the contexts
kubectx <NAME> : switch to context <NAME> %PROG% <NAME> : switch to context <NAME>
kubectx - : switch to the previous context %PROG% - : switch to the previous context
kubectx -c, --current : show the current context name %PROG% -c, --current : show the current context name
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME> %PROG% <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME> %PROG% <NEW_NAME>=. : rename current-context to <NEW_NAME>
kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context) %PROG% -u, --unset : unset the current context
(this command won't delete the user/cluster entry %PROG% -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
that is used by the context) %SPAC% (this command won't delete the user/cluster entry
kubectx -u, --unset : unset the current context %SPAC% referenced by the context entry)
%PROG% -h,--help : show this message`
kubectx -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) _, err := fmt.Fprintf(out, "%s\n", help)
return errors.Wrap(err, "write error") 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"
}

View File

@ -51,16 +51,15 @@ func kubeconfigPath() (string, error) {
return v, nil return v, nil
} }
// default path
home := homeDir() home := homeDir()
if home == "" { if home == "" {
return "", errors.New("HOME or USERPROFILE environment variable not set") return "", errors.New("HOME or USERPROFILE environment variable not set")
} }
// return default path
return filepath.Join(home, ".kube", "config"), nil return filepath.Join(home, ".kube", "config"), nil
} }
func homeDir() string { func homeDir() string {
// TODO move tests for this out of kubeconfigPath to TestHomeDir()
if v := os.Getenv("XDG_CACHE_HOME"); v != "" { if v := os.Getenv("XDG_CACHE_HOME"); v != "" {
return v return v
} }

View File

@ -25,4 +25,3 @@ func main() {
defer os.Exit(1) defer os.Exit(1)
} }
} }

View File

@ -25,8 +25,8 @@ func (op SwitchOp) Run(_, stderr io.Writer) error {
if err != nil { if err != nil {
return errors.Wrap(err, "failed to switch context") return errors.Wrap(err, "failed to switch context")
} }
printer.Success(stderr, "Switched to context %q.", newCtx) err = printer.Success(stderr, "Switched to context %q.", newCtx)
return nil return errors.Wrap(err, "print error")
} }
// switchContext switches to specified context name. // switchContext switches to specified context name.

View File

@ -1,12 +1,12 @@
package main package main
import ( import (
"fmt"
"io" "io"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/ahmetb/kubectx/internal/kubeconfig" "github.com/ahmetb/kubectx/internal/kubeconfig"
"github.com/ahmetb/kubectx/internal/printer"
) )
// UnsetOp indicates intention to remove current-context preference. // 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") 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") return errors.Wrap(err, "write error")
} }

View File

@ -8,9 +8,9 @@ import (
) )
var ( var (
errorColor = color.New(color.FgRed, color.Bold) ErrorColor = color.New(color.FgRed, color.Bold)
warningColor = color.New(color.FgYellow, color.Bold) WarningColor = color.New(color.FgYellow, color.Bold)
successColor = color.New(color.FgGreen) SuccessColor = color.New(color.FgGreen)
) )
func init() { func init() {
@ -19,24 +19,27 @@ func init() {
return return
} }
if *colors { if *colors {
errorColor.EnableColor() ErrorColor.EnableColor()
warningColor.EnableColor() WarningColor.EnableColor()
successColor.EnableColor() SuccessColor.EnableColor()
} else { } else {
errorColor.DisableColor() ErrorColor.DisableColor()
warningColor.DisableColor() WarningColor.DisableColor()
successColor.DisableColor() SuccessColor.DisableColor()
} }
} }
func Error(w io.Writer, format string, args ...interface{}) { func Error(w io.Writer, format string, args ...interface{}) error {
fmt.Fprintf(w, color.RedString("error: ")+format+"\n", args...) _, err := fmt.Fprintf(w, ErrorColor.Sprint("error: ")+format+"\n", args...)
return err
} }
func Warning(w io.Writer, format string, args ...interface{}) { func Warning(w io.Writer, format string, args ...interface{}) error {
fmt.Fprintf(w, color.YellowString("warning: ")+format+"\n", args...) _, err := fmt.Fprintf(w, WarningColor.Sprint("warning: ")+format+"\n", args...)
return err
} }
func Success(w io.Writer, format string, args ...interface{}) { func Success(w io.Writer, format string, args ...interface{}) error {
fmt.Fprintf(w, color.GreenString(fmt.Sprintf(format+"\n", args...))) _, err := fmt.Fprintf(w, SuccessColor.Sprint("✔ ")+fmt.Sprintf(format+"\n", args...))
return err
} }