From eaf85736ac3da1c75b2b938755877791b3e7bf30 Mon Sep 17 00:00:00 2001 From: Theodore Kokkoris Date: Thu, 13 Jun 2013 17:11:05 +0300 Subject: [PATCH] Added remote add command --- commands/commands.go | 1 + commands/remoteAdd.go | 66 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 commands/remoteAdd.go diff --git a/commands/commands.go b/commands/commands.go index 8aca1566..398a4bab 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -43,6 +43,7 @@ var All = []*Command{ cmdPull, cmdFork, cmdCi, + cmdRemoteAdd, cmdBrowse, cmdCompare, cmdHelp, diff --git a/commands/remoteAdd.go b/commands/remoteAdd.go new file mode 100644 index 00000000..ebe12f87 --- /dev/null +++ b/commands/remoteAdd.go @@ -0,0 +1,66 @@ +package commands + +import ( + "fmt" + "os" + "github.com/jingweno/gh/github" + "github.com/jingweno/gh/git" + "github.com/jingweno/gh/utils" +) + +var cmdRemoteAdd = &Command{ + Run: remoteAdd, + Usage: "remote add [-p] USER", + Short: "Add remote from GitHub repository", + Long: `Add remote from GitHub repository, using USER as the username and the current repository name. +If -p is provided, the SSH remote will be added. +If USER is "origin", your own username will be used. +`, +} + +func toSSHOrNotToSSH(args []string) bool { + for i:=0;i 0 && args[0] != "add" || len(args) == 2 && args[1] == "-p" || len(args) > 3 { + command.PrintUsage() + os.Exit(1) + } + + var name string + + if args[1] == "-p" { + name = args[2] + } else { + name = args[1] + } + + flagRemoteAddSSH := toSSHOrNotToSSH(args) + + gh := github.New() + project := gh.Project + + if name == "origin" { + project.Owner = gh.FetchUsername() + } else { + project.Owner = name + } + + var url string + + if flagRemoteAddSSH { + url = project.SshURL() + } else { + url = project.GitURL() + } + + err := git.AddRemote(name, url) + utils.Check(err) + fmt.Printf("The remote %s has been added.\n", url) +}