hub/commands/create.go

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

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/ui"
2014-02-10 20:22:36 +04:00
"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,
Usage: "create [-poc] [-d <DESCRIPTION>] [-h <HOMEPAGE>] [[<ORGANIZATION>/]<NAME>]",
Long: `Create a new repository on GitHub and add a git remote for it.
## Options:
-p
Create a private repository.
-d <DESCRIPTION>
Use this text as the description of the GitHub repository.
-h <HOMEPAGE>
Use this text as the URL of the GitHub repository.
-o, --browse
Open the new repository in a web browser.
-c, --copy
Put the URL of the new repository to clipboard instead of printing it.
[<ORGANIZATION>/]<NAME>
The name for the repository on GitHub (default: name of the current working
directory).
Optionally, create the repository within <ORGANIZATION>.
## Examples:
$ hub create
[ repo created on GitHub ]
> git remote add -f origin git@github.com:USER/REPO.git
$ hub create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add -f origin git@github.com:sinatra/recipes.git
2016-01-24 18:50:01 +03:00
## See also:
hub-init(1), hub(1)
2013-07-09 22:03:07 +04:00
`,
}
2013-07-10 01:36:02 +04:00
var (
flagCreatePrivate,
flagCreateBrowse,
flagCreateCopy bool
flagCreateDescription,
flagCreateHomepage string
2013-07-10 01:36:02 +04:00
)
func init() {
cmdCreate.Flag.BoolVarP(&flagCreatePrivate, "private", "p", false, "PRIVATE")
cmdCreate.Flag.BoolVarP(&flagCreateBrowse, "browse", "o", false, "BROWSE")
cmdCreate.Flag.BoolVarP(&flagCreateCopy, "copy", "c", false, "COPY")
cmdCreate.Flag.StringVarP(&flagCreateDescription, "description", "d", "", "DESCRIPTION")
cmdCreate.Flag.StringVarP(&flagCreateHomepage, "homepage", "h", "", "HOMEPAGE")
CmdRunner.Use(cmdCreate)
2013-07-10 01:36:02 +04:00
}
2013-07-09 22:03:07 +04:00
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() {
dirName, err := git.WorkdirName()
2013-07-10 01:36:02 +04:00
utils.Check(err)
newRepoName = github.SanitizeProjectName(dirName)
2013-07-10 01:36:02 +04:00
} 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
}
config := github.CurrentConfig()
host, err := config.DefaultHost()
if err != nil {
utils.Check(github.FormatError("creating repository", err))
}
2013-12-10 10:47:15 +04:00
2014-03-26 04:18:30 +04:00
owner := host.User
2013-12-10 10:47:15 +04:00
if strings.Contains(newRepoName, "/") {
split := strings.SplitN(newRepoName, "/", 2)
owner = split[0]
newRepoName = split[1]
}
2014-03-26 04:18:30 +04:00
project := github.NewProject(owner, newRepoName, host.Host)
2013-12-17 19:45:48 +04:00
gh := github.NewClient(project.Host)
2013-12-10 10:47:15 +04:00
2018-08-28 18:03:55 +03:00
repo, _ := gh.Repository(project)
2018-08-28 16:57:51 +03:00
if repo != nil {
if !repo.Private && flagCreatePrivate {
2018-08-28 18:03:55 +03:00
err = fmt.Errorf("Repository '%s' already exists and is public", repo.FullName)
utils.Check(err)
} else {
ui.Errorln("Existing repository detected. Updating git remote")
}
2013-07-10 01:36:02 +04:00
} else {
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
}
}
2014-04-09 02:17:43 +04:00
localRepo, err := github.LocalRepo()
utils.Check(err)
remote, _ := localRepo.OriginRemote()
if remote == nil || remote.Name != "origin" {
2013-07-10 01:36:02 +04:00
url := project.GitURL("", "", true)
args.Before("git", "remote", "add", "-f", "origin", url)
2013-07-10 01:36:02 +04:00
}
webUrl := project.WebURL("", "", "")
args.NoForward()
printBrowseOrCopy(args, webUrl, flagCreateBrowse, flagCreateCopy)
2013-07-10 01:36:02 +04:00
}