Skip to content

Commit

Permalink
Replaced %q with %s
Browse files Browse the repository at this point in the history
  • Loading branch information
jdharmon committed Aug 26, 2020
1 parent e746922 commit 5505f89
Show file tree
Hide file tree
Showing 18 changed files with 34 additions and 514 deletions.
2 changes: 1 addition & 1 deletion cmd/kubectx/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (op DeleteOp) Run(_, stderr io.Writer) error {
// TODO inefficency here. we open/write/close the same file many times.
deletedName, wasActiveContext, err := deleteContext(ctx)
if err != nil {
return errors.Wrapf(err, "error deleting context %q", deletedName)
return errors.Wrapf(err, "error deleting context \"%s\"", deletedName)
}
if wasActiveContext {
printer.Warning(stderr, "You deleted the current context. Use \"%s\" to select a new context.",
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectx/fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
if err != nil {
return errors.Wrap(err, "failed to switch context")
}
printer.Success(stderr, "Switched to context %q.", printer.SuccessColor.Sprint(name))
printer.Success(stderr, "Switched to context \"%s\".", printer.SuccessColor.Sprint(name))
return nil
}
4 changes: 2 additions & 2 deletions cmd/kubectx/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func TestPrintHelp(t *testing.T) {

out := buf.String()
if !strings.Contains(out, "USAGE:") {
t.Errorf("help string doesn't contain USAGE: ; output=%q", out)
t.Errorf("help string doesn't contain USAGE: ; output=\"%s\"", out)
}

if !strings.HasSuffix(out, "\n") {
t.Errorf("does not end with New line; output=%q", out)
t.Errorf("does not end with New line; output=\"%s\"", out)
}
}
4 changes: 2 additions & 2 deletions cmd/kubectx/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func (op RenameOp) Run(_, stderr io.Writer) error {
}

if !kc.ContextExists(op.Old) {
return errors.Errorf("context %q not found, can't rename it", op.Old)
return errors.Errorf("context \"%s\" not found, can't rename it", op.Old)
}

if kc.ContextExists(op.New) {
printer.Warning(stderr, "context %q exists, overwriting it.", op.New)
printer.Warning(stderr, "context \"%s\" exists, overwriting it.", op.New)
if err := kc.DeleteContextEntry(op.New); err != nil {
return errors.Wrap(err, "failed to delete new context to overwrite it")
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/kubectx/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Test_readLastContext_nonExistingFile(t *testing.T) {
t.Fatal(err)
}
if s != "" {
t.Fatalf("expected empty string; got=%q", s)
t.Fatalf("expected empty string; got=\"%s\"", s)
}
}

Expand All @@ -28,7 +28,7 @@ func Test_readLastContext(t *testing.T) {
t.Fatal(err)
}
if expected := "foo"; s != expected {
t.Fatalf("expected=%q; got=%q", expected, s)
t.Fatalf("expected=\"%s\"; got=\"%s\"", expected, s)
}
}

Expand Down Expand Up @@ -56,7 +56,7 @@ func Test_writeLastContext(t *testing.T) {
t.Fatal(err)
}
if expected := "ctx1"; v != expected {
t.Fatalf("read wrong value=%q; expected=%q", v, expected)
t.Fatalf("read wrong value=\"%s\"; expected=\"%s\"", v, expected)
}
}

Expand All @@ -71,7 +71,7 @@ func Test_kubectxFilePath(t *testing.T) {
t.Fatal(err)
}
if v != expected {
t.Fatalf("expected=%q got=%q", expected, v)
t.Fatalf("expected=\"%s\" got=\"%s\"", expected, v)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/kubectx/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (op SwitchOp) Run(_, stderr io.Writer) error {
if err != nil {
return errors.Wrap(err, "failed to switch context")
}
err = printer.Success(stderr, "Switched to context %q.", newCtx)
err = printer.Success(stderr, "Switched to context \"%s\".", printer.SuccessColor.Sprint(newCtx))
return errors.Wrap(err, "print error")
}

Expand All @@ -45,7 +45,7 @@ func switchContext(name string) (string, error) {

prev := kc.GetCurrentContext()
if !kc.ContextExists(name) {
return "", errors.Errorf("no context exists with the name: %q", name)
return "", errors.Errorf("no context exists with the name: \"%s\"", name)
}
if err := kc.ModifyCurrentContext(name); err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubens/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c CurrentOp) Run(stdout, _ io.Writer) error {
}
ns, err := kc.NamespaceOfContext(ctx)
if err != nil {
return errors.Wrapf(err, "failed to read namespace of %q", ctx)
return errors.Wrapf(err, "failed to read namespace of \"%s\"", ctx)
}
_, err = fmt.Fprintln(stdout, ns)
return errors.Wrap(err, "write error")
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubens/fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
if err != nil {
return errors.Wrap(err, "failed to switch namespace")
}
printer.Success(stderr, "Active namespace is %q.", printer.SuccessColor.Sprint(name))
printer.Success(stderr, "Active namespace is \"%s\".", printer.SuccessColor.Sprint(name))
return nil
}
2 changes: 1 addition & 1 deletion cmd/kubens/statefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func TestNSFile(t *testing.T) {
t.Fatal(err)
}
if expected := "bar"; v != expected {
t.Fatalf("Load()=%q; expected=%q", v, expected)
t.Fatalf("Load()=\"%s\"; expected=\"%s\"", v, expected)
}
}
6 changes: 3 additions & 3 deletions cmd/kubens/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s SwitchOp) Run(_, stderr io.Writer) error {
if err != nil {
return err
}
err = printer.Success(stderr, "Active namespace is %q", toNS)
err = printer.Success(stderr, "Active namespace is \"%s\"", printer.SuccessColor.Sprint(toNS))
return err
}

Expand Down Expand Up @@ -57,11 +57,11 @@ func switchNamespace(kc *kubeconfig.Kubeconfig, ns string) (string, error) {
return "", errors.Wrap(err, "failed to query if namespace exists (is cluster accessible?)")
}
if !ok {
return "", errors.Errorf("no namespace exists with name %q", ns)
return "", errors.Errorf("no namespace exists with name \"%s\"", ns)
}

if err := kc.SetNamespace(ctx, ns); err != nil {
return "", errors.Wrapf(err, "failed to change to namespace %q", ns)
return "", errors.Wrapf(err, "failed to change to namespace \"%s\"", ns)
}
if err := kc.Save(); err != nil {
return "", errors.Wrap(err, "failed to save kubeconfig file")
Expand Down
4 changes: 2 additions & 2 deletions internal/cmdutil/deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func TestPrintDeprecatedEnvWarnings_bgColors(t *testing.T) {
})
v := out.String()
if !strings.Contains(v, "KUBECTX_CURRENT_FGCOLOR") {
t.Fatalf("output doesn't contain 'KUBECTX_CURRENT_FGCOLOR': %q", v)
t.Fatalf("output doesn't contain 'KUBECTX_CURRENT_FGCOLOR': \"%s\"", v)
}
if !strings.Contains(v, "KUBECTX_CURRENT_BGCOLOR") {
t.Fatalf("output doesn't contain 'KUBECTX_CURRENT_BGCOLOR': %q", v)
t.Fatalf("output doesn't contain 'KUBECTX_CURRENT_BGCOLOR': \"%s\"", v)
}
}
6 changes: 3 additions & 3 deletions internal/cmdutil/kubeconfigloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Test_homeDir(t *testing.T) {

got := HomeDir()
if got != c.want {
t.Errorf("expected:%q got:%q", c.want, got)
t.Errorf("expected:\"%s\" got:\"%s\"", c.want, got)
}
for _, u := range unsets {
u()
Expand All @@ -80,7 +80,7 @@ func Test_kubeconfigPath(t *testing.T) {
t.Fatal(err)
}
if got != expected {
t.Fatalf("got=%q expected=%q", got, expected)
t.Fatalf("got=\"%s\" expected=\"%s\"", got, expected)
}
}

Expand All @@ -103,7 +103,7 @@ func Test_kubeconfigPath_envOvveride(t *testing.T) {
t.Fatal(err)
}
if expected := "foo"; v != expected {
t.Fatalf("expected=%q, got=%q", expected, v)
t.Fatalf("expected=\"%s\", got=\"%s\"", expected, v)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/kubeconfig/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (k *Kubeconfig) contextNode(name string) (*yaml.Node, error) {
return contextNode, nil
}
}
return nil, errors.Errorf("context with name %q not found", name)
return nil, errors.Errorf("context with name \"%s\" not found", name)
}

func (k *Kubeconfig) ContextNames() []string {
Expand Down
6 changes: 3 additions & 3 deletions internal/kubeconfig/currentcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestKubeconfig_GetCurrentContext(t *testing.T) {

expected := "foo"
if v != expected {
t.Fatalf("expected=%q; got=%q", expected, v)
t.Fatalf("expected=\"%s\"; got=\"%s\"", expected, v)
}
}

Expand All @@ -30,7 +30,7 @@ func TestKubeconfig_GetCurrentContext_missingField(t *testing.T) {

expected := ""
if v != expected {
t.Fatalf("expected=%q; got=%q", expected, v)
t.Fatalf("expected=\"%s\"; got=\"%s\"", expected, v)
}
}

Expand All @@ -50,6 +50,6 @@ func TestKubeconfig_UnsetCurrentContext(t *testing.T) {
out := tl.Output()
expected := testutil.KC().WithCurrentCtx("").ToYAML(t)
if out != expected {
t.Fatalf("expected=%q; got=%q", expected, out)
t.Fatalf("expected=\"%s\"; got=\"%s\"", expected, out)
}
}
4 changes: 2 additions & 2 deletions internal/kubeconfig/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func TestKubeconfig_NamespaceOfContext(t *testing.T) {
t.Fatal("expected err")
}
if expected := `default`; v1 != expected {
t.Fatalf("c1: expected=%q got=%q", expected, v1)
t.Fatalf("c1: expected=\"%s\" got=\"%s\"", expected, v1)
}

v2, err := kc.NamespaceOfContext("c2")
if err != nil {
t.Fatal("expected err")
}
if expected := `c2n1`; v2 != expected {
t.Fatalf("c2: expected=%q got=%q", expected, v2)
t.Fatalf("c2: expected=\"%s\" got=\"%s\"", expected, v2)
}
}

Expand Down
16 changes: 5 additions & 11 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package printer
import (
"fmt"
"io"
"strings"

"github.com/fatih/color"
)
Expand Down Expand Up @@ -31,21 +30,16 @@ func init() {
}

func Error(w io.Writer, format string, args ...interface{}) error {
return writeOutput(w, ErrorColor.Sprint("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{}) error {
return writeOutput(w, WarningColor.Sprint("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{}) error {
return writeOutput(w, SuccessColor.Sprint("✔ ")+format+"\n", args...)
}

func writeOutput(w io.Writer, format string, args ...interface{}) error {
// Replace %q with "%s" so unescaped color sequences are written to output
format = strings.ReplaceAll(format, "%q", "\"%s\"")

_, err := fmt.Fprintf(w, format, args...)
_, err := fmt.Fprintf(w, SuccessColor.Sprint("✔ ")+fmt.Sprintf(format+"\n", args...))
return err
}
Loading

0 comments on commit 5505f89

Please sign in to comment.