add printSuccess, pass writers to print funcs

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2020-04-12 12:37:04 -07:00
parent 7c2cf62cf0
commit 21d0a6aeeb
No known key found for this signature in database
GPG Key ID: 441833503E604E2C
5 changed files with 17 additions and 19 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"io" "io"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -23,9 +22,10 @@ func (op DeleteOp) Run(_, stderr io.Writer) error {
} }
if wasActiveContext { if wasActiveContext {
// TODO we don't always run as kubectx (sometimes "kubectl ctx") // 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 return nil
} }

View File

@ -50,7 +50,5 @@ func parseArgs(argv []string) Op {
} }
return SwitchOp{Target: argv[0]} return SwitchOp{Target: argv[0]}
} }
// TODO handle too many arguments e.g. "kubectx a b c"
return UnsupportedOp{Err: errors.New("too many arguments")} return UnsupportedOp{Err: errors.New("too many arguments")}
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"github.com/fatih/color" "github.com/fatih/color"
@ -13,15 +14,20 @@ func main() {
op = parseArgs(os.Args[1:]) op = parseArgs(os.Args[1:])
if err := op.Run(os.Stdout, os.Stderr); err != nil { if err := op.Run(os.Stdout, os.Stderr); err != nil {
printError(err.Error()) printError(os.Stderr, err.Error())
os.Exit(1) 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...) fmt.Fprintf(os.Stderr, color.RedString("error: ")+format+"\n", args...)
} }
func printWarning(format string, args ...interface{}) { func printWarning(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, color.YellowString("warning: ")+format+"\n", args...) 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...)))
}

View File

@ -1,9 +1,7 @@
package main package main
import ( import (
"fmt"
"io" "io"
"os"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -33,7 +31,7 @@ func parseRenameSyntax(v string) (string, string, bool) {
// rename changes the old (NAME or '.' for current-context) // rename changes the old (NAME or '.' for current-context)
// to the "new" value. If the old refers to the current-context, // to the "new" value. If the old refers to the current-context,
// current-context preference is also updated. // 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() f, rootNode, err := openKubeconfig()
if err != nil { if err != nil {
return nil return nil
@ -50,7 +48,7 @@ func (op RenameOp) Run(_, _ io.Writer) error {
} }
if checkContextExists(rootNode, op.New) { 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 { if err := modifyDocToDeleteContext(rootNode, op.New); err != nil {
return errors.Wrap(err, "failed to delete new context to overwrite it") 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 { if err := saveKubeconfigRaw(f, rootNode); err != nil {
return errors.Wrap(err, "failed to save modified kubeconfig") return errors.Wrap(err, "failed to save modified kubeconfig")
} }
printSuccess(stderr, "Context %q renamed to %q.", op.Old, op.New)
return nil return nil
} }
@ -93,8 +92,5 @@ func modifyContextName(rootNode *yaml.Node, old, new string) error {
if !changed { if !changed {
return errors.New("no changes were made") 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 return nil
} }

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"io" "io"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -24,8 +23,7 @@ func (op SwitchOp) Run(stdout, 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")
} }
// TODO use printSuccess when available. printSuccess(stderr, "Switched to context %q.", newCtx)
fmt.Fprintf(stderr, "Switched to context %q.\n", newCtx)
return nil return nil
} }