This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
completion.go
77 lines (68 loc) · 1.63 KB
/
completion.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
// psmcli
// Copyright (C) 2014 Procera Networks, Inc.
package main
import (
"regexp"
"sort"
"strings"
"kastelo.io/psmcli/completion"
)
func importSMD(services map[string]smdService) []completion.Matcher {
var matchers []completion.Matcher
var svcs []string
for name := range services {
svcs = append(svcs, name)
}
sort.Strings(svcs)
roots := make(map[string]completion.Matcher)
for _, svc := range svcs {
parts := strings.SplitN(svc, ".", 2)
if len(parts) != 2 {
continue
}
cmd := &completion.Literal{
Value: parts[1],
}
if root, ok := roots[parts[0]]; ok {
root.AddNext(cmd)
} else {
root = &completion.Literal{Value: parts[0]}
roots[parts[0]] = root
matchers = append(matchers, root)
root.AddNext(cmd)
}
var cur completion.Matcher = cmd
for _, param := range services[svc].Parameters {
if param.Name == "type" && !param.Optional && param.Type == "string" {
s := &completion.Combine{
Matchers: []completion.Matcher{
&completion.Literal{Value: "session"},
&completion.Literal{Value: "subscriber"},
&completion.Literal{Value: "group"},
},
}
cur.AddNext(s)
cur = s
} else {
var m completion.Matcher
switch param.Type {
case "integer":
m = &completion.Regexp{
Exp: regexp.MustCompile(`^\d+$`),
Placeholder: param.Name + " (int)",
Optional: param.Optional,
}
default:
m = &completion.Regexp{
Exp: regexp.MustCompile(`.`),
Placeholder: param.Name + " (" + param.Type + ")",
Optional: param.Optional,
}
}
cur.AddNext(m)
cur = m
}
}
}
return matchers
}