-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathinit.go
78 lines (64 loc) · 1.38 KB
/
init.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
package cmd
import (
"fmt"
"github.com/matsuyoshi30/gitsu/internal/config"
"github.com/matsuyoshi30/gitsu/internal/git"
"github.com/matsuyoshi30/gitsu/internal/models"
"github.com/urfave/cli/v2"
)
func InitCommand() *cli.Command {
return &cli.Command{
Name: "init",
Aliases: []string{"i"},
Usage: "Initialize user config by providing an alias",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Value: false,
Usage: "Set git user globally",
},
},
Action: func(c *cli.Context) error {
alias := c.Args().First()
cfg, err := config.Read()
if err != nil {
return err
}
list := cfg.UserList()
if len(list) == 0 {
fmt.Println("No users")
return nil
}
var scope = models.Local
if c.Bool("global") {
scope = models.Global
}
err = git.IsInsideWorktree(scope)
if err != nil {
return err
}
if alias == "" {
defaultUser, err := cfg.SelectDefaultUser()
if err != nil {
return err
}
err = git.SetConfig(defaultUser, scope)
if err != nil {
return err
}
fmt.Printf("Setting default profile %s", defaultUser.Format(0))
return nil
}
user, err := cfg.SelectUserByAlias(alias)
if err != nil {
return err
}
err = git.SetConfig(user, scope)
if err != nil {
return err
}
fmt.Printf("Setting profile %s", user.Format(0))
return nil
},
}
}