Skip to content
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

fix: kratos command error on windows #1884

Merged
merged 2 commits into from
Mar 17, 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
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)
}
}
}