Skip to content

Commit

Permalink
Merge pull request #7 from moznion/option_to_get_by_ssh
Browse files Browse the repository at this point in the history
Support the option to `get` and `import` by SSH with GitHub
  • Loading branch information
motemen committed Jun 6, 2014
2 parents 635ff96 + 7ba384a commit 3877ce9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
23 changes: 20 additions & 3 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var commandGet = cli.Command{
Action: doGet,
Flags: []cli.Flag{
cli.BoolFlag{"update, u", "Update local repository if cloned already"},
cli.BoolFlag{"p", "Clone with SSH"},
},
}

Expand Down Expand Up @@ -82,6 +83,7 @@ var commandImportStarred = cli.Command{
Action: doImportStarred,
Flags: []cli.Flag{
cli.BoolFlag{"update, u", "Update local repository if cloned already"},
cli.BoolFlag{"p", "Clone with SSH"},
},
}

Expand All @@ -104,11 +106,11 @@ type commandDoc struct {
}

var commandDocs = map[string]commandDoc{
"get": {"", "[-u] <repository URL> | <user>/<project>"},
"get": {"", "[-u] <repository URL> | [-u] [-p] <user>/<project>"},
"list": {"", "[-p] [-e] [<query>]"},
"look": {"", "<project> | <user>/<project> | <host>/<user>/<project>"},
"import": {"", "[-u] starred <user> | pocket"},
"starred": {"import", "[-u] <user>"},
"import": {"", "[-u] [-p] starred <user> | [-u] pocket"},
"starred": {"import", "[-u] [-p] <user>"},
"pocket": {"import", "[-u]"},
}

Expand Down Expand Up @@ -157,6 +159,12 @@ func doGet(c *cli.Context) {
if url.Path[0] != '/' {
url.Path = "/" + url.Path
}

isSSH := c.Bool("p")
if isSSH {
url, err = ConvertGitHubURLHTTPToSSH(url)
utils.DieIf(err)
}
}

remote, err := NewRemoteRepository(url)
Expand Down Expand Up @@ -323,6 +331,8 @@ func doLook(c *cli.Context) {
func doImportStarred(c *cli.Context) {
user := c.Args().First()

isSSH := c.Bool("p")

if user == "" {
cli.ShowCommandHelp(c, "starred")
os.Exit(1)
Expand All @@ -344,6 +354,13 @@ func doImportStarred(c *cli.Context) {
utils.Log("error", fmt.Sprintf("Could not parse URL <%s>: %s", repo.HTMLURL, err))
continue
}
if isSSH {
url, err = ConvertGitHubURLHTTPToSSH(url)
if err != nil {
utils.Log("error", fmt.Sprintf("Could not convert URL <%s>: %s", repo.HTMLURL, err))
continue
}
}

remote, err := NewRemoteRepository(url)
if utils.ErrorIf(err) {
Expand Down
5 changes: 5 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ func NewURL(ref string) (*url.URL, error) {

return url.Parse(ref)
}

func ConvertGitHubURLHTTPToSSH(url *url.URL) (*url.URL, error) {
sshURL := fmt.Sprintf("ssh://git@github.com/%s", url.Path)
return url.Parse(sshURL)
}
9 changes: 9 additions & 0 deletions url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ func TestNewURL(t *testing.T) {
Expect(scpUrlWithoutUser.Host).To(Equal("github.com"))
Expect(err).To(BeNil())
}

func TestConvertGitHubURLHTTPToSSH(t *testing.T) {
RegisterTestingT(t)

httpsURL, err := NewURL("https://github.com/motemen/pusheen-explorer")
sshURL, err := ConvertGitHubURLHTTPToSSH(httpsURL)
Expect(err).To(BeNil())
Expect(sshURL.String()).To(Equal("ssh://git@github.com/motemen/pusheen-explorer"))
}

0 comments on commit 3877ce9

Please sign in to comment.