forked from FiloSottile/mkcert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.go
152 lines (126 loc) · 3.73 KB
/
prompt.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
package main
//go:generate mockgen -destination mock_prompt_test.go -package main -source prompt.go Prompt
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/manifoldco/promptui"
)
type Prompt interface {
GenRootCert() bool
RootMenu() int
InputHost() []string
}
type prompt struct {
}
type item struct {
Name string
Description string
}
func (p *prompt) GenRootCert() bool {
items := []item{
{Name: "生成网站根证书(不覆盖文件)", Description: "检查到" + execNameWithOutSuffix + "未生成根证书,根证书是颁发https证书所依赖的,你需要先生成它"},
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U0001F336 {{ .Name | cyan }}",
Inactive: " {{ .Name | cyan }}",
Selected: "\U0001F336 {{ .Name | red | cyan }}",
Details: `
--------- 详情 ----------
{{ "名字:" | faint }} {{ .Name }}
{{ "解释:" | faint }} {{ .Description }}`,
}
searcher := func(input string, index int) bool {
pepper := items[index]
name := strings.Replace(strings.ToLower(pepper.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "你要做什么",
Items: items,
Templates: templates,
Size: 4,
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return false
}
return i == 0
}
func (p *prompt) RootMenu() int {
items := []item{
{Name: "签名新证书", Description: "当前具备根证书,可以签名子证书"},
{Name: "导出根证书", Description: "当前携带根证书公私钥,具有完全权限,可导出公私钥,请妥善保管"},
{Name: "生成证书授信端", Description: "生成携带根证书公钥的授信客户端,可以部署到服务器让其他人安装根证书公钥授信"},
{Name: "退出", Description: "什么都不做"},
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U0001F336 {{ .Name | cyan }}",
Inactive: " {{ .Name | cyan }}",
Selected: "\U0001F336 {{ .Name | red | cyan }}",
Details: `
--------- 详情 ----------
{{ "名字:" | faint }} {{ .Name }}
{{ "解释:" | faint }} {{ .Description }}`,
}
searcher := func(input string, index int) bool {
pepper := items[index]
name := strings.Replace(strings.ToLower(pepper.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "当前是携带根证书,你要做什么",
Items: items,
Templates: templates,
Size: 4,
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return -1
}
return i
}
func (p *prompt) InputHost() []string {
validate := func(input string) error {
hosts := strings.Split(input, ",")
ipv4Regex, _ := regexp.Compile(`^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`)
hostRegex, _ := regexp.Compile(`^(?:[\w-]+\.)+[\w-]+$`)
for idx, host := range hosts {
match := ipv4Regex.MatchString(host)
if !match {
match = hostRegex.MatchString(host)
if !match {
return errors.New("第" + fmt.Sprint(idx+1) + "个域名或ip的格式不正确:" + host)
}
}
}
return nil
}
templates := &promptui.PromptTemplates{
Prompt: "{{ . }} ",
Valid: "{{ . | green }} ",
Invalid: "{{ . | red }} ",
Success: "{{ . | bold }} ",
}
prompt := promptui.Prompt{
Label: "输入服务器的域名以及ip,逗号隔开",
Templates: templates,
Validate: validate,
}
result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return []string{}
}
fmt.Printf("You answered %s\n", result)
return []string{result}
}