Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the option to get and import by SSH with GitHub #7

Merged
merged 4 commits into from
Jun 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"))
}