Skip to content

Commit

Permalink
chore: added unable to parse remote url error type
Browse files Browse the repository at this point in the history
  • Loading branch information
arunpoudel committed Dec 20, 2023
1 parent 967ee00 commit 2c006a3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
29 changes: 29 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package magekubernetes

import "fmt"

type (
ErrUnableToParseRemoteURL struct {
url string
}
)

// Error implements error.
func (e *ErrUnableToParseRemoteURL) Error() string {
return fmt.Sprintf("Unable to parse remote url %v", e.url)
}

func (e *ErrUnableToParseRemoteURL) Is(target error) bool {
t, ok := target.(*ErrUnableToParseRemoteURL)
if !ok {
return false
}
if t.url != e.url {
return false
}
return true
}

func NewErrUnableToParseRemoteURL(url string) *ErrUnableToParseRemoteURL {
return &ErrUnableToParseRemoteURL{url: url}
}
3 changes: 1 addition & 2 deletions metadata.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package magekubernetes

import (
"fmt"
"strings"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -29,6 +28,6 @@ func gitRemoteParser(remote string) (string, error) {
toHTTPS := "https://github.com/" + strings.Split(url, ":")[1]
return strings.TrimSuffix(toHTTPS, ".git"), nil
}
return "", fmt.Errorf("Unable to parse remote url %v", remote)
return "", NewErrUnableToParseRemoteURL(url)

}
5 changes: 3 additions & 2 deletions metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package magekubernetes

import (
"errors"
"fmt"
"testing"
)
Expand All @@ -21,13 +22,13 @@ func TestGitRemoteParser(t *testing.T) {
}{
{"origin [email protected]:coopnorge/helloworld.git (fetch)", "https://github.com/coopnorge/helloworld", nil},
{"origin https://github.com/coopnorge/helloworld.git (fetch)", "https://github.com/coopnorge/helloworld", nil},
{"origin http://github.com/coopnorge/helloworld.git (fetch)", "", fmt.Errorf("Unable to parse remote url %v", "origin http://github.com/coopnorge/helloworld.git (fetch)")},
{"origin http://github.com/coopnorge/helloworld.git (fetch)", "", NewErrUnableToParseRemoteURL("http://github.com/coopnorge/helloworld.git")},
}
for _, tt := range tests {
testname := fmt.Sprintf("%s,%s", tt.remote, tt.want)
t.Run(testname, func(t *testing.T) {
got, err := gitRemoteParser(tt.remote)
if got != tt.want || err != tt.err {
if got != tt.want || !errors.Is(err, tt.err) {
t.Errorf("\n got: %s,%v \nwant: %s,%v", got, err, tt.want, tt.err)
}
})
Expand Down

0 comments on commit 2c006a3

Please sign in to comment.