mirror of
https://github.com/go-kratos/kratos.git
synced 2025-02-19 19:10:08 +02:00
fix(cmd/kratos): use context timeout control, add timeout param (#1592)
This commit is contained in:
parent
143dc584cb
commit
ce8ed35c1d
@ -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()
|
||||
return err
|
||||
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…
x
Reference in New Issue
Block a user