fix: kratos command error on windows (#1884)

pull/1887/head
letian 3 years ago committed by GitHub
parent c550a886e9
commit d373c51acf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      cmd/kratos/internal/base/repo.go
  2. 17
      cmd/kratos/internal/base/repo_test.go

@ -3,6 +3,7 @@ package base
import ( import (
"context" "context"
"fmt" "fmt"
stdurl "net/url"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -16,8 +17,19 @@ type Repo struct {
branch string branch string
} }
// NewRepo new a repository manager. func repoDir(url string) string {
func NewRepo(url string, branch string) *Repo { 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 var start int
start = strings.Index(url, "//") start = strings.Index(url, "//")
if start == -1 { if start == -1 {
@ -26,9 +38,14 @@ func NewRepo(url string, branch string) *Repo {
start += 2 start += 2
} }
end := strings.LastIndex(url, "/") end := strings.LastIndex(url, "/")
return url[start:end]
}
// NewRepo new a repository manager.
func NewRepo(url string, branch string) *Repo {
return &Repo{ return &Repo{
url: url, url: url,
home: kratosHomeWithDir("repo/" + url[start:end]), home: kratosHomeWithDir("repo/" + repoDir(url)),
branch: branch, branch: branch,
} }
} }

@ -2,10 +2,12 @@ package base
import ( import (
"context" "context"
"os"
"testing" "testing"
) )
func TestRepo(t *testing.T) { func TestRepo(t *testing.T) {
os.RemoveAll("/tmp/test_repo")
r := NewRepo("https://github.com/go-kratos/service-layout.git", "") r := NewRepo("https://github.com/go-kratos/service-layout.git", "")
if err := r.Clone(context.Background()); err != nil { if err := r.Clone(context.Background()); err != nil {
t.Fatal(err) 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 { if err := r.CopyTo(context.Background(), "/tmp/test_repo", "github.com/go-kratos/kratos-layout", nil); err != nil {
t.Fatal(err) 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)
}
}
} }

Loading…
Cancel
Save