update: enhance command execution with real-time logging and add shorthand flag for deployment file

This commit is contained in:
jon brookes 2025-08-16 11:45:47 +01:00
parent 4b8a27a2d7
commit 5f448d7fc7
3 changed files with 90 additions and 8 deletions

View file

@ -1,6 +1,7 @@
package app
import (
"bufio"
"bytes"
"fmt"
"log/slog"
@ -67,19 +68,52 @@ func k8sCreateNamespace(project string) error {
func RunCommand(command string) error {
slog.Debug(fmt.Sprintf("🐞 Running command: %s", command))
cmd := exec.Command("sh", "-c", command)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
// Get pipes for real-time reading
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("failed to create stdout pipe: %w", err)
}
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("failed to create stderr pipe: %w", err)
}
// Start the command
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start command: %w", err)
}
// Read stdout line by line and log through slog
go func() {
scanner := bufio.NewScanner(stdoutPipe)
for scanner.Scan() {
line := scanner.Text()
stdout.WriteString(line + "\n")
slog.Info(line)
}
}()
// Read stderr line by line and log through slog
go func() {
scanner := bufio.NewScanner(stderrPipe)
for scanner.Scan() {
line := scanner.Text()
stderr.WriteString(line + "\n")
slog.Error(line)
}
}()
// Wait for command to complete
err = cmd.Wait()
if err != nil {
slog.Error(fmt.Sprintf("❌ Command failed with error: %v\n", err))
slog.Debug(fmt.Sprintf("🐞 Stdout: %s\n", stdout.String()))
slog.Debug(fmt.Sprintf("🐞 Stderr: %s\n", stderr.String()))
return fmt.Errorf("failed to run script command: %w", err)
}
output := stdout.String()
slog.Debug(fmt.Sprintf("RunCommand executed successfully: %s", command))
slog.Debug(fmt.Sprintf("RunCommand output: %s", output))
return nil
}