fix(cmd/kratos): use context timeout control, add timeout param (#1592)

pull/1598/head
包子 3 years ago committed by GitHub
parent 143dc584cb
commit ce8ed35c1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      cmd/kratos/internal/base/repo.go
  2. 28
      cmd/kratos/internal/project/project.go

@ -2,6 +2,7 @@ package base
import ( import (
"context" "context"
"fmt"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -50,17 +51,19 @@ func (r *Repo) Path() string {
// 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", "symbolic-ref", "HEAD") cmd := exec.CommandContext(ctx, "git", "symbolic-ref", "HEAD")
cmd.Dir = r.Path() cmd.Dir = r.Path()
err := cmd.Run() _, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return nil return nil
} }
cmd = exec.Command("git", "pull") cmd = exec.CommandContext(ctx, "git", "pull")
cmd.Dir = r.Path() cmd.Dir = r.Path()
cmd.Stderr = os.Stderr out, err := cmd.CombinedOutput()
cmd.Stdout = os.Stdout if err != nil {
err = cmd.Run() return err
}
fmt.Println(string(out))
return err return err
} }
@ -71,14 +74,16 @@ func (r *Repo) Clone(ctx context.Context) error {
} }
var cmd *exec.Cmd var cmd *exec.Cmd
if r.branch == "" { if r.branch == "" {
cmd = exec.Command("git", "clone", r.url, r.Path()) cmd = exec.CommandContext(ctx, "git", "clone", r.url, r.Path())
} else { } else {
cmd = exec.Command("git", "clone", "-b", r.branch, r.url, r.Path()) cmd = exec.CommandContext(ctx, "git", "clone", "-b", r.branch, r.url, r.Path())
} }
cmd.Stderr = os.Stderr out, err := cmd.CombinedOutput()
cmd.Stdout = os.Stdout if err != nil {
err := cmd.Run()
return err return err
}
fmt.Println(string(out))
return nil
} }
// CopyTo copies the repository to project path. // CopyTo copies the repository to project path.

@ -2,6 +2,7 @@ package project
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"path" "path"
@ -22,14 +23,17 @@ var CmdNew = &cobra.Command{
var ( var (
repoURL string repoURL string
branch string branch string
timeout 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"
} }
timeout = "60s"
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") CmdNew.Flags().StringVarP(&branch, "branch", "b", branch, "repo branch")
CmdNew.Flags().StringVarP(&timeout, "timeout", "t", timeout, "time out")
} }
func run(cmd *cobra.Command, args []string) { func run(cmd *cobra.Command, args []string) {
@ -37,7 +41,11 @@ func run(cmd *cobra.Command, args []string) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) t, err := time.ParseDuration(timeout)
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), t)
defer cancel() defer cancel()
name := "" name := ""
if len(args) == 0 { if len(args) == 0 {
@ -53,8 +61,20 @@ func run(cmd *cobra.Command, args []string) {
name = args[0] name = args[0]
} }
p := &Project{Name: path.Base(name), Path: name} p := &Project{Name: path.Base(name), Path: name}
if err := p.New(ctx, wd, repoURL, branch); err != nil { done := make(chan error, 1)
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err) go func() {
return done <- p.New(ctx, wd, repoURL, branch)
}()
select {
case <-ctx.Done():
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
fmt.Fprint(os.Stderr, "\033[31mERROR: project creation timed out\033[m\n")
} else {
fmt.Fprintf(os.Stderr, "\033[31mERROR: failed to create project(%s)\033[m\n", ctx.Err())
}
case err = <-done:
if err != nil {
fmt.Fprintf(os.Stderr, "\033[31mERROR: Failed to create project(%s)\033[m\n", ctx.Err())
}
} }
} }

Loading…
Cancel
Save