-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.go
More file actions
459 lines (399 loc) · 12.2 KB
/
Copy pathcli.go
File metadata and controls
459 lines (399 loc) · 12.2 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/posener/complete/v2"
"github.com/posener/complete/v2/install"
"github.com/posener/complete/v2/predict"
)
func predictIdentities(prefix string) []string {
identities := getAllIdentities()
var suggestions []string
for _, identity := range identities {
if identity.Nickname != "" {
suggestions = append(suggestions, identity.Nickname)
}
suggestions = append(suggestions, identity.Name)
suggestions = append(suggestions, identity.Email)
}
return suggestions
}
func setupCompletion() {
cmd := &complete.Command{
Sub: map[string]*complete.Command{
"list": {},
"current": {},
"switch": {Args: complete.PredictFunc(predictIdentities)},
"use": {Args: complete.PredictFunc(predictIdentities)},
"add": {},
"delete": {Args: complete.PredictFunc(predictIdentities)},
"nickname": {Args: complete.PredictFunc(predictIdentities)},
"repo": {
Sub: map[string]*complete.Command{
"current": {},
"use": {Args: complete.PredictFunc(predictIdentities)},
"add": {},
},
},
"completion": {
Sub: map[string]*complete.Command{
"upgrade": {},
},
Args: predict.Set{"bash", "zsh", "fish"},
Flags: map[string]complete.Predictor{"r": predict.Nothing},
},
"help": {},
},
Flags: map[string]complete.Predictor{
"h": predict.Nothing,
"help": predict.Nothing,
},
}
cmd.Complete("gitid")
}
func handleCLICommand(args []string) error {
if len(args) == 0 {
return fmt.Errorf("no command provided")
}
command := args[0]
switch command {
case "list":
return listIdentitiesCLI()
case "current":
return getCurrentIdentityCLI()
case "switch", "use":
if len(args) < 2 {
return fmt.Errorf("usage: gitid %s <identifier>", command)
}
return switchIdentityCLI(args[1])
case "add":
if len(args) < 3 {
return fmt.Errorf("usage: gitid add <name> <email> [nickname]")
}
nickname := ""
if len(args) > 3 {
nickname = args[3]
}
return addIdentityCLI(args[1], args[2], nickname)
case "delete":
if len(args) < 2 {
return fmt.Errorf("usage: gitid delete <identifier>")
}
return deleteIdentityCLI(args[1])
case "nickname":
if len(args) < 3 {
return fmt.Errorf("usage: gitid nickname <identifier> <nickname>")
}
return setNicknameCLI(args[1], args[2])
case "completion":
return completionCLI(args[1:])
case "repo":
if len(args) < 2 {
return fmt.Errorf("usage: gitid repo <current|use|add> [identifier]")
}
return repoCLI(args[1:])
case "help", "--help", "-h":
showHelp()
return nil
default:
return fmt.Errorf("unknown command: %s\nRun 'gitid help' for usage information", command)
}
}
func listIdentitiesCLI() error {
identities := getAllIdentities()
if len(identities) == 0 {
fmt.Println("No identities configured.")
return nil
}
var localName, localEmail string
hasLocal := hasLocalIdentity()
if hasLocal {
localName, localEmail, _ = getCurrentLocalIdentity()
}
for _, identity := range identities {
nickname := identity.Nickname
if nickname == "" {
nickname = "-"
}
// Check if this identity is the current local identity
isLocal := hasLocal && identity.Name == localName && identity.Email == localEmail
localIndicator := ""
if isLocal {
localIndicator = " [current local]"
}
fmt.Printf("%-12s %s <%s>%s\n", nickname, identity.Name, identity.Email, localIndicator)
}
if isInGitRepository() && !hasLocal {
fmt.Printf("\n(In git repository using global identity)\n")
}
return nil
}
func getCurrentIdentityCLI() error {
nameOut, err := exec.Command("git", "config", "--global", "user.name").Output()
if err != nil {
return fmt.Errorf("no global git identity configured")
}
emailOut, err := exec.Command("git", "config", "--global", "user.email").Output()
if err != nil {
return fmt.Errorf("no global git identity configured")
}
name := strings.TrimSpace(string(nameOut))
email := strings.TrimSpace(string(emailOut))
nickname := getNickname(email)
if nickname != "" {
fmt.Printf("Global: %s (%s <%s>)\n", nickname, name, email)
} else {
fmt.Printf("Global: %s <%s>\n", name, email)
}
// If we're in a git repository and have a local identity, show it too
if hasLocalIdentity() {
localName, localEmail, err := getCurrentLocalIdentity()
if err == nil {
localNickname := getNickname(localEmail)
if localNickname != "" {
fmt.Printf("Local: %s (%s <%s>)\n", localNickname, localName, localEmail)
} else {
fmt.Printf("Local: %s <%s>\n", localName, localEmail)
}
}
} else if isInGitRepository() {
fmt.Printf("Local: (using global identity)\n")
}
return nil
}
func switchIdentityCLI(identifier string) error {
identity, found := findIdentityByIdentifier(identifier)
if !found {
return fmt.Errorf("identity not found: %s", identifier)
}
switchIdentity(identity.Name, identity.Email)
display := getIdentityDisplay(identity)
fmt.Printf("Switched to %s\n", display)
return nil
}
func addIdentityCLI(name, email, nickname string) error {
if err := addIdentity(name, email, nickname); err != nil {
return err
}
identity := Identity{Name: name, Email: email, Nickname: nickname}
display := getIdentityDisplay(identity)
fmt.Printf("Added identity: %s\n", display)
return nil
}
func deleteIdentityCLI(identifier string) error {
identity, found := findIdentityByIdentifier(identifier)
if !found {
return fmt.Errorf("identity not found: %s", identifier)
}
if err := deleteIdentity(identity.Email); err != nil {
return err
}
display := getIdentityDisplay(identity)
fmt.Printf("Deleted identity: %s\n", display)
return nil
}
func setNicknameCLI(identifier, nickname string) error {
identity, found := findIdentityByIdentifier(identifier)
if !found {
return fmt.Errorf("identity not found: %s", identifier)
}
if err := setNickname(identity.Email, nickname); err != nil {
return err
}
fmt.Printf("Set nickname \"%s\" for %s <%s>\n", nickname, identity.Name, identity.Email)
return nil
}
func completionCLI(args []string) error {
if len(args) == 0 {
return fmt.Errorf("usage: gitid completion <shell> [-r] | gitid completion upgrade [shell]\nSupported shells: bash, zsh, fish")
}
// Handle upgrade subcommand
if args[0] == "upgrade" {
return upgradeCompletionCLI(args[1:])
}
shell := args[0]
remove := false
// Check for -r flag
if len(args) > 1 && args[1] == "-r" {
remove = true
}
// Validate shell
validShells := map[string]bool{"bash": true, "zsh": true, "fish": true}
if !validShells[shell] {
return fmt.Errorf("unsupported shell: %s\nSupported shells: bash, zsh, fish", shell)
}
if remove {
if !install.IsInstalled("gitid") {
fmt.Printf("Completion not installed for %s\n", shell)
return nil
}
if err := install.Uninstall("gitid"); err != nil {
return fmt.Errorf("failed to uninstall completion: %v", err)
}
fmt.Printf("Successfully removed completion for %s\n", shell)
} else {
if install.IsInstalled("gitid") {
fmt.Printf("Completion already installed for %s\n", shell)
return nil
}
if err := install.Install("gitid"); err != nil {
return fmt.Errorf("failed to install completion: %v", err)
}
fmt.Printf("Successfully installed completion for %s\n", shell)
fmt.Println("Please restart your shell or run: source ~/.bashrc (or ~/.zshrc)")
}
return nil
}
func upgradeCompletionCLI(args []string) error {
var shell string
// If shell is provided as argument, use it; otherwise detect current shell
if len(args) > 0 {
shell = args[0]
} else {
shell = detectCurrentShell()
if shell == "" {
return fmt.Errorf("could not detect current shell. Please specify shell: gitid completion upgrade <shell>\nSupported shells: bash, zsh, fish")
}
}
// Validate shell
validShells := map[string]bool{"bash": true, "zsh": true, "fish": true}
if !validShells[shell] {
return fmt.Errorf("unsupported shell: %s\nSupported shells: bash, zsh, fish", shell)
}
// Check if completion is currently installed
if !install.IsInstalled("gitid") {
fmt.Printf("Completion not currently installed for %s. Installing...\n", shell)
if err := install.Install("gitid"); err != nil {
return fmt.Errorf("failed to install completion: %v", err)
}
fmt.Printf("Successfully installed completion for %s\n", shell)
fmt.Println("Please restart your shell or run: source ~/.bashrc (or ~/.zshrc)")
return nil
}
// Remove existing completion
fmt.Printf("Removing existing completion for %s...\n", shell)
if err := install.Uninstall("gitid"); err != nil {
return fmt.Errorf("failed to remove existing completion: %v", err)
}
// Reinstall completion
fmt.Printf("Reinstalling completion for %s...\n", shell)
if err := install.Install("gitid"); err != nil {
return fmt.Errorf("failed to reinstall completion: %v", err)
}
fmt.Printf("Successfully upgraded completion for %s\n", shell)
fmt.Println("Please restart your shell or run: source ~/.bashrc (or ~/.zshrc)")
return nil
}
func detectCurrentShell() string {
shell := os.Getenv("SHELL")
if shell == "" {
return ""
}
shellName := filepath.Base(shell)
switch shellName {
case "bash":
return "bash"
case "zsh":
return "zsh"
case "fish":
return "fish"
default:
return ""
}
}
func repoCLI(args []string) error {
if len(args) == 0 {
return fmt.Errorf("usage: gitid repo <current|use|add> [args]")
}
subcommand := args[0]
switch subcommand {
case "current":
return getCurrentLocalIdentityCLI()
case "use":
if len(args) < 2 {
return fmt.Errorf("usage: gitid repo use <identifier>")
}
return useLocalIdentityCLI(args[1])
case "add":
if len(args) < 3 {
return fmt.Errorf("usage: gitid repo add <name> <email> [nickname]")
}
nickname := ""
if len(args) > 3 {
nickname = args[3]
}
return addLocalIdentityCLI(args[1], args[2], nickname)
default:
return fmt.Errorf("unknown repo subcommand: %s", subcommand)
}
}
func getCurrentLocalIdentityCLI() error {
name, email, err := getCurrentLocalIdentity()
if err != nil {
return err
}
nickname := getNickname(email)
if nickname != "" {
fmt.Printf("%s (%s <%s>) [local]\n", nickname, name, email)
} else {
fmt.Printf("%s <%s> [local]\n", name, email)
}
return nil
}
func useLocalIdentityCLI(identifier string) error {
if err := setLocalIdentityByIdentifier(identifier); err != nil {
return err
}
identity, found := findIdentityByIdentifier(identifier)
if !found {
return fmt.Errorf("identity not found: %s", identifier)
}
display := getIdentityDisplay(identity)
fmt.Printf("Set local repository identity to %s\n", display)
return nil
}
func addLocalIdentityCLI(name, email, nickname string) error {
if err := addLocalIdentity(name, email, nickname); err != nil {
return err
}
identity := Identity{Name: name, Email: email, Nickname: nickname}
display := getIdentityDisplay(identity)
fmt.Printf("Added and set local repository identity to %s\n", display)
return nil
}
func showHelp() {
fmt.Println(`GitID - Git Identity Manager
USAGE:
gitid Launch interactive TUI
gitid list List all identities
gitid current Show current global git identity
gitid switch <identifier> Switch global identity by nickname, name, or email
gitid use <identifier> Alias for switch
gitid add <name> <email> [nick] Add new identity with optional nickname
gitid delete <identifier> Delete identity
gitid nickname <id> <nickname> Set/update nickname for identity
gitid repo current Show current local repository identity
gitid repo use <identifier> Set local repository identity
gitid repo add <name> <email> [nick] Add and set new local repository identity
gitid completion <shell> Install shell completion (bash/zsh/fish)
gitid completion <shell> -r Remove shell completion
gitid completion upgrade [shell] Upgrade shell completion (remove and reinstall)
gitid help Show this help
EXAMPLES:
gitid list
gitid current
gitid switch work
gitid add "John Doe" "john@company.com" work
gitid nickname john@company.com work
gitid delete work
gitid repo current
gitid repo use work
gitid repo add "Jane Smith" "jane@company.com" work-jane
gitid completion bash
gitid completion zsh -r
gitid completion upgrade
gitid completion upgrade fish`)
}