-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
598 lines (540 loc) · 13 KB
/
Copy pathconfig.go
File metadata and controls
598 lines (540 loc) · 13 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
type platformConfig struct {
Token string
Host string
Email string
}
type config struct {
Email string
GitHub platformConfig
GitLab platformConfig
Gitea platformConfig
Forgejo platformConfig
}
func configPath() string {
home, err := os.UserHomeDir()
if err != nil {
return ""
}
return filepath.Join(home, ".config", "commitdog", "config.toml")
}
func loadConfig() config {
f, err := os.Open(configPath())
if err != nil {
return config{}
}
defer f.Close()
c := config{}
section := ""
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
section = strings.ToLower(line[1 : len(line)-1])
continue
}
key, val, ok := parseKV(line)
if !ok {
continue
}
switch section {
case "":
if key == "email" {
c.Email = val
}
case "github":
switch key {
case "token":
c.GitHub.Token = val
case "email":
c.GitHub.Email = val
}
case "gitlab":
switch key {
case "token":
c.GitLab.Token = val
case "host":
c.GitLab.Host = val
case "email":
c.GitLab.Email = val
}
case "gitea":
switch key {
case "token":
c.Gitea.Token = val
case "host":
c.Gitea.Host = val
case "email":
c.Gitea.Email = val
}
case "forgejo":
switch key {
case "token":
c.Forgejo.Token = val
case "host":
c.Forgejo.Host = val
case "email":
c.Forgejo.Email = val
}
}
}
return c
}
func parseKV(line string) (string, string, bool) {
parts := strings.SplitN(line, " = ", 2)
if len(parts) != 2 {
return "", "", false
}
return strings.TrimSpace(parts[0]), strings.Trim(strings.TrimSpace(parts[1]), "\""), true
}
func saveConfig(c config) error {
path := configPath()
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return err
}
c.Email = sanitizeInput(c.Email)
c.GitHub.Token = sanitizeInput(c.GitHub.Token)
c.GitHub.Email = sanitizeInput(c.GitHub.Email)
c.GitLab.Token = sanitizeInput(c.GitLab.Token)
c.GitLab.Host = sanitizeInput(c.GitLab.Host)
c.GitLab.Email = sanitizeInput(c.GitLab.Email)
c.Gitea.Token = sanitizeInput(c.Gitea.Token)
c.Gitea.Host = sanitizeInput(c.Gitea.Host)
c.Gitea.Email = sanitizeInput(c.Gitea.Email)
c.Forgejo.Token = sanitizeInput(c.Forgejo.Token)
c.Forgejo.Host = sanitizeInput(c.Forgejo.Host)
c.Forgejo.Email = sanitizeInput(c.Forgejo.Email)
var sb strings.Builder
sb.WriteString(fmt.Sprintf("email = \"%s\"\n", c.Email))
if c.GitHub.Token != "" {
sb.WriteString("\n[github]\n")
sb.WriteString(fmt.Sprintf("token = \"%s\"\n", c.GitHub.Token))
if c.GitHub.Email != "" {
sb.WriteString(fmt.Sprintf("email = \"%s\"\n", c.GitHub.Email))
}
}
if c.GitLab.Token != "" {
sb.WriteString("\n[gitlab]\n")
sb.WriteString(fmt.Sprintf("token = \"%s\"\n", c.GitLab.Token))
if c.GitLab.Host != "" {
sb.WriteString(fmt.Sprintf("host = \"%s\"\n", c.GitLab.Host))
}
if c.GitLab.Email != "" {
sb.WriteString(fmt.Sprintf("email = \"%s\"\n", c.GitLab.Email))
}
}
if c.Gitea.Token != "" {
sb.WriteString("\n[gitea]\n")
sb.WriteString(fmt.Sprintf("token = \"%s\"\n", c.Gitea.Token))
if c.Gitea.Host != "" {
sb.WriteString(fmt.Sprintf("host = \"%s\"\n", c.Gitea.Host))
}
if c.Gitea.Email != "" {
sb.WriteString(fmt.Sprintf("email = \"%s\"\n", c.Gitea.Email))
}
}
if c.Forgejo.Token != "" {
sb.WriteString("\n[forgejo]\n")
sb.WriteString(fmt.Sprintf("token = \"%s\"\n", c.Forgejo.Token))
if c.Forgejo.Host != "" {
sb.WriteString(fmt.Sprintf("host = \"%s\"\n", c.Forgejo.Host))
}
if c.Forgejo.Email != "" {
sb.WriteString(fmt.Sprintf("email = \"%s\"\n", c.Forgejo.Email))
}
}
return os.WriteFile(path, []byte(sb.String()), 0600)
}
func emailForPlatform(c config, platform string) string {
switch platform {
case "gitlab":
if c.GitLab.Email != "" {
return c.GitLab.Email
}
case "gitea":
if c.Gitea.Email != "" {
return c.Gitea.Email
}
case "forgejo":
if c.Forgejo.Email != "" {
return c.Forgejo.Email
}
default:
if c.GitHub.Email != "" {
return c.GitHub.Email
}
}
return c.Email
}
func tokenForPlatform(c config, platform string) string {
switch platform {
case "gitlab":
return c.GitLab.Token
case "gitea":
return c.Gitea.Token
case "forgejo":
return c.Forgejo.Token
default:
return c.GitHub.Token
}
}
func runSetup() {
fmt.Println()
existing := loadConfig()
fmt.Println(" which platform token do you want to configure?")
fmt.Println()
fmt.Println(" 1 github")
fmt.Println(" 2 gitlab")
fmt.Println(" 3 gitea")
fmt.Println(" 4 forgejo")
fmt.Println(" 5 remove a platform")
fmt.Println()
fmt.Printf(" [1/2/3/4/5] pick › ")
platform := ""
for {
switch strings.TrimSpace(readLine()) {
case "1":
platform = "github"
case "2":
platform = "gitlab"
case "3":
platform = "gitea"
case "4":
platform = "forgejo"
case "5":
removePlatformFromConfig()
return
default:
fmt.Printf(" [1/2/3/4/5] pick › ")
continue
}
break
}
fmt.Println()
setupEmail(&existing, platform)
setupToken(&existing, platform)
if err := saveConfig(existing); err != nil {
fatal("could not save config: %v", err)
}
fmt.Println()
fmt.Printf(" ✓ saved to %s\n", configPath())
fmt.Println()
}
func setupEmail(c *config, platform string) {
var current string
switch platform {
case "gitlab":
current = c.GitLab.Email
case "gitea":
current = c.Gitea.Email
case "forgejo":
current = c.Forgejo.Email
default:
current = c.GitHub.Email
}
if current == "" {
current = c.Email
}
if current != "" {
fmt.Printf(" email for %s: %s\n", platform, current)
fmt.Printf(" change it? [y/N] › ")
if strings.ToLower(readLine()) != "y" {
fmt.Println()
return
}
}
fmt.Println()
fmt.Printf(" email for %s › ", platform)
for {
input := sanitizeInput(readLine())
if input == "" {
fmt.Printf(" email cannot be empty › ")
continue
}
if !strings.Contains(input, "@") {
fmt.Printf(" doesn't look like an email › ")
continue
}
switch platform {
case "gitlab":
c.GitLab.Email = input
case "gitea":
c.Gitea.Email = input
case "forgejo":
c.Forgejo.Email = input
default:
c.GitHub.Email = input
c.Email = input
}
break
}
fmt.Println()
}
func setupToken(c *config, platform string) {
var current string
switch platform {
case "gitlab":
current = c.GitLab.Token
case "gitea":
current = c.Gitea.Token
default:
current = c.GitHub.Token
}
if current != "" {
fmt.Printf(" %s token already saved.\n", platform)
fmt.Printf(" replace it? [y/N] › ")
if strings.ToLower(readLine()) != "y" {
return
}
fmt.Println()
}
switch platform {
case "github":
fmt.Println(" enter your GitHub personal access token.")
fmt.Println(" create one at: github.com/settings/tokens")
fmt.Println(" required scopes: repo, write:org")
case "gitlab":
fmt.Println(" enter your GitLab personal access token.")
fmt.Println(" create one at: gitlab.com/-/profile/personal_access_tokens")
fmt.Println(" required scopes: api")
case "gitea":
fmt.Println(" enter your Gitea personal access token.")
fmt.Println(" create one under: settings → applications")
case "forgejo":
fmt.Println(" enter your Forgejo personal access token.")
fmt.Println(" create one under: settings → applications")
}
fmt.Println()
fmt.Printf(" token › ")
for {
input := sanitizeInput(readLine())
if input == "" {
fmt.Printf(" token cannot be empty › ")
continue
}
switch platform {
case "github":
if !strings.HasPrefix(input, "ghp_") && !strings.HasPrefix(input, "github_pat_") {
fmt.Printf(" doesn't look like a GitHub token (ghp_ or github_pat_) › ")
continue
}
c.GitHub.Token = input
case "gitlab":
if !strings.HasPrefix(input, "glpat-") {
fmt.Printf(" doesn't look like a GitLab token (glpat-) › ")
continue
}
c.GitLab.Token = input
fmt.Println()
setupHost(&c.GitLab, "https://gitlab.com")
case "gitea":
c.Gitea.Token = input
fmt.Println()
setupHost(&c.Gitea, "")
case "forgejo":
c.Forgejo.Token = input
fmt.Println()
setupHost(&c.Forgejo, "")
}
break
}
fmt.Println()
}
func setupHost(pc *platformConfig, defaultHost string) {
if defaultHost != "" {
fmt.Printf(" self-hosted instance? [enter] for %s or paste URL › ", defaultHost)
input := sanitizeInput(readLine())
if input == "" {
pc.Host = defaultHost
} else {
pc.Host = strings.TrimRight(input, "/")
}
} else {
fmt.Printf(" instance URL (e.g. https://git.yourcompany.com) › ")
pc.Host = strings.TrimRight(sanitizeInput(readLine()), "/")
}
}
func checkFirstRun() {
c := loadConfig()
proj := loadProjectConfig()
platform := proj.platform
if platform == "" {
platform = "github"
}
email := emailForPlatform(c, platform)
if email != "" {
_ = setGitEmailLocal(email)
return
}
if getGitEmail() != "" {
return
}
fmt.Println()
fmt.Println(" git email is not set.")
fmt.Println()
fmt.Printf(" email › ")
for {
input := sanitizeInput(readLine())
if input == "" {
fmt.Printf(" email cannot be empty › ")
continue
}
if !strings.Contains(input, "@") {
fmt.Printf(" doesn't look like an email › ")
continue
}
c.Email = input
break
}
if err := setGitEmail(c.Email); err != nil {
fmt.Printf(" warning: could not set git email: %s\n", err)
} else {
fmt.Printf(" ✓ git email set\n\n")
}
_ = saveConfig(c)
}
func sanitizeInput(s string) string {
s = strings.TrimSpace(s)
s = strings.ReplaceAll(s, "\x00", "")
s = strings.ReplaceAll(s, "\n", "")
s = strings.ReplaceAll(s, "\r", "")
return s
}
func removePlatformFromConfig() {
c := loadConfig()
var configured []string
if c.GitHub.Token != "" {
configured = append(configured, "github")
}
if c.GitLab.Token != "" {
configured = append(configured, "gitlab")
}
if c.Gitea.Token != "" {
configured = append(configured, "gitea")
}
if c.Forgejo.Token != "" {
configured = append(configured, "forgejo")
}
if len(configured) == 0 {
fmt.Println()
fmt.Println(" no platforms configured.")
fmt.Println()
return
}
fmt.Println()
fmt.Println(" remove which platform?")
fmt.Println()
for i, p := range configured {
fmt.Printf(" %d %s\n", i+1, p)
}
fmt.Println()
fmt.Printf(" [1-%d] pick, [q] quit › ", len(configured))
var target string
for {
input := strings.TrimSpace(readLine())
if input == "q" || input == "" {
fmt.Println(" cancelled.")
return
}
for i, p := range configured {
if input == fmt.Sprintf("%d", i+1) {
target = p
break
}
}
if target != "" {
break
}
fmt.Printf(" 1-%d or q › ", len(configured))
}
proj := loadProjectConfig()
isPrimary := proj.effectivePrimary() == target
fmt.Println()
fmt.Printf(" %s removing %s\n\n", colorYellow("⚠"), target)
fmt.Println(" this will:")
fmt.Println(" · delete the token, host, and email for " + target + " from global config")
fmt.Println(" · remove the git remote for " + target + " from this repo")
fmt.Println(" · remove " + target + " from .commitdog in this repo")
if isPrimary {
fmt.Println(" · " + target + " is your primary — you will need to run 'commitdog init' again")
}
fmt.Println()
fmt.Printf(" type 'y' to confirm or 'n' to cancel › ")
for {
input := strings.ToLower(strings.TrimSpace(readLine()))
if input == "y" || input == "yes" {
break
}
if input == "n" || input == "no" {
fmt.Println(" cancelled.")
return
}
fmt.Printf(" type 'y' or 'n' › ")
}
fmt.Println()
switch target {
case "github":
c.GitHub = platformConfig{}
case "gitlab":
c.GitLab = platformConfig{}
case "gitea":
c.Gitea = platformConfig{}
case "forgejo":
c.Forgejo = platformConfig{}
}
if err := saveConfig(c); err != nil {
fmt.Printf(" %s could not save config: %v\n", colorRed("✗"), err)
return
}
fmt.Printf(" %s removed %s from global config\n", colorGreen("✓"), target)
remoteName := platformRemoteName(target)
if isPrimary {
remoteName = "origin"
}
if remoteExists(remoteName) {
exec.Command("git", "remote", "remove", remoteName).Run()
fmt.Printf(" %s remote '%s' removed\n", colorGreen("✓"), remoteName)
}
if isPrimary {
proj.primary = ""
proj.platform = ""
if err := saveProjectConfig(proj); err != nil {
fmt.Printf(" %s could not update .commitdog: %v\n", colorRed("✗"), err)
} else {
fmt.Printf(" %s .commitdog updated\n", colorGreen("✓"))
}
fmt.Println()
fmt.Printf(" %s %s was your primary — run 'commitdog init' to reconfigure\n\n", colorYellow("⚠"), target)
return
}
isMirror := false
for _, m := range proj.mirrors {
if m == target {
isMirror = true
break
}
}
if isMirror {
var updated []string
for _, m := range proj.mirrors {
if m != target {
updated = append(updated, m)
}
}
proj.mirrors = updated
if err := saveProjectConfig(proj); err != nil {
fmt.Printf(" %s could not update .commitdog: %v\n", colorRed("✗"), err)
} else {
fmt.Printf(" %s .commitdog updated\n", colorGreen("✓"))
}
}
fmt.Println()
}