mirror of
https://github.com/x-motemen/ghq.git
synced 2026-09-10 07:26:27 -04:00
Move building url logic to NewURL
This commit is contained in:
parent
cbc6b29cff
commit
0b872fcbb2
19
commands.go
19
commands.go
|
|
@ -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
16
url.go
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue