-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
170 lines (140 loc) · 4 KB
/
repl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"io"
"os"
"path"
"github.com/UsingCoding/fpgo/pkg/slices"
"github.com/anmitsu/go-shlex"
"github.com/ergochat/readline"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"github.com/UsingCoding/gostore/data"
"github.com/UsingCoding/gostore/internal/gostore/app/service"
"github.com/UsingCoding/gostore/internal/gostore/app/storage"
"github.com/UsingCoding/gostore/internal/gostore/app/store"
"github.com/UsingCoding/gostore/internal/gostore/infrastructure/consoleoutput"
)
func repl(ctx *cli.Context) error {
output := consoleoutput.New(
os.Stdout,
consoleoutput.WithNewline(true),
)
rl, err := readline.New("gostore> ")
if err != nil {
return err
}
defer func() {
_ = rl.Close()
}()
// override ExitErrHandler to exclude application shutdown
ctx.App.ExitErrHandler = func(c *cli.Context, err error) {
if err == nil {
return
}
output.Errorf("%s", err)
}
s, _ := newStoreService(ctx)
data.Logo(output)
output.Printf("Welcome to GoStore!")
output.Printf("This is built-in shell with autocompletion")
for {
config := rl.GetConfig()
autoCompleter, err2 := completer(ctx, s)
if err2 != nil {
return errors.Wrap(err2, "failed to build completer")
}
config.AutoComplete = autoCompleter
err2 = rl.SetConfig(config)
if err2 != nil {
return errors.Wrap(err2, "failed to set readline config")
}
line, err2 := rl.ReadLine()
if err2 != nil {
if errors.Is(err2, readline.ErrInterrupt) || errors.Is(err2, io.EOF) {
return nil
}
return errors.Wrap(err2, "failed to read line")
}
args, err2 := shlex.Split(line, false)
if err2 != nil {
return errors.Wrap(err2, "failed to split line")
}
if len(args) < 1 {
continue
}
// ignore errors from app, since in REPL mode all errors from app displayed to user
_ = ctx.App.RunContext(ctx.Context, append([]string{"gostore"}, args...))
}
}
func completer(c *cli.Context, s service.Service) (readline.AutoCompleter, error) {
recursivelyCollectArgs := func(cmd *cli.Command, args *[]readline.PrefixCompleterInterface) error {
if cmd.Hidden {
return nil
}
var subCommands []readline.PrefixCompleterInterface
completions, err := completionItemForCmd(c, cmd.Name, s)
if err != nil {
return err
}
if len(completions) != 0 {
subCommands = append(subCommands, completions...)
}
for _, subcommand := range cmd.Subcommands {
subCommands = append(subCommands, readline.PcItem(subcommand.Name))
}
*args = append(*args, readline.PcItem(cmd.Name, subCommands...))
for _, alias := range cmd.Aliases {
*args = append(*args, readline.PcItem(alias, subCommands...))
}
return nil
}
var args []readline.PrefixCompleterInterface
for _, command := range c.App.Commands {
err := recursivelyCollectArgs(command, &args)
if err != nil {
return nil, err
}
}
return readline.NewPrefixCompleter(args...), nil
}
func completionItemForCmd(c *cli.Context, cmd string, s service.Service) ([]readline.PrefixCompleterInterface, error) {
switch cmd {
case "get",
"move",
"copy",
"remove":
return storeEntriesPathsForCompletion(c, s)
default:
return nil, nil
}
}
func storeEntriesPathsForCompletion(c *cli.Context, s service.Service) ([]readline.PrefixCompleterInterface, error) {
entries, err := s.List(c.Context, store.ListParams{})
if err != nil {
return nil, err
}
return slices.Map(inlinePaths(entries), func(p string) readline.PrefixCompleterInterface {
return readline.PcItem(p)
}), nil
}
func inlinePaths(entries []storage.Entry) []string {
var recursiveInlinePath func(e storage.Entry) []string
recursiveInlinePath = func(e storage.Entry) []string {
if len(e.Children) == 0 {
return []string{e.Name}
}
var res []string
for _, child := range e.Children {
childsPath := recursiveInlinePath(child)
res = append(res, slices.Map(childsPath, func(p string) string {
return path.Join(e.Name, p)
})...)
}
return res
}
var res []string
for _, entry := range entries {
res = append(res, recursiveInlinePath(entry)...)
}
return res
}