func transformRemoteArgs(args *Args) { ownerWithName := args.LastParam() owner, name, match := parseRepoNameOwner(ownerWithName) if !match { return } var err error if name == "" { name, err = utils.DirName() utils.Check(err) } isPriavte := parseRemotePrivateFlag(args) if owner == "origin" { owner = github.CurrentConfig().FetchUser() } else if args.ParamsSize() > 2 { // `git remote add jingweno foo/bar` args.RemoveParam(args.ParamsSize() - 1) } project := github.Project{Owner: owner, Name: name} url := project.GitURL(name, owner, isPriavte) args.AppendParams(url) }
func NewProject(owner, name, host string) *Project { if strings.Contains(owner, "/") { result := strings.SplitN(owner, "/", 2) owner = result[0] if name == "" { name = result[1] } } else if strings.Contains(name, "/") { result := strings.SplitN(name, "/", 2) if owner == "" { owner = result[0] } name = result[1] } if host == "" { host = DefaultHost() } if owner == "" { c := CurrentConfigs().PromptFor(host) owner = c.User } if name == "" { name, _ = utils.DirName() } return &Project{Name: name, Owner: owner, Host: host} }
/* $ gh create ... create repo on github ... > git remote add -f origin [email protected]: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 [email protected]:YOUR_USER/recipes.git $ gh create sinatra/recipes [ repo created in GitHub organization ] > git remote add origin [email protected]:sinatra/recipes.git */ func create(command *Command, args *Args) { _, err := git.Dir() if err != nil { err = fmt.Errorf("'create' must be run from inside a git repository") utils.Check(err) } var newRepoName string if args.IsParamsEmpty() { newRepoName, err = utils.DirName() utils.Check(err) } else { reg := regexp.MustCompile("^[^-]") if !reg.MatchString(args.FirstParam()) { err = fmt.Errorf("invalid argument: %s", args.FirstParam()) utils.Check(err) } newRepoName = args.FirstParam() } configs := github.CurrentConfigs() credentials := configs.DefaultCredentials() 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) gh := github.NewClient(project.Host) var action string if gh.IsRepositoryExist(project) { fmt.Printf("%s already exists on %s\n", project, project.Host) action = "set remote origin" } else { action = "created repository" if !args.Noop { repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate) utils.Check(err) project = github.NewProject(repo.FullName, "", project.Host) } } remote, _ := github.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:", action), project.String()) }
func transformInitArgs(args *Args) error { if !parseInitFlag(args) { return nil } name, err := utils.DirName() if err != nil { return err } project := github.NewProject("", name, "") url := project.GitURL("", "", true) args.After("git", "remote", "add", "origin", url) return nil }
func transformInitArgs(args *Args) error { if !parseInitFlag(args) { return nil } name, err := utils.DirName() if err != nil { return err } owner := github.CurrentConfig().FetchUser() project := github.Project{Owner: owner, Name: name} url := project.GitURL(name, owner, true) args.After("git", "remote", "add", "origin", url) return nil }
func transformRemoteArgs(args *Args) { ownerWithName := args.LastParam() owner, name := parseRepoNameOwner(ownerWithName) if owner == "" { return } localRepo := github.LocalRepo() var repoName string if name == "" { project, err := localRepo.MainProject() if err == nil { repoName = project.Name } else { repoName, err = utils.DirName() utils.Check(err) } name = repoName } words := args.Words() isPriavte := parseRemotePrivateFlag(args) if len(words) == 2 && words[1] == "origin" { // gh add origin credentials := github.CurrentConfigs().DefaultCredentials() owner = credentials.User name = repoName } else if len(words) == 2 { // gh remote add jingweno foo/bar if idx := args.IndexOfParam(words[1]); idx != -1 { args.ReplaceParam(idx, owner) } } else { args.RemoveParam(args.ParamsSize() - 1) } project := github.NewProject(owner, name, "") // for GitHub Enterprise isPriavte = isPriavte || project.Host != github.GitHubHost url := project.GitURL(name, owner, isPriavte) args.AppendParams(url) }
func NewProjectFromNameAndOwner(name, owner string) Project { if strings.Contains(owner, "/") { result := strings.SplitN(owner, "/", 2) owner = result[0] name = result[1] } else if strings.Contains(name, "/") { result := strings.SplitN(owner, "/", 2) owner = result[0] name = result[1] } if owner == "" { owner = CurrentConfig().FetchUser() } if name == "" { name, _ = utils.DirName() } return Project{Name: name, Owner: owner} }
/* $ gh create ... create repo on github ... > git remote add -f origin [email protected]: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 [email protected]:YOUR_USER/recipes.git $ gh create sinatra/recipes [ repo created in GitHub organization ] > git remote add origin [email protected]:sinatra/recipes.git */ func create(command *Command, args *Args) { var ( name string err error ) if args.IsParamsEmpty() { name, err = utils.DirName() utils.Check(err) } else { name = args.FirstParam() } var msg string project := github.NewProjectFromNameAndOwner(name, "") gh := github.NewWithoutProject() if gh.IsRepositoryExist(project) { fmt.Printf("%s already exists on %s\n", project, github.GitHubHost) msg = "set remmote origin" } else { if !args.Noop { repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate) utils.Check(err) project = github.NewProjectFromNameAndOwner("", repo.FullName) } msg = "created repository" } 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()) }