Better success msgs, handle -d without args

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

View File

@ -20,14 +20,14 @@ 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", ctx)
return errors.Wrapf(err, "error deleting context %q", deletedName)
}
if wasActiveContext {
// TODO we don't always run as kubectx (sometimes "kubectl ctx")
printer.Warning(stderr, "You deleted the current context. use \"kubectx\" to select a different one.")
printer.Warning(stderr, "You deleted the current context. Use \"%s\" to select a new context.",
selfName())
}
printer.Success(stderr, "deleted context %q", deletedName)
printer.Success(stderr, `Deleted context %s.`, printer.SuccessColor.Sprint(deletedName))
}
return nil
}
@ -38,23 +38,25 @@ func deleteContext(name string) (deleteName string, wasActiveContext bool, err e
kc := new(kubeconfig.Kubeconfig).WithLoader(defaultLoader)
defer kc.Close()
if err := kc.Parse(); err != nil {
return "", false, errors.Wrap(err, "kubeconfig error")
return deleteName, false, errors.Wrap(err, "kubeconfig error")
}
cur := kc.GetCurrentContext()
// resolve "." to a real name
if name == "." {
if cur == "" {
return deleteName, false, errors.New("can't use '.' as the no active context is set")
}
wasActiveContext = true
name = cur
}
if !kc.ContextExists(name) {
return "", false, errors.New("context does not exist")
return name, false, errors.New("context does not exist")
}
if err := kc.DeleteContextEntry(name); err != nil {
return "", false, errors.Wrap(err, "failed to modify yaml doc")
return name, false, errors.Wrap(err, "failed to modify yaml doc")
}
return name, wasActiveContext, errors.Wrap(kc.Save(), "failed to save modified kubeconfig file")
}

View File

@ -25,6 +25,9 @@ func parseArgs(argv []string) Op {
}
if argv[0] == "-d" {
if len(argv) == 1 {
return UnsupportedOp{Err:fmt.Errorf("'-d' needs arguments")}
}
return DeleteOp{Contexts: argv[1:]}
}

View File

@ -45,7 +45,7 @@ func Test_parseArgs_new(t *testing.T) {
want: SwitchOp{Target: "-"}},
{name: "delete - without contexts",
args: []string{"-d"},
want: DeleteOp{[]string{}}},
want: UnsupportedOp{fmt.Errorf("'-d' needs arguments")}},
{name: "delete - current context",
args: []string{"-d", "."},
want: DeleteOp{[]string{"."}}},

View File

@ -43,7 +43,7 @@ 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.", name)
printer.Success(stderr, "Switched to context %s.", printer.SuccessColor.Sprint(name))
return nil
}

View File

@ -59,7 +59,7 @@ func (op RenameOp) Run(_, stderr io.Writer) error {
if err := kc.ModifyContextName(op.Old, op.New); err != nil {
return errors.Wrap(err, "failed to change context name")
}
if op.New == cur {
if op.Old == cur {
if err := kc.ModifyCurrentContext(op.New); err != nil {
return errors.Wrap(err, "failed to set current-context to new name")
}
@ -67,6 +67,8 @@ func (op RenameOp) Run(_, stderr io.Writer) error {
if err := kc.Save(); err != nil {
return errors.Wrap(err, "failed to save modified kubeconfig")
}
printer.Success(stderr, "Context %q renamed to %q.", op.Old, op.New)
printer.Success(stderr, "Context %s renamed to %s.",
printer.SuccessColor.Sprint(op.Old),
printer.SuccessColor.Sprint(op.New))
return nil
}