From 22a3a9e9bb8445d70fd7bf44882ad7b0cf132165 Mon Sep 17 00:00:00 2001 From: letian Date: Thu, 17 Mar 2022 16:58:33 +0800 Subject: [PATCH] fix: kratos command error on windows (#1884) --- cmd/kratos/internal/base/repo.go | 23 ++++++++++++++++++++--- cmd/kratos/internal/base/repo_test.go | 17 +++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/cmd/kratos/internal/base/repo.go b/cmd/kratos/internal/base/repo.go index 7bf606d3b..86d4a1546 100644 --- a/cmd/kratos/internal/base/repo.go +++ b/cmd/kratos/internal/base/repo.go @@ -3,6 +3,7 @@ package base import ( "context" "fmt" + stdurl "net/url" "os" "os/exec" "path" @@ -16,8 +17,19 @@ type Repo struct { branch string } -// NewRepo new a repository manager. -func NewRepo(url string, branch string) *Repo { +func repoDir(url string) string { + if !strings.Contains(url, "//") { + url = "//" + url + } + if strings.HasPrefix(url, "//git@") { + url = "ssh:" + url + } else if strings.HasPrefix(url, "//") { + url = "https:" + url + } + u, err := stdurl.Parse(url) + if err == nil { + url = fmt.Sprintf("%s://%s%s", u.Scheme, u.Hostname(), u.Path) + } var start int start = strings.Index(url, "//") if start == -1 { @@ -26,9 +38,14 @@ func NewRepo(url string, branch string) *Repo { start += 2 } end := strings.LastIndex(url, "/") + return url[start:end] +} + +// NewRepo new a repository manager. +func NewRepo(url string, branch string) *Repo { return &Repo{ url: url, - home: kratosHomeWithDir("repo/" + url[start:end]), + home: kratosHomeWithDir("repo/" + repoDir(url)), branch: branch, } } diff --git a/cmd/kratos/internal/base/repo_test.go b/cmd/kratos/internal/base/repo_test.go index c3decaef7..15ed33c8c 100644 --- a/cmd/kratos/internal/base/repo_test.go +++ b/cmd/kratos/internal/base/repo_test.go @@ -2,10 +2,12 @@ package base import ( "context" + "os" "testing" ) func TestRepo(t *testing.T) { + os.RemoveAll("/tmp/test_repo") r := NewRepo("https://github.com/go-kratos/service-layout.git", "") if err := r.Clone(context.Background()); err != nil { t.Fatal(err) @@ -13,4 +15,19 @@ func TestRepo(t *testing.T) { if err := r.CopyTo(context.Background(), "/tmp/test_repo", "github.com/go-kratos/kratos-layout", nil); err != nil { t.Fatal(err) } + urls := []string{ + "ssh://git@gitlab.xxx.com:1234/foo/bar.git", + "ssh://gitlab.xxx.com:1234/foo/bar.git", + "//git@gitlab.xxx.com:1234/foo/bar.git", + "git@gitlab.xxx.com:1234/foo/bar.git", + "gitlab.xxx.com:1234/foo/bar.git", + "gitlab.xxx.com/foo/bar.git", + "gitlab.xxx.com/foo/bar", + } + for _, url := range urls { + dir := repoDir(url) + if dir != "gitlab.xxx.com/foo" { + t.Fatal("repoDir test failed", dir) + } + } }