/
command_help.go
81 lines (69 loc) · 2.37 KB
/
command_help.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
package beans
import (
"fmt"
"github.com/lixianmin/gonsole/ifs"
"net/url"
"sort"
)
/********************************************************************
created: 2019-11-16
author: lixianmin
Copyright (C) - All Rights Reserved
*********************************************************************/
type CommandHelp struct {
Name string
Note string
}
func FetchCommandHelp(commands []ifs.Command, isAuthorized bool) []CommandHelp {
var list = make([]CommandHelp, 0, len(commands)+2)
list = append(list, CommandHelp{Name: "sub xxx", Note: "订阅主题,例:sub top"})
list = append(list, CommandHelp{Name: "unsub xxx", Note: "取消订阅主题,例:unsub top"})
return fetchCommandHelpImpl(list, commands, isAuthorized)
}
func FetchTopicHelp(topics []ifs.Command, isAuthorized bool) []CommandHelp {
var list = make([]CommandHelp, 0, len(topics))
return fetchCommandHelpImpl(list, topics, isAuthorized)
}
func fetchCommandHelpImpl(list []CommandHelp, commands []ifs.Command, isAuthorized bool) []CommandHelp {
for _, cmd := range commands {
if (isAuthorized || cmd.IsPublic()) && !cmd.IsInvisible() {
list = append(list, CommandHelp{Name: cmd.GetName(), Note: cmd.GetNote()})
}
}
// 排序
sort.Slice(list, func(i, j int) bool {
return list[i].Name < list[j].Name
})
return list
}
func FetchPProfHelp(args []string) []CommandHelp {
var host = ""
if len(args) >= 2 {
if u, err := url.Parse(args[1]); err == nil {
host = u.Scheme + "://" + u.Host
}
}
var list = make([]CommandHelp, 0, 8)
list = append(list,
CommandHelp{
Name: fmt.Sprintf("<a href=\"%s/debug/pprof\">pprof</a>", host),
Note: "",
}, CommandHelp{
Name: "<a href=\"https://github.com/lixianmin/writer/blob/master/golang/pprof.md\">参考文档</a>",
Note: "",
}, CommandHelp{
Name: "CPU (30s)",
Note: addCopyButton(fmt.Sprintf("go tool pprof -http=: %s/debug/pprof/profile", host)),
}, CommandHelp{
Name: "Heap",
Note: addCopyButton(fmt.Sprintf("go tool pprof -http=: %s/debug/pprof/heap", host)),
}, CommandHelp{
Name: "goroutine",
Note: addCopyButton(fmt.Sprintf("curl -G -k %s/debug/pprof/goroutine?debug=2 > goroutine.list.txt", host)),
})
return list
}
func addCopyButton(text string) string {
var result = fmt.Sprintf("%s <input type=\"button\" class=\"copy_button\" onclick=\"copyToClipboard('%s')\" value=\"复制\"/>", text, text)
return result
}