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