-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.go
118 lines (110 loc) · 3.29 KB
/
common.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
package completers
import (
"strings"
prompt "github.com/c-bata/go-prompt"
"github.com/lflxp/showme/pkg/prompt/suggests"
)
// 解析函数 判断最新参数是否含有-字符
func getPreviousOption(d prompt.Document) (cmd, option string, found bool) {
args := strings.Split(d.TextBeforeCursor(), " ")
l := len(args)
if l >= 2 {
option = args[l-2]
}
if strings.HasPrefix(option, "-") {
return args[0], option, true
}
return "", "", false
}
// 全局固定命令
func GlobalOptionFunc(d prompt.Document) ([]prompt.Suggest, bool) {
cmd, option, found := getPreviousOption(d)
if !found {
return []prompt.Suggest{}, false
}
switch cmd {
case "dashboard":
// 带 - 参数的命令提示
// 命令输入大于等于两个
switch option {
case "-v", "-vvv":
return prompt.FilterHasPrefix(
suggests.DetailSuggest(d),
d.GetWordBeforeCursor(),
true,
), true
}
}
return []prompt.Suggest{}, false
}
// 用户自定义命令
func FirstCommandFunc(d prompt.Document, args []string) []prompt.Suggest {
if len(args) <= 1 {
return prompt.FilterHasPrefix(Commands, args[0], true)
}
first := args[0]
switch first {
case "dashboard":
second := args[1]
if len(args) == 2 {
subcommands := []prompt.Suggest{
{Text: "help"},
{Text: "show", Description: "console for show me"},
{Text: "helloworld", Description: "dashboard for tcell cellviews.go"},
}
return prompt.FilterHasPrefix(subcommands, second, true)
}
case "gocui":
second := args[1]
if len(args) == 2 {
subcommands := []prompt.Suggest{
{Text: "help", Description: "帮助文档"},
{Text: "active", Description: "界面布局"},
}
return prompt.FilterHasPrefix(subcommands, second, true)
}
case "scan":
second := args[1]
if len(args) == 2 {
subcommands := []prompt.Suggest{
{Text: "192.168.50.1-255", Description: "192网段"},
{Text: "192.168.40.1-255", Description: "192网段"},
{Text: "192.168.1.1-255", Description: "192网段"},
{Text: "10.128.0.1-255", Description: "10网段"},
{Text: "10.128.142.1-255", Description: "10网段"},
{Text: "172.16.13.1-255", Description: "172网段"},
{Text: "172.168.0.1-255", Description: "172网段"},
}
return prompt.FilterHasPrefix(subcommands, second, true)
}
case "mysql":
second := args[1]
if len(args) == 2 {
subcommands := []prompt.Suggest{
{Text: "test", Description: "测试获取数据"},
{Text: "status", Description: "查看状态"},
{Text: "processlist", Description: "查看进程"},
}
return prompt.FilterHasPrefix(subcommands, second, true)
}
third := args[2]
if len(args) == 3 {
switch second {
case "test":
subcommands := []prompt.Suggest{
{Text: "GetHostAndIps", Description: "GetHostAndIps"},
{Text: "GetShowDatabases", Description: "GetShowDatabases"},
{Text: "GetShowGlobalVariables", Description: "GetShowGlobalVariables"},
{Text: "GetShowVariables", Description: "GetShowVariables"},
{Text: "GetShowStatus", Description: "GetShowStatus"},
{Text: "GetShowGlobalStatus", Description: "GetShowGlobalStatus"},
{Text: "GetShowEngineInnodbStatus", Description: "GetShowEngineInnodbStatus"},
}
return prompt.FilterHasPrefix(subcommands, third, true)
}
}
default:
return []prompt.Suggest{}
}
return []prompt.Suggest{}
}