2013-07-09 22:03:07 +04:00
|
|
|
package commands
|
|
|
|
|
2013-07-10 01:36:02 +04:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-03-10 01:42:06 +04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2014-02-10 20:22:36 +04:00
|
|
|
"github.com/github/hub/git"
|
|
|
|
"github.com/github/hub/github"
|
|
|
|
"github.com/github/hub/utils"
|
2013-07-10 01:36:02 +04:00
|
|
|
)
|
|
|
|
|
2013-07-09 22:03:07 +04:00
|
|
|
var cmdCreate = &Command{
|
|
|
|
Run: create,
|
2013-07-10 01:36:02 +04:00
|
|
|
Usage: "create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME]",
|
2013-07-09 22:03:07 +04:00
|
|
|
Short: "Create this repository on GitHub and add GitHub as origin",
|
|
|
|
Long: `Create a new public GitHub repository from the current git
|
|
|
|
repository and add remote origin at "git@github.com:USER/REPOSITORY.git";
|
|
|
|
USER is your GitHub username and REPOSITORY is the current working
|
|
|
|
directory name. To explicitly name the new repository, pass in NAME,
|
|
|
|
optionally in ORGANIZATION/NAME form to create under an organization
|
|
|
|
you're a member of. With -p, create a private repository, and with
|
|
|
|
-d and -h set the repository's description and homepage URL, respectively.
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-07-10 01:36:02 +04:00
|
|
|
var (
|
|
|
|
flagCreatePrivate bool
|
|
|
|
flagCreateDescription, flagCreateHomepage string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2013-12-30 03:07:58 +04:00
|
|
|
cmdCreate.Flag.BoolVarP(&flagCreatePrivate, "private", "p", false, "PRIVATE")
|
|
|
|
cmdCreate.Flag.StringVarP(&flagCreateDescription, "description", "d", "", "DESCRIPTION")
|
|
|
|
cmdCreate.Flag.StringVarP(&flagCreateHomepage, "homepage", "h", "", "HOMEPAGE")
|
2013-12-30 02:18:14 +04:00
|
|
|
|
|
|
|
CmdRunner.Use(cmdCreate)
|
2013-07-10 01:36:02 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 22:03:07 +04:00
|
|
|
/*
|
|
|
|
$ gh create
|
|
|
|
... create repo on github ...
|
|
|
|
> git remote add -f origin git@github.com:YOUR_USER/CURRENT_REPO.git
|
|
|
|
|
|
|
|
# with description:
|
|
|
|
$ gh create -d 'It shall be mine, all mine!'
|
|
|
|
|
|
|
|
$ gh create recipes
|
|
|
|
[ repo created on GitHub ]
|
|
|
|
> git remote add origin git@github.com:YOUR_USER/recipes.git
|
|
|
|
|
|
|
|
$ gh create sinatra/recipes
|
|
|
|
[ repo created in GitHub organization ]
|
|
|
|
> git remote add origin git@github.com:sinatra/recipes.git
|
|
|
|
*/
|
|
|
|
func create(command *Command, args *Args) {
|
2013-12-10 10:47:15 +04:00
|
|
|
_, err := git.Dir()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("'create' must be run from inside a git repository")
|
|
|
|
utils.Check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var newRepoName string
|
2013-07-10 01:36:02 +04:00
|
|
|
if args.IsParamsEmpty() {
|
2013-12-10 10:47:15 +04:00
|
|
|
newRepoName, err = utils.DirName()
|
2013-07-10 01:36:02 +04:00
|
|
|
utils.Check(err)
|
|
|
|
} else {
|
2013-12-10 10:47:15 +04:00
|
|
|
reg := regexp.MustCompile("^[^-]")
|
|
|
|
if !reg.MatchString(args.FirstParam()) {
|
|
|
|
err = fmt.Errorf("invalid argument: %s", args.FirstParam())
|
|
|
|
utils.Check(err)
|
|
|
|
}
|
|
|
|
newRepoName = args.FirstParam()
|
2013-07-10 01:36:02 +04:00
|
|
|
}
|
|
|
|
|
2013-12-10 10:47:15 +04:00
|
|
|
configs := github.CurrentConfigs()
|
2014-03-10 01:42:06 +04:00
|
|
|
credentials := configs.DefaultCredential()
|
2013-12-10 10:47:15 +04:00
|
|
|
|
|
|
|
owner := credentials.User
|
|
|
|
if strings.Contains(newRepoName, "/") {
|
|
|
|
split := strings.SplitN(newRepoName, "/", 2)
|
|
|
|
owner = split[0]
|
|
|
|
newRepoName = split[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
project := github.NewProject(owner, newRepoName, credentials.Host)
|
2013-12-17 19:45:48 +04:00
|
|
|
gh := github.NewClient(project.Host)
|
2013-12-10 10:47:15 +04:00
|
|
|
|
|
|
|
var action string
|
2013-07-10 01:36:02 +04:00
|
|
|
if gh.IsRepositoryExist(project) {
|
2013-12-10 10:47:15 +04:00
|
|
|
fmt.Printf("%s already exists on %s\n", project, project.Host)
|
|
|
|
action = "set remote origin"
|
2013-07-10 01:36:02 +04:00
|
|
|
} else {
|
2013-12-10 10:47:15 +04:00
|
|
|
action = "created repository"
|
2013-07-10 01:36:02 +04:00
|
|
|
if !args.Noop {
|
|
|
|
repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate)
|
|
|
|
utils.Check(err)
|
2013-12-10 10:47:15 +04:00
|
|
|
project = github.NewProject(repo.FullName, "", project.Host)
|
2013-07-10 01:36:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 13:14:07 +04:00
|
|
|
remote, _ := github.OriginRemote()
|
2013-07-10 01:36:02 +04:00
|
|
|
if remote == nil {
|
|
|
|
url := project.GitURL("", "", true)
|
|
|
|
args.Replace("git", "remote", "add", "-f", "origin", url)
|
|
|
|
} else {
|
|
|
|
args.Replace("git", "remote", "-v")
|
|
|
|
}
|
|
|
|
|
2013-12-10 10:47:15 +04:00
|
|
|
args.After("echo", fmt.Sprintf("%s:", action), project.String())
|
2013-07-10 01:36:02 +04:00
|
|
|
}
|