From f03c6fb6d73e43b98675da4168cf159faa38bdac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20K=C3=B6ser?= Date: Wed, 13 Dec 2023 15:26:00 +0100 Subject: [PATCH 1/2] refactor(cli): always print errors when using the cli Prior to this change, `slick run` did not print any errors that were caused by the underlying run implementation, which required the user to use `go run` manually to find out what's wrong. This change fixes that, and also pipes the stderr of all commands into the OS' stderr which should help with debugging things. To achieve that, I abstracted the command logic and added the `commandOptions` type. I chose an options type over passing a `verbose` parameter directly, this keeps things more flexible and maintainable down the line. --- slick/main.go | 85 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 22 deletions(-) diff --git a/slick/main.go b/slick/main.go index 2c8ff83..fbdf02a 100644 --- a/slick/main.go +++ b/slick/main.go @@ -13,37 +13,76 @@ func usage() { os.Exit(0) } +type commandOptions struct { + verbose bool // Currently always false, but could be set by a flag in the future. +} + +func runCommand(opts commandOptions, name string, args ...string) error { + cmd := exec.Command(name, args...) + + if opts.verbose { + cmd.Stdout = os.Stdout + } + + cmd.Stderr = os.Stderr // We always want to see errors. + + if err := cmd.Run(); err != nil { + return fmt.Errorf("%s: %w", cmd.String(), err) + } + + return nil +} + func main() { args := os.Args if len(args) < 2 { usage() + return } - cmd := os.Args[1] + cmd := args[1] + cmdOpts := commandOptions{} + + var err error switch cmd { case "run": - if _, err := os.Stat("cmd/main.go"); err != nil { - fmt.Println("not in slick app root: cmd/main.go not found") - os.Exit(1) - } - if err := exec.Command("templ", "generate").Run(); err != nil { - fmt.Println(err) - os.Exit(1) - } - exec.Command("go", "run", "cmd/main.go").Run() + err = runProject(cmdOpts) case "install": - if err := installProject(); err != nil { - fmt.Println(err) - } + err = installProject(cmdOpts) case "new": - if len(os.Args) != 3 { + if len(args) != 3 { usage() + return } - name := os.Args[2] - if err := generateProject(name); err != nil { - fmt.Println(err) - } + + name := args[2] + err = generateProject(name) + default: + usage() + err = fmt.Errorf("unknown command: %s", cmd) + } + + if err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func runProject(cmdOpts commandOptions) error { + if _, err := os.Stat("cmd/main.go"); err != nil { + return fmt.Errorf("not in slick app root: cmd/main.go not found") + } + if err := runCommand(cmdOpts, "templ", "generate"); err != nil { + return err + } + + // We make an exception for the run command, since we want to see the output. + cmdOpts = commandOptions{verbose: true} + if err := runCommand(cmdOpts, "go", "run", "cmd/main.go"); err != nil { + return err } + + return nil } func generateProject(name string) error { @@ -97,18 +136,20 @@ func generateProject(name string) error { return nil } -func installProject() error { +func installProject(cmdOpts commandOptions) error { start := time.Now() fmt.Println("installing project...") - if err := exec.Command("go", "get", "github.com/anthdm/slick@latest").Run(); err != nil { + + if err := runCommand(cmdOpts, "go", "get", "github.com/anthdm/slick@latest"); err != nil { return err } - if err := exec.Command("go", "get", "github.com/a-h/templ").Run(); err != nil { + if err := runCommand(cmdOpts, "go", "get", "github.com/a-h/templ"); err != nil { return err } - if err := exec.Command("templ", "generate").Run(); err != nil { + if err := runCommand(cmdOpts, "templ", "generate"); err != nil { return err } + fmt.Printf("done installing project in %v\n", time.Since(start)) return nil } From 068941ea6eca11199e50d00223757775649f4cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20K=C3=B6ser?= Date: Wed, 13 Dec 2023 15:41:52 +0100 Subject: [PATCH 2/2] chore: simplify verbosity enforcement in run command --- slick/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slick/main.go b/slick/main.go index fbdf02a..8fe7c15 100644 --- a/slick/main.go +++ b/slick/main.go @@ -77,7 +77,8 @@ func runProject(cmdOpts commandOptions) error { } // We make an exception for the run command, since we want to see the output. - cmdOpts = commandOptions{verbose: true} + cmdOpts.verbose = true + if err := runCommand(cmdOpts, "go", "run", "cmd/main.go"); err != nil { return err }