From 9175fc06d40d8cea9a03a60750e59828ecb36267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 25 Feb 2020 17:58:04 +0100 Subject: [PATCH] Add preliminary `repo create` command --- command/repo.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/command/repo.go b/command/repo.go index 484ef609c..7b8dd2728 100644 --- a/command/repo.go +++ b/command/repo.go @@ -3,6 +3,7 @@ package command import ( "fmt" "os" + "path" "strings" "github.com/cli/cli/git" @@ -14,6 +15,8 @@ import ( func init() { RootCmd.AddCommand(repoCmd) repoCmd.AddCommand(repoCloneCmd) + repoCmd.AddCommand(repoCreateCmd) + repoCreateCmd.Flags().Bool("public", false, "Make the new repository public") repoCmd.AddCommand(repoViewCmd) } @@ -37,6 +40,13 @@ To pass 'git clone' options, separate them with '--'.`, RunE: repoClone, } +var repoCreateCmd = &cobra.Command{ + Use: "create []", + Short: "Create a new repository", + Long: `Create a new GitHub repository.`, + RunE: repoCreate, +} + var repoViewCmd = &cobra.Command{ Use: "view []", Short: "View a repository in the browser", @@ -63,6 +73,61 @@ func repoClone(cmd *cobra.Command, args []string) error { return utils.PrepareCmd(cloneCmd).Run() } +func repoCreate(cmd *cobra.Command, args []string) error { + var name string + if len(args) > 0 { + name = args[0] + } else { + dir, err := git.ToplevelDir() + if err != nil { + return err + } + name = path.Base(dir) + } + + visibility := "PRIVATE" + if isPublic, err := cmd.Flags().GetBool("public"); err == nil && isPublic { + visibility = "PUBLIC" + } + + ctx := contextForCommand(cmd) + client, err := apiClientForContext(ctx) + if err != nil { + return err + } + + variables := map[string]interface{}{ + "input": map[string]interface{}{ + "name": name, + "visibility": visibility, + }, + } + + var response struct { + CreateRepository struct { + Repository struct { + URL string + } + } + } + + err = client.GraphQL(` + mutation($input: CreateRepositoryInput!) { + createRepository(input: $input) { + repository { + url + } + } + } + `, variables, &response) + if err != nil { + return err + } + + cmd.Println(response.CreateRepository.Repository.URL) + return nil +} + func repoView(cmd *cobra.Command, args []string) error { ctx := contextForCommand(cmd)