mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
fix: kratos command error on windows (#1884)
This commit is contained in:
parent
c550a886e9
commit
d373c51acf
@ -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,
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user