modify kratos cmd new project (#907)

* Modifying the kratos cmd new project does not use the go git module, but uses the native git command
pull/911/head
包子 4 years ago committed by GitHub
parent 42313e9368
commit 0e4a057027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      cmd/kratos/internal/base/path.go
  2. 44
      cmd/kratos/internal/base/repo.go
  3. 14
      config/file/file_test.go

@ -73,10 +73,8 @@ func copyDir(src, dst string, replaces, ignores []string) error {
if hasSets(fd.Name(), ignores) { if hasSets(fd.Name(), ignores) {
continue continue
} }
srcfp := path.Join(src, fd.Name()) srcfp := path.Join(src, fd.Name())
dstfp := path.Join(dst, fd.Name()) dstfp := path.Join(dst, fd.Name())
if fd.IsDir() { if fd.IsDir() {
if err = copyDir(srcfp, dstfp, replaces, ignores); err != nil { if err = copyDir(srcfp, dstfp, replaces, ignores); err != nil {
return err return err

@ -2,12 +2,10 @@ package base
import ( import (
"context" "context"
"errors"
"os" "os"
"os/exec"
"path" "path"
"strings" "strings"
"github.com/go-git/go-git/v5"
) )
// Repo is git repository manager. // Repo is git repository manager.
@ -18,11 +16,17 @@ type Repo struct {
// NewRepo new a repository manager. // NewRepo new a repository manager.
func NewRepo(url string) *Repo { func NewRepo(url string) *Repo {
start := strings.Index(url, "//") var start int
start = strings.Index(url, "//")
if start == -1 {
start = strings.Index(url, ":") + 1
} else {
start += 2
}
end := strings.LastIndex(url, "/") end := strings.LastIndex(url, "/")
return &Repo{ return &Repo{
url: url, url: url,
home: kratosHomeWithDir("repo/" + url[start+2:end]), home: kratosHomeWithDir("repo/" + url[start:end]),
} }
} }
@ -30,25 +34,19 @@ func NewRepo(url string) *Repo {
func (r *Repo) Path() string { func (r *Repo) Path() string {
start := strings.LastIndex(r.url, "/") start := strings.LastIndex(r.url, "/")
end := strings.LastIndex(r.url, ".git") end := strings.LastIndex(r.url, ".git")
if end == -1 {
end = len(r.url)
}
return path.Join(r.home, r.url[start+1:end]) return path.Join(r.home, r.url[start+1:end])
} }
// Pull fetch the repository from remote url. // Pull fetch the repository from remote url.
func (r *Repo) Pull(ctx context.Context) error { func (r *Repo) Pull(ctx context.Context) error {
repo, err := git.PlainOpen(r.Path()) cmd := exec.Command("git", "pull")
if err != nil { cmd.Dir = r.Path()
return err cmd.Stdout = os.Stdout
} cmd.Stderr = os.Stderr
w, err := repo.Worktree() err := cmd.Run()
if err != nil {
return err
}
if err = w.PullContext(ctx, &git.PullOptions{
RemoteName: "origin",
Progress: os.Stdout,
}); errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil
}
return err return err
} }
@ -57,10 +55,10 @@ func (r *Repo) Clone(ctx context.Context) error {
if _, err := os.Stat(r.Path()); !os.IsNotExist(err) { if _, err := os.Stat(r.Path()); !os.IsNotExist(err) {
return r.Pull(ctx) return r.Pull(ctx)
} }
_, err := git.PlainCloneContext(ctx, r.Path(), false, &git.CloneOptions{ cmd := exec.Command("git", "clone", r.url, r.Path())
URL: r.url, cmd.Stdout = os.Stdout
Progress: os.Stdout, cmd.Stderr = os.Stderr
}) err := cmd.Run()
return err return err
} }

@ -37,6 +37,7 @@ const (
} }
] ]
}` }`
// _testYaml = ` // _testYaml = `
//Foo: //Foo:
// bar : // bar :
@ -46,6 +47,7 @@ const (
// //
//` //`
) )
//func TestScan(t *testing.T) { //func TestScan(t *testing.T) {
// //
//} //}
@ -168,19 +170,19 @@ func testScan(t *testing.T, c config.Config) {
type TestJSON struct { type TestJSON struct {
Test struct { Test struct {
Settings struct { Settings struct {
IntKey int `json:"int_key"` IntKey int `json:"int_key"`
FloatKey float64 `json:"float_key"` FloatKey float64 `json:"float_key"`
DurationKey int `json:"duration_key"` DurationKey int `json:"duration_key"`
StringKey string `json:"string_key"` StringKey string `json:"string_key"`
} `json:"settings"` } `json:"settings"`
Server struct { Server struct {
Addr string `json:"addr"` Addr string `json:"addr"`
Port int `json:"port"` Port int `json:"port"`
} `json:"server"` } `json:"server"`
} `json:"test"` } `json:"test"`
Foo []struct { Foo []struct {
Name string `json:"name"` Name string `json:"name"`
Age int `json:"age"` Age int `json:"age"`
} `json:"foo"` } `json:"foo"`
} }
var conf TestJSON var conf TestJSON

Loading…
Cancel
Save