Skip to content

Commit 5253acf

Browse files
committed
Add run command that proxies commands to the actual app
1 parent 32c64f3 commit 5253acf

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

cli.go renamed to new.go

File renamed without changes.

root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func Execute() error {
3434
genCmd.AddCommand(formCmd)
3535

3636
AddCmd(newCmd)
37+
AddCmd(runCmd)
3738
AddCmd(genCmd)
3839

3940
return rootCmd.Execute()

run.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
const lemmegoIndicator = "github.com/lemmego/api/app"
14+
15+
var runCmd = &cobra.Command{
16+
Use: "run [args]",
17+
Short: "Run the Lemmego application with optional arguments",
18+
Run: func(cmd *cobra.Command, args []string) {
19+
// Check if we're in a Lemmego project
20+
if !isLemmegoProject() {
21+
fmt.Println("Error: This does not appear to be a Lemmego project directory.")
22+
return
23+
}
24+
25+
// Construct the go run command
26+
goRunCmd := exec.Command("go", append([]string{"run", "./cmd/app"}, args...)...)
27+
28+
// Set up stdout and stderr to be the same as the parent's
29+
goRunCmd.Stdout = os.Stdout
30+
goRunCmd.Stderr = os.Stderr
31+
32+
// Execute the command
33+
if err := goRunCmd.Run(); err != nil {
34+
fmt.Printf("Error running the app: %v\n", err)
35+
return
36+
}
37+
},
38+
}
39+
40+
// Function to check if the current directory is a Lemmego project
41+
func isLemmegoProject() bool {
42+
mainGoPath := filepath.Join("./cmd/app", "main.go")
43+
content, err := os.ReadFile(mainGoPath)
44+
if err != nil {
45+
return false
46+
}
47+
return strings.Contains(string(content), lemmegoIndicator)
48+
}

0 commit comments

Comments
 (0)