hub/commands/create.go

88 строки
2.5 KiB
Go
Исходник Обычный вид История

2013-07-09 22:03:07 +04:00
package commands
2013-07-10 01:36:02 +04:00
import (
"fmt"
"github.com/jingweno/gh/git"
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
)
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() {
cmdCreate.Flag.BoolVar(&flagCreatePrivate, "p", false, "PRIVATE")
cmdCreate.Flag.StringVar(&flagCreateDescription, "d", "", "DESCRIPTION")
cmdCreate.Flag.StringVar(&flagCreateHomepage, "h", "", "HOMEPAGE")
}
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-07-11 03:29:26 +04:00
var (
name string
err error
)
2013-07-10 01:36:02 +04:00
if args.IsParamsEmpty() {
2013-07-11 03:29:26 +04:00
name, err = utils.DirName()
2013-07-10 01:36:02 +04:00
utils.Check(err)
} else {
2013-07-11 03:29:26 +04:00
name = args.FirstParam()
2013-07-10 01:36:02 +04:00
}
var msg string
project := github.NewProjectFromOwnerAndName("", name)
2013-07-11 03:29:26 +04:00
gh := github.NewWithoutProject()
2013-07-10 01:36:02 +04:00
if gh.IsRepositoryExist(project) {
fmt.Printf("%s already exists on %s\n", project, github.GitHubHost)
2013-10-22 01:19:12 +04:00
msg = "set remote origin"
2013-07-10 01:36:02 +04:00
} else {
if !args.Noop {
repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate)
utils.Check(err)
project = github.NewProjectFromOwnerAndName(repo.FullName, "")
2013-07-10 01:36:02 +04:00
}
2013-07-11 03:29:26 +04:00
msg = "created repository"
2013-07-10 01:36:02 +04:00
}
remote, _ := git.OriginRemote()
if remote == nil {
url := project.GitURL("", "", true)
args.Replace("git", "remote", "add", "-f", "origin", url)
} else {
args.Replace("git", "remote", "-v")
}
args.After("echo", fmt.Sprintf("%s:", msg), project.String())
}