Skip to content

Commit

Permalink
fix: kratos command error on windows (#1884)
Browse files Browse the repository at this point in the history
  • Loading branch information
letian0805 authored Mar 17, 2022
1 parent c550a88 commit d373c51
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
23 changes: 20 additions & 3 deletions cmd/kratos/internal/base/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package base
import (
"context"
"fmt"
stdurl "net/url"
"os"
"os/exec"
"path"
Expand All @@ -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 {
Expand All @@ -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,
}
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/kratos/internal/base/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@ 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)
}
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)
}
}
}

0 comments on commit d373c51

Please sign in to comment.