Skip to content

Commit 9175fc0

Browse files
committed
Add preliminary repo create command
1 parent d4012b1 commit 9175fc0

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

command/repo.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package command
33
import (
44
"fmt"
55
"os"
6+
"path"
67
"strings"
78

89
"github.com/cli/cli/git"
@@ -14,6 +15,8 @@ import (
1415
func init() {
1516
RootCmd.AddCommand(repoCmd)
1617
repoCmd.AddCommand(repoCloneCmd)
18+
repoCmd.AddCommand(repoCreateCmd)
19+
repoCreateCmd.Flags().Bool("public", false, "Make the new repository public")
1720
repoCmd.AddCommand(repoViewCmd)
1821
}
1922

@@ -37,6 +40,13 @@ To pass 'git clone' options, separate them with '--'.`,
3740
RunE: repoClone,
3841
}
3942

43+
var repoCreateCmd = &cobra.Command{
44+
Use: "create [<name>]",
45+
Short: "Create a new repository",
46+
Long: `Create a new GitHub repository.`,
47+
RunE: repoCreate,
48+
}
49+
4050
var repoViewCmd = &cobra.Command{
4151
Use: "view [<repo>]",
4252
Short: "View a repository in the browser",
@@ -63,6 +73,61 @@ func repoClone(cmd *cobra.Command, args []string) error {
6373
return utils.PrepareCmd(cloneCmd).Run()
6474
}
6575

76+
func repoCreate(cmd *cobra.Command, args []string) error {
77+
var name string
78+
if len(args) > 0 {
79+
name = args[0]
80+
} else {
81+
dir, err := git.ToplevelDir()
82+
if err != nil {
83+
return err
84+
}
85+
name = path.Base(dir)
86+
}
87+
88+
visibility := "PRIVATE"
89+
if isPublic, err := cmd.Flags().GetBool("public"); err == nil && isPublic {
90+
visibility = "PUBLIC"
91+
}
92+
93+
ctx := contextForCommand(cmd)
94+
client, err := apiClientForContext(ctx)
95+
if err != nil {
96+
return err
97+
}
98+
99+
variables := map[string]interface{}{
100+
"input": map[string]interface{}{
101+
"name": name,
102+
"visibility": visibility,
103+
},
104+
}
105+
106+
var response struct {
107+
CreateRepository struct {
108+
Repository struct {
109+
URL string
110+
}
111+
}
112+
}
113+
114+
err = client.GraphQL(`
115+
mutation($input: CreateRepositoryInput!) {
116+
createRepository(input: $input) {
117+
repository {
118+
url
119+
}
120+
}
121+
}
122+
`, variables, &response)
123+
if err != nil {
124+
return err
125+
}
126+
127+
cmd.Println(response.CreateRepository.Repository.URL)
128+
return nil
129+
}
130+
66131
func repoView(cmd *cobra.Command, args []string) error {
67132
ctx := contextForCommand(cmd)
68133

0 commit comments

Comments
 (0)