github.com/pengwynn/gh@v1.0.1-0.20140118055701-14327ca3942e/commands/remote.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "github.com/jingweno/gh/github" 6 "github.com/jingweno/gh/utils" 7 "regexp" 8 ) 9 10 var cmdRemote = &Command{ 11 Run: remote, 12 GitExtension: true, 13 Usage: "remote [-p] OPTIONS USER[/REPOSITORY]", 14 Short: "View and manage a set of remote repositories", 15 Long: `Add remote "git://github.com/USER/REPOSITORY.git" as with 16 git-remote(1). When /REPOSITORY is omitted, the basename of the 17 current working directory is used. With -p, use private remote 18 "git@github.com:USER/REPOSITORY.git". If USER is "origin" 19 then uses your GitHub login. 20 `, 21 } 22 23 func init() { 24 CmdRunner.Use(cmdRemote) 25 } 26 27 /* 28 $ gh remote add jingweno 29 > git remote add jingweno git://github.com/jingweno/THIS_REPO.git 30 31 $ gh remote add -p jingweno 32 > git remote add jingweno git@github.com:jingweno/THIS_REPO.git 33 34 $ gh remote add origin 35 > git remote add origin git://github.com/YOUR_LOGIN/THIS_REPO.git 36 */ 37 func remote(command *Command, args *Args) { 38 if !args.IsParamsEmpty() && (args.FirstParam() == "add" || args.FirstParam() == "set-url") { 39 transformRemoteArgs(args) 40 } 41 } 42 43 func transformRemoteArgs(args *Args) { 44 ownerWithName := args.LastParam() 45 owner, name := parseRepoNameOwner(ownerWithName) 46 if owner == "" { 47 return 48 } 49 50 localRepo := github.LocalRepo() 51 var repoName string 52 if name == "" { 53 project, err := localRepo.MainProject() 54 if err == nil { 55 repoName = project.Name 56 } else { 57 repoName, err = utils.DirName() 58 utils.Check(err) 59 } 60 name = repoName 61 } 62 63 words := args.Words() 64 isPriavte := parseRemotePrivateFlag(args) 65 if len(words) == 2 && words[1] == "origin" { 66 // gh add origin 67 credentials := github.CurrentConfigs().DefaultCredentials() 68 owner = credentials.User 69 name = repoName 70 } else if len(words) == 2 { 71 // gh remote add jingweno foo/bar 72 if idx := args.IndexOfParam(words[1]); idx != -1 { 73 args.ReplaceParam(idx, owner) 74 } 75 } else { 76 args.RemoveParam(args.ParamsSize() - 1) 77 } 78 79 project := github.NewProject(owner, name, "") 80 // for GitHub Enterprise 81 isPriavte = isPriavte || project.Host != github.GitHubHost 82 url := project.GitURL(name, owner, isPriavte) 83 args.AppendParams(url) 84 } 85 86 func parseRemotePrivateFlag(args *Args) bool { 87 if i := args.IndexOfParam("-p"); i != -1 { 88 args.RemoveParam(i) 89 return true 90 } 91 92 return false 93 } 94 95 func parseRepoNameOwner(nameWithOwner string) (owner, name string) { 96 ownerRe := fmt.Sprintf("^(%s)$", OwnerRe) 97 ownerRegexp := regexp.MustCompile(ownerRe) 98 if ownerRegexp.MatchString(nameWithOwner) { 99 owner = ownerRegexp.FindStringSubmatch(nameWithOwner)[1] 100 return 101 } 102 103 nameWithOwnerRe := fmt.Sprintf("^(%s)\\/(%s)$", OwnerRe, NameRe) 104 nameWithOwnerRegexp := regexp.MustCompile(nameWithOwnerRe) 105 if nameWithOwnerRegexp.MatchString(nameWithOwner) { 106 result := nameWithOwnerRegexp.FindStringSubmatch(nameWithOwner) 107 owner = result[1] 108 name = result[2] 109 } 110 111 return 112 }