Skip to content

Commit ee4d507

Browse files
committed
Bettel shell completion for totp get and totp delete
1 parent 579e2c8 commit ee4d507

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

main.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ func addItem(name, secret string) error {
3232
return keychain.AddItem(item)
3333
}
3434

35+
func listItems() ([]string, error) {
36+
// Query items
37+
query := keychain.NewItem()
38+
query.SetSecClass(keychain.SecClassGenericPassword)
39+
query.SetService(serviceName)
40+
query.SetMatchLimit(keychain.MatchLimitAll)
41+
query.SetReturnAttributes(true)
42+
results, err := keychain.QueryItem(query)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
var names []string
48+
for _, r := range results {
49+
names = append(names, r.Account)
50+
}
51+
return names, nil
52+
}
53+
3554
func main() {
3655
var useBarcodeHintWhenScan bool
3756

@@ -135,20 +154,13 @@ func main() {
135154
Short: "List all registered TOTP codes",
136155
Args: cobra.NoArgs,
137156
RunE: func(cmd *cobra.Command, args []string) error {
138-
// Query items
139-
query := keychain.NewItem()
140-
query.SetSecClass(keychain.SecClassGenericPassword)
141-
query.SetService(serviceName)
142-
query.SetMatchLimit(keychain.MatchLimitAll)
143-
query.SetReturnAttributes(true)
144-
results, err := keychain.QueryItem(query)
157+
names, err := listItems()
145158
if err != nil {
146159
return err
147160
}
148161

149-
// List query results
150-
for _, r := range results {
151-
fmt.Println(r.Account)
162+
for _, name := range names {
163+
fmt.Println(name)
152164
}
153165
return nil
154166
},
@@ -181,6 +193,18 @@ func main() {
181193
fmt.Println(gotp.NewDefaultTOTP(string(r.Data)).Now())
182194
return nil
183195
},
196+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
197+
if len(args) != 0 {
198+
return nil, cobra.ShellCompDirectiveNoFileComp
199+
}
200+
201+
names, err := listItems()
202+
if err != nil {
203+
return nil, cobra.ShellCompDirectiveNoFileComp
204+
}
205+
206+
return names, cobra.ShellCompDirectiveNoFileComp
207+
},
184208
}
185209

186210
var cmdDelete = &cobra.Command{
@@ -204,6 +228,18 @@ func main() {
204228
fmt.Printf("Successfully deleted \"%v\".\n", name)
205229
return nil
206230
},
231+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
232+
if len(args) != 0 {
233+
return nil, cobra.ShellCompDirectiveNoFileComp
234+
}
235+
236+
names, err := listItems()
237+
if err != nil {
238+
return nil, cobra.ShellCompDirectiveNoFileComp
239+
}
240+
241+
return names, cobra.ShellCompDirectiveNoFileComp
242+
},
207243
}
208244

209245
var cmdTemp = &cobra.Command{

0 commit comments

Comments
 (0)