Skip to content

Commit

Permalink
Improve multiline cmd handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Andriiklymiuk committed Sep 4, 2023
1 parent b962918 commit c3867be
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

var APP_VERSION = "1.1.80"
var APP_VERSION = "1.1.81"

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down
44 changes: 36 additions & 8 deletions utils/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,45 @@ import (
)

func RunServiceCmd(serviceName string, serviceCommand string, path string) error {
executingMessage := fmt.Sprintf("\n🚀 🤖 Executing command for %s: ", serviceName)
fmt.Println(executingMessage, art.GreenColor, serviceCommand, art.WhiteColor)
lines := strings.Split(serviceCommand, "\n")
var accumulatedCommand string

commandSlice := strings.Fields(serviceCommand)
cmd := exec.Command(commandSlice[0], commandSlice[1:]...)
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}

// If the line ends with a backslash, remove it and append the line to the next
if strings.HasSuffix(line, "\\") {
accumulatedCommand += strings.TrimSuffix(line, "\\") + " "
continue
}

// Execute the accumulated command if any, otherwise execute the line itself
finalCommand := line
if accumulatedCommand != "" {
finalCommand = accumulatedCommand + line
accumulatedCommand = ""
}
executingMessage := fmt.Sprintf("\n🚀 🤖 Executing command for %s: ", serviceName)
fmt.Println(executingMessage, art.GreenColor, finalCommand, art.WhiteColor)

commandSlice := strings.Fields(finalCommand)
if len(commandSlice) == 0 {
continue
}
cmd := exec.Command(commandSlice[0], commandSlice[1:]...)

cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

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

func RunCombinedCmd(command string, path string) error {
Expand Down

0 comments on commit c3867be

Please sign in to comment.