-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
275 lines (248 loc) · 5.86 KB
/
render.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"fmt"
"net/mail"
"os"
"path/filepath"
"strings"
"text/tabwriter"
"github.com/manifoldco/promptui"
"github.com/thansetan/git-sw/pkg/gitconfig"
"golang.org/x/crypto/ssh"
)
func validateNotEmpty(s string) error {
if len(s) <= 0 {
return ErrEmptyField
}
return nil
}
func displayCreateForm() (Profile, error) {
var (
profile Profile
err error
)
profile.Config = gitconfig.New()
profileMap := make(map[string]struct{})
for i := range profiles {
profileMap[strings.ToLower(profiles[i].Name)] = struct{}{}
}
profileNamePrompt := promptui.Prompt{
Label: "Name",
Validate: func(s string) error {
err := validateNotEmpty(s)
if err != nil {
return err
}
if _, ok := profileMap[strings.ToLower(s)]; ok {
return ErrDuplicateProfile
}
return nil
},
}
gitNamePrompt := promptui.Prompt{
Label: "Git Username",
Validate: func(s string) error {
err := validateNotEmpty(s)
if err != nil {
return err
}
err = gitconfig.ValidateValue(s)
if err != nil {
return err
}
return nil
},
}
gitEmailPrompt := promptui.Prompt{
Label: "Git Email",
Validate: func(s string) error {
err := validateNotEmpty(s)
if err != nil {
return err
}
_, err = mail.ParseAddress(s)
if err != nil {
return ErrInvalidEmail
}
err = gitconfig.ValidateValue(s)
if err != nil {
return err
}
return nil
},
}
gitWithSigningKeyPrompt := promptui.Prompt{
Label: "Add Signing Key",
IsConfirm: true,
}
gitGPGFormatSelect := promptui.Select{
Label: "Select Key Format",
Items: gpgFormat,
HideHelp: true,
}
profile.Name, err = profileNamePrompt.Run()
if err != nil {
return Profile{}, err
}
name, err := gitNamePrompt.Run()
if err != nil {
return Profile{}, err
}
err = profile.Config.Set("user.name", name)
if err != nil {
return Profile{}, err
}
email, err := gitEmailPrompt.Run()
if err != nil {
return Profile{}, err
}
err = profile.Config.Set("user.email", email)
if err != nil {
return Profile{}, err
}
_, err = gitWithSigningKeyPrompt.Run()
if err == nil {
ix, _, err := gitGPGFormatSelect.Run()
if err != nil {
return Profile{}, err
}
keyFormat := gpgFormat[ix]
signingKey, err := getSigningKeyPrompt(keyFormat).Run()
if err != nil {
return Profile{}, err
}
err = profile.Config.Set("gpg.format", string(keyFormat))
if err != nil {
return Profile{}, err
}
err = profile.Config.Set("user.signingKey", signingKey)
if err != nil {
return Profile{}, err
}
err = profile.Config.Set("commit.gpgsign", "true")
if err != nil {
return Profile{}, err
}
}
return profile, nil
}
func displayProfileSelector(profiles []Profile) (Profile, error) {
keys := &promptui.SelectKeys{
Prev: promptui.Key{Code: promptui.KeyPrev, Display: promptui.KeyPrevDisplay},
Next: promptui.Key{Code: promptui.KeyNext, Display: promptui.KeyNextDisplay},
PageUp: promptui.Key{Code: promptui.KeyBackward, Display: promptui.KeyBackwardDisplay},
PageDown: promptui.Key{Code: promptui.KeyForward, Display: promptui.KeyForwardDisplay},
Search: promptui.Key{Code: 3},
}
prompt := promptui.Select{
Label: "Profile",
Items: profiles,
Size: 5,
Keys: keys,
Searcher: func(input string, index int) bool {
profile := strings.ReplaceAll(profiles[index].Name, " ", "")
input = strings.TrimSpace(strings.ReplaceAll(input, " ", ""))
return strings.Contains(strings.ToLower(profile), strings.ToLower(input))
},
HideHelp: true,
Templates: &promptui.SelectTemplates{
Label: "Please select one of the available profiles",
Active: "> {{ .Name | cyan }}\t{{ if .IsActive }}{{ \"active\" | green }} {{ end }}",
Inactive: " {{ .Name | blue }}\t{{ if .IsActive }}{{ \"active\" | green }} {{ end }}",
Selected: "> {{ .Name | cyan }}",
},
StartInSearchMode: true,
}
ix, _, err := prompt.Run()
if err != nil {
return Profile{}, err
}
return profiles[ix], nil
}
func displayProfileList(profiles []Profile) error {
tw := tabwriter.NewWriter(os.Stdout, 4, 4, 0, ' ', 0)
_, err := fmt.Fprint(tw, "List of available profiles:\n")
if err != nil {
return err
}
for i, profile := range profiles {
fmt.Fprintf(tw, "%d.\tName: %s ", i+1, profile.Name)
if profile.IsActive {
fmt.Fprint(tw, promptui.Styler(promptui.FGGreen)("(active)"))
}
fmt.Fprint(tw, "\n")
fmt.Fprintf(tw, "\tPath: %s\n", filepath.Join(saveDirPath, profile.DirName))
}
err = tw.Flush()
if err != nil {
return err
}
return nil
}
func displayDeleteConfirmation() bool {
deletePrompt := promptui.Prompt{
Label: "You're about to delete a GLOBAL config file, do you want to proceed",
IsConfirm: true,
}
_, err := deletePrompt.Run()
if err != nil {
return false
}
return err == nil
}
func getSigningKeyPrompt(keyFormat GPGFormat) *promptui.Prompt {
prompt := new(promptui.Prompt)
switch keyFormat {
case OPENPGP:
prompt.Label = "Enter your GPG key"
prompt.Validate = func(s string) error {
err := validateNotEmpty(s)
if err != nil {
return err
}
err = gitconfig.ValidateValue(s)
if err != nil {
return err
}
return nil
}
case SSH:
prompt.Label = "Enter path to your public key"
prompt.Validate = func(s string) error {
err := validateNotEmpty(s)
if err != nil {
return err
}
err = gitconfig.ValidateValue(s)
if err != nil {
return err
}
if filepath.Ext(s) != ".pub" {
return ErrInvalidPublicKeyExt
}
content, err := os.ReadFile(s)
if err != nil {
return err
}
_, _, _, _, err = ssh.ParseAuthorizedKey(content)
if err != nil {
return err
}
return nil
}
case X509:
prompt.Label = "Enter your certificate ID"
prompt.Validate = func(s string) error {
err := validateNotEmpty(s)
if err != nil {
return err
}
err = gitconfig.ValidateValue(s)
if err != nil {
return err
}
return nil
}
}
return prompt
}