// Package compose executes Docker Compose through a fixed executable and argument arrays. package compose import ( "bytes" "context" "errors" "fmt" "io" "os" "os/exec" ) // Result is the captured output and process exit code for one Docker invocation. type Result struct { Stdout string Stderr string ExitCode int } // Runner executes the Docker CLI. It never invokes a shell. type Runner struct { binary string } // NewRunner returns a runner for binary. An empty binary selects docker from PATH. func NewRunner(binary string) Runner { if binary == "" { binary = "docker" } return Runner{binary: binary} } // Run invokes Docker with the supplied argument array and optional standard input. func (r Runner) Run(ctx context.Context, args []string, stdin io.Reader) (Result, error) { command := exec.CommandContext(ctx, r.binary, args...) command.Stdin = stdin var stdout, stderr bytes.Buffer command.Stdout = &stdout command.Stderr = &stderr err := command.Run() result := Result{Stdout: stdout.String(), Stderr: stderr.String()} if err == nil { return result, nil } var exitError *exec.ExitError if errors.As(err, &exitError) { result.ExitCode = exitError.ExitCode() return result, err } if errors.Is(err, exec.ErrNotFound) || errors.Is(err, os.ErrNotExist) { result.ExitCode = 127 return result, fmt.Errorf("%w: %w", exec.ErrNotFound, err) } return result, err }