Skip to content

Commit

Permalink
feat: add geng run for go run main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mukezhz committed Dec 7, 2023
1 parent d55b18e commit 3da01b2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
18 changes: 17 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ var newProjectCmd = &cobra.Command{
Run: createProject,
}

var runProjectCmd = &cobra.Command{
Use: "run [project name]",
Short: "Run the project",
Args: cobra.MaximumNArgs(1),
Run: runProject,
}

func init() {
newProjectCmd.Flags().StringP("mod", "m", "", "features name")
newProjectCmd.Flags().StringP("dir", "d", "", "target directory")
newProjectCmd.Flags().StringP("version", "v", "", "version support")
rootCmd.AddCommand(newModuleCmd, newProjectCmd)
rootCmd.AddCommand(newModuleCmd, newProjectCmd, runProjectCmd)
}

func main() {
Expand Down Expand Up @@ -178,3 +185,12 @@ func createProject(cmd *cobra.Command, args []string) {
utility.PrintColorizeProjectDetail(data)
fmt.Println("")
}

func runProject(cmd *cobra.Command, args []string) {
runGo := "go"
// execute command from golang
err := utility.ExecuteCommand(runGo, args...)
if err != nil {
return
}
}
33 changes: 33 additions & 0 deletions pkg/utility/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utility

import (
"os"
"os/exec"
)

func ExecuteCommand(command string, args ...string) error {
var cmd *exec.Cmd
runCommand := []string{"run", "main.go"}
if len(args) == 0 {
cmd = exec.Command(command, runCommand...)
} else {
runCommand = append(runCommand, args...)
cmd = exec.Command(command, runCommand...)
}

cmd.Dir = "." // Ensure this is correct

// Set Stdout and Stderr to os.Stdout, os.Stderr respectively
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

// Start the command and check for errors
if err := cmd.Start(); err != nil {
return err
}

if err := cmd.Wait(); err != nil {
return err
}
return nil
}

0 comments on commit 3da01b2

Please sign in to comment.