add cmd specify the version or branch #977 (#986)

* add cmd specify the version or branch
pull/987/head
包子 4 years ago committed by GitHub
parent c547c61f89
commit 02f9ea49c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      cmd/kratos/internal/base/repo.go
  2. 2
      cmd/kratos/internal/base/repo_test.go
  3. 5
      cmd/kratos/internal/project/new.go
  4. 4
      cmd/kratos/internal/project/project.go

@ -1,6 +1,7 @@
package base
import (
"bytes"
"context"
"os"
"os/exec"
@ -10,12 +11,13 @@ import (
// Repo is git repository manager.
type Repo struct {
url string
home string
url string
home string
branch string
}
// NewRepo new a repository manager.
func NewRepo(url string) *Repo {
func NewRepo(url string, branch string) *Repo {
var start int
start = strings.Index(url, "//")
if start == -1 {
@ -25,8 +27,9 @@ func NewRepo(url string) *Repo {
}
end := strings.LastIndex(url, "/")
return &Repo{
url: url,
home: kratosHomeWithDir("repo/" + url[start:end]),
url: url,
home: kratosHomeWithDir("repo/" + url[start:end]),
branch: branch,
}
}
@ -37,15 +40,26 @@ func (r *Repo) Path() string {
if end == -1 {
end = len(r.url)
}
return path.Join(r.home, r.url[start+1:end])
branch := ""
if r.branch == "" {
branch = "@main"
} else {
branch = "@" + r.branch
}
return path.Join(r.home, r.url[start+1:end]+branch)
}
// Pull fetch the repository from remote url.
func (r *Repo) Pull(ctx context.Context) error {
cmd := exec.Command("git", "pull")
cmd.Dir = r.Path()
cmd.Stderr = os.Stderr
var out bytes.Buffer
cmd.Stderr = &out
cmd.Stdout = os.Stdout
err := cmd.Run()
if strings.Contains(out.String(), "You are not currently on a branch.") {
return nil
}
return err
}
@ -54,8 +68,14 @@ func (r *Repo) Clone(ctx context.Context) error {
if _, err := os.Stat(r.Path()); !os.IsNotExist(err) {
return r.Pull(ctx)
}
cmd := exec.Command("git", "clone", r.url, r.Path())
cmd := &exec.Cmd{}
if r.branch == "" {
cmd = exec.Command("git", "clone", r.url, r.Path())
} else {
cmd = exec.Command("git", "clone", "-b", r.branch, r.url, r.Path())
}
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
return err
}

@ -6,7 +6,7 @@ import (
)
func TestRepo(t *testing.T) {
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 {
t.Fatal(err)
}

@ -18,7 +18,8 @@ type Project struct {
}
// New new a project from remote repo.
func (p *Project) New(ctx context.Context, dir string, layout string) error {
func (p *Project) New(ctx context.Context, dir string, layout string, branch string) error {
to := path.Join(dir, p.Name)
if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Printf("🚫 %s already exists\n", p.Name)
@ -34,7 +35,7 @@ func (p *Project) New(ctx context.Context, dir string, layout string) error {
os.RemoveAll(to)
}
fmt.Printf("🚀 Creating service %s, layout repo is %s, please wait a moment.\n\n", p.Name, layout)
repo := base.NewRepo(layout)
repo := base.NewRepo(layout, branch)
if err := repo.CopyTo(ctx, to, p.Name, []string{".git", ".github"}); err != nil {
return err
}

@ -19,12 +19,14 @@ var CmdNew = &cobra.Command{
}
var repoURL string
var branch string
func init() {
if repoURL = os.Getenv("KRATOS_LAYOUT_REPO"); repoURL == "" {
repoURL = "https://github.com/go-kratos/kratos-layout.git"
}
CmdNew.Flags().StringVarP(&repoURL, "-repo-url", "r", repoURL, "layout repo")
CmdNew.Flags().StringVarP(&branch, "-branch", "b", branch, "repo branch")
}
func run(cmd *cobra.Command, args []string) {
@ -48,7 +50,7 @@ func run(cmd *cobra.Command, args []string) {
name = args[0]
}
p := &Project{Name: name}
if err := p.New(ctx, wd, repoURL); err != nil {
if err := p.New(ctx, wd, repoURL, branch); err != nil {
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err)
return
}

Loading…
Cancel
Save