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

@ -2,6 +2,7 @@ package project
import (
"context"
"errors"
"fmt"
"os"
"path"
@ -22,14 +23,17 @@ var CmdNew = &cobra.Command{
var (
repoURL string
branch string
timeout string
)
func init() {
if repoURL = os.Getenv("KRATOS_LAYOUT_REPO"); repoURL == "" {
repoURL = "https://github.com/go-kratos/kratos-layout.git"
}
timeout = "60s"
CmdNew.Flags().StringVarP(&repoURL, "repo-url", "r", repoURL, "layout repo")
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) {
@ -37,7 +41,11 @@ func run(cmd *cobra.Command, args []string) {
if err != nil {
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()
name := ""
if len(args) == 0 {
@ -53,8 +61,20 @@ func run(cmd *cobra.Command, args []string) {
name = args[0]
}
p := &Project{Name: path.Base(name), Path: name}
if err := p.New(ctx, wd, repoURL, branch); err != nil {
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err)
return
done := make(chan error, 1)
go func() {
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