-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.go
57 lines (50 loc) · 1.13 KB
/
shell.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
package main
import (
"fmt"
"os"
"github.com/agupta666/elf/actions"
"github.com/agupta666/elf/commands"
"github.com/agupta666/elf/router"
shellwords "github.com/mattn/go-shellwords"
readline "gopkg.in/readline.v1"
)
func processCmd(line string) {
args, err := shellwords.Parse(line)
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: syntax error")
return
}
if len(args) > 0 {
handler := commands.LookupHandler(args[0])
handler(args[1:])
}
}
func startShell() {
rl, err := readline.NewEx(&readline.Config{
Prompt: "elf> ",
HistoryFile: ".elf.hist",
AutoComplete: completer,
})
if err != nil {
panic(err)
}
defer rl.Close()
for {
line, err := rl.Readline()
if err != nil {
break
}
processCmd(line)
}
}
var completer = readline.NewPrefixCompleter(
readline.PcItem("route",
readline.PcItem("/path", readline.PcItemDynamic(actions.ActionList)),
),
readline.PcItem("lsrt"),
readline.PcItem("delrt", readline.PcItemDynamic(router.RouteNames)),
readline.PcItem("kvset"),
readline.PcItem("lskv"),
readline.PcItem("help", readline.PcItemDynamic(commands.CommandList)),
readline.PcItem("quit"),
)