github.com/trevoraustin/hub@v2.2.0-preview1.0.20141105230840-96d8bfc654cc+incompatible/commands/create.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 8 "github.com/github/hub/git" 9 "github.com/github/hub/github" 10 "github.com/github/hub/utils" 11 ) 12 13 var cmdCreate = &Command{ 14 Run: create, 15 Usage: "create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME]", 16 Short: "Create this repository on GitHub and add GitHub as origin", 17 Long: `Create a new public GitHub repository from the current git 18 repository and add remote origin at "git@github.com:USER/REPOSITORY.git"; 19 USER is your GitHub username and REPOSITORY is the current working 20 directory name. To explicitly name the new repository, pass in NAME, 21 optionally in ORGANIZATION/NAME form to create under an organization 22 you're a member of. With -p, create a private repository, and with 23 -d and -h set the repository's description and homepage URL, respectively. 24 `, 25 } 26 27 var ( 28 flagCreatePrivate bool 29 flagCreateDescription, flagCreateHomepage string 30 ) 31 32 func init() { 33 cmdCreate.Flag.BoolVarP(&flagCreatePrivate, "private", "p", false, "PRIVATE") 34 cmdCreate.Flag.StringVarP(&flagCreateDescription, "description", "d", "", "DESCRIPTION") 35 cmdCreate.Flag.StringVarP(&flagCreateHomepage, "homepage", "h", "", "HOMEPAGE") 36 37 CmdRunner.Use(cmdCreate) 38 } 39 40 /* 41 $ gh create 42 ... create repo on github ... 43 > git remote add -f origin git@github.com:YOUR_USER/CURRENT_REPO.git 44 45 # with description: 46 $ gh create -d 'It shall be mine, all mine!' 47 48 $ gh create recipes 49 [ repo created on GitHub ] 50 > git remote add origin git@github.com:YOUR_USER/recipes.git 51 52 $ gh create sinatra/recipes 53 [ repo created in GitHub organization ] 54 > git remote add origin git@github.com:sinatra/recipes.git 55 */ 56 func create(command *Command, args *Args) { 57 _, err := git.Dir() 58 if err != nil { 59 err = fmt.Errorf("'create' must be run from inside a git repository") 60 utils.Check(err) 61 } 62 63 var newRepoName string 64 if args.IsParamsEmpty() { 65 newRepoName, err = utils.DirName() 66 utils.Check(err) 67 } else { 68 reg := regexp.MustCompile("^[^-]") 69 if !reg.MatchString(args.FirstParam()) { 70 err = fmt.Errorf("invalid argument: %s", args.FirstParam()) 71 utils.Check(err) 72 } 73 newRepoName = args.FirstParam() 74 } 75 76 config := github.CurrentConfig() 77 host, err := config.DefaultHost() 78 if err != nil { 79 utils.Check(github.FormatError("creating repository", err)) 80 } 81 82 owner := host.User 83 if strings.Contains(newRepoName, "/") { 84 split := strings.SplitN(newRepoName, "/", 2) 85 owner = split[0] 86 newRepoName = split[1] 87 } 88 89 project := github.NewProject(owner, newRepoName, host.Host) 90 gh := github.NewClient(project.Host) 91 92 var action string 93 if gh.IsRepositoryExist(project) { 94 fmt.Printf("%s already exists on %s\n", project, project.Host) 95 action = "set remote origin" 96 } else { 97 action = "created repository" 98 if !args.Noop { 99 repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate) 100 utils.Check(err) 101 project = github.NewProject(repo.FullName, "", project.Host) 102 } 103 } 104 105 localRepo, err := github.LocalRepo() 106 utils.Check(err) 107 108 remote, _ := localRepo.OriginRemote() 109 if remote == nil { 110 url := project.GitURL("", "", true) 111 args.Replace("git", "remote", "add", "-f", "origin", url) 112 } else { 113 args.Replace("git", "remote", "-v") 114 } 115 116 args.After("echo", fmt.Sprintf("%s:", action), project.String()) 117 }