/
cmd_git_create.go
117 lines (102 loc) · 2.89 KB
/
cmd_git_create.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
package client
import (
"errors"
"fmt"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
)
type CmdGitCreate struct {
libkb.Contextified
repoName keybase1.GitRepoName
teamName keybase1.TeamName
skipNotify bool
}
func newCmdGitCreate(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "create",
ArgumentHelp: "[--team=<team name>] <repo name>",
Usage: "Create a personal or team git repository.",
Description: "`keybase git create reponame` will create a personal git repo.\n `keybase git create --team=treehouse reponame` will create a\n team git repo for the `treehouse` team.",
Action: func(c *cli.Context) {
cmd := NewCmdGitCreateRunner(g)
cl.ChooseCommand(cmd, "create", c)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "team",
Usage: "keybase team name (optional)",
},
cli.BoolFlag{
Name: "skip-notify",
Usage: "skip posting new repo notifications for team members",
},
},
}
}
func NewCmdGitCreateRunner(g *libkb.GlobalContext) *CmdGitCreate {
return &CmdGitCreate{Contextified: libkb.NewContextified(g)}
}
func (c *CmdGitCreate) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) == 0 {
return errors.New("repo name argument required")
}
c.repoName = keybase1.GitRepoName(ctx.Args()[0])
c.skipNotify = ctx.Bool("skip-notify")
if len(ctx.String("team")) > 0 {
teamName, err := keybase1.TeamNameFromString(ctx.String("team"))
if err != nil {
return err
}
c.teamName = teamName
}
return nil
}
func (c *CmdGitCreate) Run() error {
cli, err := GetGitClient(c.G())
if err != nil {
return err
}
var urlString string
if len(c.teamName.String()) > 0 {
urlString, err = c.runTeam(cli)
} else {
urlString, err = c.runPersonal(cli)
}
if err != nil {
return err
}
dui := c.G().UI.GetDumbOutputUI()
dui.Printf(`Repo created! You can clone it with:
git clone %s
Or add it as a remote to an existing repo with:
git remote add origin %s
`, urlString, urlString)
return nil
}
func (c *CmdGitCreate) runPersonal(cli keybase1.GitClient) (string, error) {
if _, err := cli.CreatePersonalRepo(context.Background(), c.repoName); err != nil {
return "", err
}
return fmt.Sprintf("keybase://private/%s/%s", c.G().Env.GetUsername(), c.repoName), nil
}
func (c *CmdGitCreate) runTeam(cli keybase1.GitClient) (string, error) {
arg := keybase1.CreateTeamRepoArg{
TeamName: c.teamName,
RepoName: c.repoName,
NotifyTeam: !c.skipNotify,
}
if _, err := cli.CreateTeamRepo(context.Background(), arg); err != nil {
return "", err
}
return fmt.Sprintf("keybase://team/%s/%s", c.teamName, c.repoName), nil
}
func (c *CmdGitCreate) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}