Skip to content

Commit

Permalink
feat(delete/node): challenge deletion by text phrase
Browse files Browse the repository at this point in the history
  • Loading branch information
hansbogert committed Oct 20, 2023
1 parent e906fa6 commit e837b31
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
18 changes: 17 additions & 1 deletion internal/ui/dialog/delete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dialog

import (
"fmt"

"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tview"
Expand All @@ -25,7 +27,7 @@ var propagationOptions []string = []string{
}

// ShowDelete pops a resource deletion dialog.
func ShowDelete(styles config.Dialog, pages *ui.Pages, msg string, ok okFunc, cancel cancelFunc) {
func ShowDelete(styles config.Dialog, pages *ui.Pages, msg string, ok okFunc, cancel cancelFunc, challenge string) {
propagation, force := "", false
f := tview.NewForm()
f.SetItemPadding(0)
Expand All @@ -45,11 +47,25 @@ func ShowDelete(styles config.Dialog, pages *ui.Pages, msg string, ok okFunc, ca
f.AddCheckbox("Force:", force, func(_ string, checked bool) {
force = checked
})

confirmCheck := ""
if challenge != "" {
f.AddInputField(fmt.Sprintf("Confirm with: \"%s\":", challenge), confirmCheck, 30, nil, func(text string) {
confirmCheck = text
})
}

f.AddButton("Cancel", func() {
dismiss(pages)
cancel()
})

f.AddButton("OK", func() {
if challenge != "" && confirmCheck != challenge {
cancel()
return
}

switch propagation {
case noDeletePropagation:
ok(nil, force)
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/dialog/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestDeleteDialog(t *testing.T) {
caFunc := func() {
assert.True(t, true)
}
ShowDelete(config.Dialog{}, p, "Yo", okFunc, caFunc)
ShowDelete(config.Dialog{}, p, "Yo", okFunc, caFunc, "")

d := p.GetPrimitive(dialogKey).(*tview.ModalForm)
assert.NotNil(t, d)
Expand Down
12 changes: 9 additions & 3 deletions internal/view/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,13 @@ func (b *Browser) deleteCmd(evt *tcell.EventKey) *tcell.EventKey {
b.simpleDelete(selections, msg)
return nil
}
b.resourceDelete(selections, msg)

challenge := ""
if b.GVR().R() == "nodes" {
challenge = "delete!"
}

b.resourceDelete(selections, msg, challenge)
}

return nil
Expand Down Expand Up @@ -542,7 +548,7 @@ func (b *Browser) simpleDelete(selections []string, msg string) {
}, func() {})
}

func (b *Browser) resourceDelete(selections []string, msg string) {
func (b *Browser) resourceDelete(selections []string, msg string, challenge string ) {
dialog.ShowDelete(b.app.Styles.Dialog(), b.app.Content.Pages, msg, func(propagation *metav1.DeletionPropagation, force bool) {
b.ShowDeleted()
if len(selections) > 1 {
Expand All @@ -563,5 +569,5 @@ func (b *Browser) resourceDelete(selections []string, msg string) {
b.GetTable().DeleteMark(sel)
}
b.refresh()
}, func() {})
}, func() {}, challenge)
}
2 changes: 1 addition & 1 deletion internal/view/xray.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ func (x *Xray) resourceDelete(gvr client.GVR, spec *xray.NodeSpec, msg string) {
x.app.factory.DeleteForwarder(spec.Path())
}
x.Refresh()
}, func() {})
}, func() {}, "")
}

// ----------------------------------------------------------------------------
Expand Down

0 comments on commit e837b31

Please sign in to comment.