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. 28
      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 package base
import ( import (
"bytes"
"context" "context"
"os" "os"
"os/exec" "os/exec"
@ -12,10 +13,11 @@ import (
type Repo struct { type Repo struct {
url string url string
home string home string
branch string
} }
// NewRepo new a repository manager. // NewRepo new a repository manager.
func NewRepo(url string) *Repo { func NewRepo(url string, branch string) *Repo {
var start int var start int
start = strings.Index(url, "//") start = strings.Index(url, "//")
if start == -1 { if start == -1 {
@ -27,6 +29,7 @@ func NewRepo(url string) *Repo {
return &Repo{ return &Repo{
url: url, url: url,
home: kratosHomeWithDir("repo/" + url[start:end]), home: kratosHomeWithDir("repo/" + url[start:end]),
branch: branch,
} }
} }
@ -37,15 +40,26 @@ func (r *Repo) Path() string {
if end == -1 { if end == -1 {
end = len(r.url) 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. // Pull fetch the repository from remote url.
func (r *Repo) Pull(ctx context.Context) error { func (r *Repo) Pull(ctx context.Context) error {
cmd := exec.Command("git", "pull") cmd := exec.Command("git", "pull")
cmd.Dir = r.Path() cmd.Dir = r.Path()
cmd.Stderr = os.Stderr var out bytes.Buffer
cmd.Stderr = &out
cmd.Stdout = os.Stdout
err := cmd.Run() err := cmd.Run()
if strings.Contains(out.String(), "You are not currently on a branch.") {
return nil
}
return err return err
} }
@ -54,8 +68,14 @@ 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)
} }
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.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run() err := cmd.Run()
return err return err
} }

@ -6,7 +6,7 @@ import (
) )
func TestRepo(t *testing.T) { 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 { if err := r.Clone(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }

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

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

Loading…
Cancel
Save