Skip to content

Implement getAbsoluteUrl function to abort the difference in incoming url when use -t option #120

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

Merged
merged 2 commits into from
Feb 14, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pkg/cgapp/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package cgapp

import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/go-git/go-git/v5"
)
Expand All @@ -30,7 +32,7 @@ func GitClone(templateType, templateURL string) error {
folder,
false,
&git.CloneOptions{
URL: fmt.Sprintf("https://%s", templateURL),
URL: getAbsoluteURL(templateURL),
},
)
if errPlainClone != nil {
Expand All @@ -44,3 +46,14 @@ func GitClone(templateType, templateURL string) error {

return nil
}

func getAbsoluteURL(templateURL string) string {
templateURL = strings.TrimSpace(templateURL)
u, _ := url.Parse(templateURL)

if len(u.Scheme) == 0 {
u.Scheme = "https"
}

return u.String()
}
41 changes: 41 additions & 0 deletions pkg/cgapp/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,44 @@ func TestGitClone(t *testing.T) {
}
}
}

func Test_getAbsoluteURL(t *testing.T) {
type args struct {
templateURL string
}
tests := []struct {
name string
args args
want string
}{
{
"successfully get absolute url from url with scheme",
args{
templateURL: "https://github.com/create-go-app/net_http-go-template",
},
"https://github.com/create-go-app/net_http-go-template",
},
{
"successfully get absolute url from url without scheme",
args{
templateURL: "github.com/create-go-app/net_http-go-template",
},
"https://github.com/create-go-app/net_http-go-template",
},
{
"successfully get absolute url from url starting space",
args{
templateURL: " github.com/create-go-app/net_http-go-template",
},
"https://github.com/create-go-app/net_http-go-template",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getAbsoluteURL(tt.args.templateURL); got != tt.want {
t.Errorf("getAbsoluteURL() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 0 additions & 2 deletions pkg/registry/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,13 @@ var (
Name: "backend",
Prompt: &survey.Input{
Message: "Enter URL to the custom backend repository:",
Help: "No need to specify `http://` or `https://` protocol.",
},
Validate: survey.Required,
},
{
Name: "frontend",
Prompt: &survey.Input{
Message: "Enter URL to the custom frontend repository:",
Help: "No need to specify `http://` or `https://` protocol.",
Default: "none",
},
},
Expand Down