Move building url logic to NewURL

This commit is contained in:
Tomohiro Nishimura 2014-06-08 00:02:39 +09:00
parent cbc6b29cff
commit 0b872fcbb2
3 changed files with 28 additions and 13 deletions

View file

@ -154,18 +154,12 @@ func doGet(c *cli.Context) {
url, err := NewURL(argURL)
utils.DieIf(err)
if !url.IsAbs() {
url.Scheme = "https"
url.Host = "github.com"
if url.Path[0] != '/' {
url.Path = "/" + url.Path
}
isSSH := c.Bool("p")
if isSSH {
url, err = ConvertGitHubURLHTTPToSSH(url)
utils.DieIf(err)
}
isSSH := c.Bool("p")
if isSSH {
fmt.Println(url)
url, err = ConvertGitHubURLHTTPToSSH(url)
fmt.Println(url)
utils.DieIf(err)
}
remote, err := NewRemoteRepository(url)
@ -440,3 +434,4 @@ func doImportPocket(c *cli.Context) {
getRemoteRepository(remote, c.Bool("update"))
}
}

16
url.go
View file

@ -22,10 +22,24 @@ func NewURL(ref string) (*url.URL, error) {
ref = fmt.Sprintf("ssh://%s%s/%s", user, host, path)
}
return url.Parse(ref)
url, err := url.Parse(ref)
if err != nil {
return url, err
}
if !url.IsAbs() {
url.Scheme = "https"
url.Host = "github.com"
if url.Path[0] != '/' {
url.Path = "/" + url.Path
}
}
return url, nil
}
func ConvertGitHubURLHTTPToSSH(url *url.URL) (*url.URL, error) {
sshURL := fmt.Sprintf("ssh://git@github.com/%s", url.Path)
return url.Parse(sshURL)
}

View file

@ -24,6 +24,11 @@ func TestNewURL(t *testing.T) {
Expect(scpUrlWithoutUser.String()).To(Equal("ssh://github.com/motemen/pusheen-explorer.git"))
Expect(scpUrlWithoutUser.Host).To(Equal("github.com"))
Expect(err).To(BeNil())
differentNameRepository, err := NewURL("motemen/ghq")
Expect(differentNameRepository.String()).To(Equal("https://github.com/motemen/ghq"))
Expect(differentNameRepository.Host).To(Equal("github.com"))
Expect(err).To(BeNil())
}
func TestConvertGitHubURLHTTPToSSH(t *testing.T) {
@ -34,3 +39,4 @@ func TestConvertGitHubURLHTTPToSSH(t *testing.T) {
Expect(err).To(BeNil())
Expect(sshURL.String()).To(Equal("ssh://git@github.com/motemen/pusheen-explorer"))
}