-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to run commands before trying to connect
- Loading branch information
1 parent
ddf5664
commit 7bd76cd
Showing
6 changed files
with
151 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
|
||
"github.com/mitchellh/go-linereader" | ||
|
||
"github.com/jorgerojas26/lazysql/helpers/logger" | ||
) | ||
|
||
func RunCommand(ctx context.Context, command string) error { | ||
var cmd *exec.Cmd | ||
|
||
parts := strings.Fields(command) | ||
if len(parts) == 1 { | ||
cmd = exec.CommandContext(ctx, parts[0]) | ||
} else { | ||
cmd = exec.CommandContext(ctx, parts[0], parts[1:]...) | ||
} | ||
|
||
// Create a pipe to read the output from. | ||
pr, pw := io.Pipe() | ||
startedCh := make(chan struct{}) | ||
copyDoneCh := make(chan struct{}) | ||
go logOutput(pr, startedCh, copyDoneCh) | ||
|
||
// Connect the pipe to stdout and stderr. | ||
cmd.Stderr = pw | ||
cmd.Stdout = pw | ||
|
||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
if err := cmd.Wait(); err != nil { | ||
logger.Error("Command failed", map[string]any{"error": err.Error()}) | ||
} | ||
|
||
pw.Close() | ||
<-copyDoneCh | ||
}() | ||
|
||
// Wait for the command to start | ||
select { | ||
case <-ctx.Done(): | ||
logger.Error("Command cancelled", map[string]any{"error": ctx.Err()}) | ||
case <-startedCh: | ||
logger.Info("Command started", map[string]any{"command": command}) | ||
case <-time.After(5 * time.Second): | ||
return errors.New("command timeout") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func logOutput(r io.Reader, started, doneCh chan struct{}) { | ||
defer close(doneCh) | ||
lr := linereader.New(r) | ||
|
||
// Wait for the command to start | ||
line := <-lr.Ch | ||
started <- struct{}{} | ||
logger.Debug("Command output", map[string]any{"line": line}) | ||
|
||
// Log the rest of the output | ||
for line := range lr.Ch { | ||
logger.Debug("Command output", map[string]any{"line": line}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters