Skip to content

Commit

Permalink
✨ Add support for SSH URL
Browse files Browse the repository at this point in the history
* ✨ Add support for SSH URL
Due to net/url not parsing any ssh URL, gut would fail to use ssh.
All input validations wasn't working.
To fix this, gut now uses "whilp/git-urls" to validate URL

* 🐛 Fix path issue with cloning ssh repo
getRepoNameFromURL determines in which folder the repo must be cloned according to the URL
But the net/url package doesn't work well with ssh URL
If net/url returned an error, gut would fall back to the current directory.
To fix that, getRepoNameFromURL uses a new package "whilp/git-urls" to parse the URL. Repos are now cloned in the right directory
  • Loading branch information
julien040 authored Apr 10, 2023
1 parent 12b3b18 commit d7484a1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/go-git/go-git/v5 v5.5.1
github.com/matoous/go-nanoid/v2 v2.0.0
github.com/spf13/cobra v1.6.1
github.com/whilp/git-urls v1.0.0
github.com/zalando/go-keyring v0.2.2
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/whilp/git-urls v1.0.0 h1:95f6UMWN5FKW71ECsXRUd3FVYiXdrE7aX4NZKcPmIjU=
github.com/whilp/git-urls v1.0.0/go.mod h1:J16SAmobsqc3Qcy98brfl5f5+e0clUvg1krgwk/qCfE=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
Expand Down
11 changes: 5 additions & 6 deletions src/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package controller
import (
"fmt"
"net/mail"
"net/url"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,6 +15,7 @@ import (
"strings"

"github.com/spf13/cobra"
giturls "github.com/whilp/git-urls"

"github.com/julien040/gut/src/executor"
"github.com/julien040/gut/src/print"
Expand All @@ -24,18 +24,17 @@ import (

// Return true if the string is a valid URL for git
func checkURL(str string) bool {
parsed, err := url.Parse(str)
url, err := giturls.Parse(str)
if err != nil {
return false
} else {
// Check if the URL has a scheme and a host
return parsed.Scheme != "" && parsed.Host != ""
return url.Host != "" && url.Scheme != ""
}
}

// Return the name of the repo from the URL
func getRepoNameFromURL(str string) string {
parsed, err := url.Parse(str)
parsed, err := giturls.Parse(str)
if err != nil {
return ""
} else {
Expand Down Expand Up @@ -63,7 +62,7 @@ func makeValidPath(originalPath string, repoName string) string {
}

func getHost(str string) string {
parsed, err := url.Parse(str)
parsed, err := giturls.Parse(str)
if err != nil {
return ""
} else {
Expand Down
72 changes: 72 additions & 0 deletions src/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,42 @@ func Test_checkURL(t *testing.T) {
},
want: false,
},
{
name: "Valid URL with ssh",
args: args{
str: "git@github.com:julien040/gut.git",
},
want: true,
},
{
name: "Valid URL with ssh without .git",
args: args{
str: "git@github.com:julien040/gut",
},
want: true,
},
{
name: "Valid URL with ssh without host",
args: args{
str: "git@julien040/gut.git",
},
want: false,
},
{
name: "Valid URL with ssh without scheme",
args: args{
str: "julien040@git/github.com/julien040/gut.git",
},
want: false,
},
{
name: "Valid Gitlab URL with ssh",
args: args{
str: "git@gitlab.com:gitlab-org/gitlab.git",
},
want: true,
},

// Check URL doesn't support ssh yet
}
for _, tt := range tests {
Expand Down Expand Up @@ -79,6 +115,20 @@ func Test_getRepoNameFromURL(t *testing.T) {
},
want: "",
},
{
name: "Valid URL with ssh",
args: args{
str: "git@github.com:julien040/gut.git",
},
want: "gut",
},
{
name: "Valid URL with ssh without .git",
args: args{
str: "git@github.com:julien040/gut",
},
want: "gut",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -119,7 +169,29 @@ func Test_getHost(t *testing.T) {
},
want: "gitlab.com:8080",
},
{
name: "Valid URL with ssh",
args: args{
str: "git@github.com:julien040/gut",
},
want: "github.com",
},
{
name: "Invalid URL",
args: args{
str: "",
},
want: "",
},
{
name: "Valid URL with ssh without path",
args: args{
str: "git@github.com:julien040",
},
want: "github.com",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getHost(tt.args.str); got != tt.want {
Expand Down

0 comments on commit d7484a1

Please sign in to comment.