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 (
"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 <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`
%PROG% : list the contexts
%PROG% <NAME> : switch to context <NAME>
%PROG% - : switch to the previous context
%PROG% -c, --current : show the current context name
%PROG% <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
%PROG% <NEW_NAME>=. : rename current-context to <NEW_NAME>
%PROG% -u, --unset : unset the current context
%PROG% -d <NAME> [<NAME...>] : delete context <NAME> ('.' 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"
}

View File

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

View File

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

View File

@ -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.

View File

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

View File

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