Skip to content

Commit

Permalink
Fixed interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
Andriiklymiuk committed Sep 2, 2024
1 parent 26a3166 commit 4e82742
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ dist/
corgi_services/db_services
corgi-compose*.yml
.env*
corgi_services/*
corgi_services/*
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.7.3"
var APP_VERSION = "1.7.4"

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down
78 changes: 48 additions & 30 deletions utils/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,43 +80,61 @@ func RunServiceCmd(
}

cmd := exec.Command("/bin/sh", "-c", finalCommand)

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

if interactive {
cmd.Stdin = os.Stdin
}
SetProcessGroup(cmd)
if err := cmd.Start(); err != nil {
return err
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

done := make(chan error, 1)

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

go func() {
done <- cmd.Wait()
}()

addProcess(cmd.Process)

if err := cmd.Wait(); err != nil {
removeProcess(cmd.Process)
// Check the error directly
if strings.Contains(err.Error(), "executable file not found") {
// Attempt to install missing command
missingCommand := commandSlice[0]
if cmdInfo, ok := CommandInstructions[missingCommand]; ok {
fmt.Printf("\n❗%s is missing. Attempting to install it using: %s\n", missingCommand, cmdInfo.Install)
installCmd := exec.Command("/bin/bash", "-c", cmdInfo.Install)
installCmd.Dir = path
installCmd.Stdout = os.Stdout
installCmd.Stderr = os.Stderr
if err := installCmd.Run(); err != nil {
return fmt.Errorf("failed to install %s: %v", missingCommand, err)
if err := <-done; err != nil {
return err
}
} else {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
SetProcessGroup(cmd)
if err := cmd.Start(); err != nil {
return err
}

addProcess(cmd.Process)

if err := cmd.Wait(); err != nil {
removeProcess(cmd.Process)
// Check the error directly
if strings.Contains(err.Error(), "executable file not found") {
// Attempt to install missing command
missingCommand := commandSlice[0]
if cmdInfo, ok := CommandInstructions[missingCommand]; ok {
fmt.Printf("\n❗%s is missing. Attempting to install it using: %s\n", missingCommand, cmdInfo.Install)
installCmd := exec.Command("/bin/bash", "-c", cmdInfo.Install)
installCmd.Dir = path
installCmd.Stdout = os.Stdout
installCmd.Stderr = os.Stderr
if err := installCmd.Run(); err != nil {
return fmt.Errorf("failed to install %s: %v", missingCommand, err)
}
// Rerun the original command
fmt.Printf("\n🔄 Retrying the command: %s\n", finalCommand)
return RunServiceCmd(serviceName, finalCommand, path, interactive)
} else {
return fmt.Errorf("unknown command %s, no install instructions found", missingCommand)
}
// Rerun the original command
fmt.Printf("\n🔄 Retrying the command: %s\n", finalCommand)
return RunServiceCmd(serviceName, finalCommand, path, false)
} else {
return fmt.Errorf("unknown command %s, no install instructions found", missingCommand)
return err
}
} else {
return err
}
}
}
Expand Down

0 comments on commit 4e82742

Please sign in to comment.