Skip to content

Commit

Permalink
feat: add tab completion for delete
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonTheLeg committed Sep 5, 2022
1 parent 303bb78 commit ee2db5d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
25 changes: 24 additions & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func newDeleteCommand() *deleteCmd {
-> 'delete <konfig id> [<konfig id 2>]' delete specific konf(s)
-> 'delete "my-konf*"' delete konf matching fileglob
`,
RunE: dc.delete,
RunE: dc.delete,
ValidArgsFunction: dc.completeDelete,
}

return dc
Expand Down Expand Up @@ -100,6 +101,28 @@ func idsForGlobs(f afero.Fs, patterns []string) ([]utils.KonfID, error) {
return ids, nil
}

func (c *deleteCmd) completeDelete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
konfs, err := store.FetchAllKonfs(c.fs)
if err != nil {
// if the store is just empty, return no suggestions, instead of throwing an error
if _, ok := err.(*store.EmptyStore); ok {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

cobra.CompDebugln(err.Error(), true)
return nil, cobra.ShellCompDirectiveError
}

sug := []string{}
for _, konf := range konfs {
// with the current design of 'set', we need to return the ID here in the autocomplete as the first part of the completion
// as it is directly passed to set
sug = append(sug, string(utils.IDFromClusterAndContext(konf.Cluster, konf.Context)))
}

return sug, cobra.ShellCompDirectiveNoFileComp
}

func init() {
rootCmd.AddCommand(newDeleteCommand().cmd)
}
42 changes: 42 additions & 0 deletions cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"sort"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/simontheleg/konf-go/config"
"github.com/simontheleg/konf-go/prompt"
"github.com/simontheleg/konf-go/store"
"github.com/simontheleg/konf-go/testhelper"
"github.com/simontheleg/konf-go/utils"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"k8s.io/utils/strings/slices"
)

Expand Down Expand Up @@ -226,3 +228,43 @@ func TestDelete(t *testing.T) {
}

}

func TestCompleteDelete(t *testing.T) {
// since cobra takes care of the majority of the complexity (like parsing out results that don't match completion start),
// we only need to test regular cases
fm := testhelper.FilesystemManager{}

tt := map[string]struct {
fsCreator func() afero.Fs
expComp []string
expCompDirec cobra.ShellCompDirective
}{
"normal results": {
testhelper.FSWithFiles(fm.StoreDir, fm.SingleClusterSingleContextASIA, fm.SingleClusterSingleContextEU),
[]string{"dev-asia_dev-asia-1", "dev-eu_dev-eu-1"},
cobra.ShellCompDirectiveNoFileComp,
},
"no results": {
testhelper.FSWithFiles(fm.StoreDir),
[]string{},
cobra.ShellCompDirectiveNoFileComp,
},
}

for name, tc := range tt {
t.Run(name, func(t *testing.T) {
dcmd := newDeleteCommand()
dcmd.fs = tc.fsCreator()

res, compdirec := dcmd.completeDelete(dcmd.cmd, []string{}, "")

if !cmp.Equal(res, tc.expComp) {
t.Errorf("Exp and given comps differ: \n '%s'", cmp.Diff(tc.expComp, res))
}

if compdirec != tc.expCompDirec {
t.Errorf("Exp compdirec %q, got %q", tc.expCompDirec, compdirec)
}
})
}
}

0 comments on commit ee2db5d

Please sign in to comment.