From e2ac65017a64372b4b011c91773a14ec5c83f776 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 19 Nov 2023 23:01:36 +0800 Subject: [PATCH] chore: imporve cmd[run/new] (#116) --- CHANGELOG.md | 1 + README.md | 2 +- cmd/eagle/internal/project/new.go | 4 ++-- cmd/eagle/internal/run/run.go | 23 ++++++++++++++++------- cmd/eagle/main.go | 2 +- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 171893f3fa..0263a130e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - chore(cli): improve gen task command - chore(response): remove init resp and improve Error - chore: improve redis queue and add example +- chore: improve cmd: new and run ## v1.7.0 - feat(http): can custom http status diff --git a/README.md b/README.md index 9eae2b4dd5..b693062d70 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ eagle new github.com/foo/eagle-demo make build # run -./eagle-demo +eagle run ``` ## Documentation diff --git a/cmd/eagle/internal/project/new.go b/cmd/eagle/internal/project/new.go index fa2c8870b1..0727add68b 100644 --- a/cmd/eagle/internal/project/new.go +++ b/cmd/eagle/internal/project/new.go @@ -57,8 +57,8 @@ func (p *Project) New(ctx context.Context, dir string, layout string) error { fmt.Print("💻 Use the following command to start the project 👇:\n\n") fmt.Println(color.WhiteString("$ cd %s", p.Name)) - fmt.Println(color.WhiteString("$ go build")) - fmt.Println(color.WhiteString("$ ./%s\n", p.Name)) + fmt.Println(color.WhiteString("$ make build")) + fmt.Println(color.WhiteString("$ eagle run\n")) fmt.Println("🤝 Thanks for using Eagle") fmt.Println("📚 Tutorial: https://go-eagle.org/docs/getting-started/start") return nil diff --git a/cmd/eagle/internal/run/run.go b/cmd/eagle/internal/run/run.go index 9128154ed0..ae317fbbf1 100644 --- a/cmd/eagle/internal/run/run.go +++ b/cmd/eagle/internal/run/run.go @@ -21,7 +21,7 @@ var CmdRun = &cobra.Command{ Run: Run, } -// Run run project. +// Run project. func Run(cmd *cobra.Command, args []string) { var dir string if len(args) > 0 { @@ -32,6 +32,7 @@ func Run(cmd *cobra.Command, args []string) { fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err) return } + var selectedDir string if dir == "" { // find the directory containing the cmd/* cmdPath, err := findCMD(base) @@ -52,17 +53,24 @@ func Run(cmd *cobra.Command, args []string) { cmdPaths = append(cmdPaths, k) } prompt := &survey.Select{ - Message: "Which directory do you want to run?", - Options: cmdPaths, + Message: "Which directory do you want to run?", + Options: cmdPaths, + PageSize: 6, } - survey.AskOne(prompt, &dir) - if dir == "" { + // get cmd dir, eg: cmd/server + err := survey.AskOne(prompt, &dir) + if err != nil && dir == "" { return } - dir = path.Join(cmdPath[dir], dir) + // eg: cmd/server + selectedDir = dir + // project absolute path + dir = cmdPath[dir] } } - fd := exec.Command("go", "run", ".") + + // go run /path/cmd/server + fd := exec.Command("go", []string{"run", path.Join(dir, selectedDir)}...) fd.Stdout = os.Stdout fd.Stderr = os.Stderr fd.Dir = dir @@ -73,6 +81,7 @@ func Run(cmd *cobra.Command, args []string) { return } +// map, eg: cmd/server -> project absolute path func findCMD(base string) (map[string]string, error) { var root bool next := func(dir string) (map[string]string, error) { diff --git a/cmd/eagle/main.go b/cmd/eagle/main.go index e6ced1b141..86644d0df7 100644 --- a/cmd/eagle/main.go +++ b/cmd/eagle/main.go @@ -19,7 +19,7 @@ import ( var ( // Version is the version of the compiled software. - Version = "v0.15.6" + Version = "v0.16.0" rootCmd = &cobra.Command{ Use: "eagle",