Skip to content

Commit 1288d50

Browse files
committed
feat: add autocomplete func
1 parent f67971a commit 1288d50

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

internal/cmd/delete.go

+12
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,16 @@ var deleteCmd = &cobra.Command{
3232
}
3333
log.Infof("Delete: %s", name)
3434
},
35+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
36+
if len(args) != 0 {
37+
return nil, cobra.ShellCompDirectiveNoFileComp
38+
}
39+
40+
stg, err := storage.GetStorage()
41+
if err != nil {
42+
return nil, cobra.ShellCompDirectiveNoFileComp
43+
}
44+
45+
return stg.GetMatched(toComplete), cobra.ShellCompDirectiveNoFileComp
46+
},
3547
}

internal/cmd/get.go

+12
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,16 @@ var getCmd = &cobra.Command{
3535

3636
fmt.Println(data)
3737
},
38+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
39+
if len(args) != 0 {
40+
return nil, cobra.ShellCompDirectiveNoFileComp
41+
}
42+
43+
stg, err := storage.GetStorage()
44+
if err != nil {
45+
return nil, cobra.ShellCompDirectiveNoFileComp
46+
}
47+
48+
return stg.GetMatched(toComplete), cobra.ShellCompDirectiveNoFileComp
49+
},
3850
}

internal/cmd/storage/set.go

+12
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,16 @@ var setCmd = &cobra.Command{
4343
}
4444
log.Infof("Storage Configured: %s", name)
4545
},
46+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
47+
if len(args) != 0 {
48+
return nil, cobra.ShellCompDirectiveNoFileComp
49+
}
50+
51+
cfg, err := storage.GetConfig()
52+
if err != nil {
53+
return nil, cobra.ShellCompDirectiveNoFileComp
54+
}
55+
56+
return cfg.GetMatchedStorage(toComplete), cobra.ShellCompDirectiveNoFileComp
57+
},
4658
}

internal/storage/config.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"os"
88
"path/filepath"
9+
"strings"
910
)
1011

1112
const (
@@ -136,3 +137,13 @@ func (cfg *config) AddStorage(n, k string, s map[string]interface{}) {
136137
cfg.CurrentStorage = n
137138
}
138139
}
140+
141+
func (cfg *config) GetMatchedStorage(toMatched string) []string {
142+
var matched []string
143+
for _, s := range cfg.Storages {
144+
if strings.HasPrefix(s.Name, toMatched) {
145+
matched = append(matched, s.Name)
146+
}
147+
}
148+
return matched
149+
}

internal/storage/github/storage.go

+32
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,38 @@ func (s *Storage) List() (tree.Node, error) {
146146
return gitTreeToNode(t)
147147
}
148148

149+
func (s *Storage) GetMatched(toMatched string) []string {
150+
var matched []string
151+
152+
ctx := context.Background()
153+
client := s.getClient()
154+
155+
if err := s.checkRepoExist(); err != nil {
156+
return matched
157+
}
158+
159+
b, _, err := client.Repositories.GetBranch(ctx, s.spec.owner, s.spec.repo, s.spec.Branch, true)
160+
if err != nil {
161+
return matched
162+
}
163+
164+
t, _, err := client.Git.GetTree(ctx, s.spec.owner, s.spec.repo, b.GetCommit().GetSHA(), true)
165+
if err != nil {
166+
return matched
167+
}
168+
169+
for _, e := range t.Entries {
170+
if e.GetType() == "blob" && strings.HasSuffix(e.GetPath(), gboxSuffix) {
171+
name := e.GetPath()
172+
name = name[:len(name)-len(gboxSuffix)]
173+
if strings.HasPrefix(name, toMatched) {
174+
matched = append(matched, name)
175+
}
176+
}
177+
}
178+
return matched
179+
}
180+
149181
func (s *Storage) checkRepoExist() error {
150182
ctx := context.Background()
151183
client := s.getClient()

internal/storage/storage.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Storage interface {
1212
Set(name, data string) error
1313
Delete(name string) error
1414
List() (tree.Node, error)
15+
GetMatched(toMatched string) []string
1516
}
1617

1718
var (

0 commit comments

Comments
 (0)