feat: add cross-platform thothctl

This commit is contained in:
2026-08-04 16:48:40 +02:00
parent e2264ee295
commit 853a151796
11 changed files with 730 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
// 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
}
@@ -0,0 +1,66 @@
package compose
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestRunnerPassesEachArgumentWithoutShellSplitting(t *testing.T) {
t.Parallel()
runner := NewRunner(writeExecutable(t, "#!/bin/sh\nprintf '<%s>\\n' \"$@\"\ncat\n"))
result, err := runner.Run(context.Background(), []string{"compose", "--project-directory", "/tmp/a project with spaces", "config"}, strings.NewReader("stdin value\n"))
if err != nil {
t.Fatalf("Run() error = %v", err)
}
want := "<compose>\n<--project-directory>\n</tmp/a project with spaces>\n<config>\nstdin value\n"
if result.Stdout != want {
t.Errorf("stdout = %q, want %q", result.Stdout, want)
}
if result.ExitCode != 0 {
t.Errorf("ExitCode = %d, want 0", result.ExitCode)
}
}
func TestRunnerReturnsTheChildExitCode(t *testing.T) {
t.Parallel()
runner := NewRunner(writeExecutable(t, "#!/bin/sh\necho unavailable >&2\nexit 42\n"))
result, err := runner.Run(context.Background(), []string{"compose", "ps"}, nil)
if err == nil {
t.Fatal("Run() error = nil, want child exit error")
}
if result.ExitCode != 42 {
t.Errorf("ExitCode = %d, want 42", result.ExitCode)
}
if result.Stderr != "unavailable\n" {
t.Errorf("stderr = %q, want unavailable output", result.Stderr)
}
}
func TestRunnerReportsMissingDocker(t *testing.T) {
t.Parallel()
runner := NewRunner(filepath.Join(t.TempDir(), "docker-does-not-exist"))
result, err := runner.Run(context.Background(), []string{"compose", "version"}, nil)
if !errors.Is(err, exec.ErrNotFound) {
t.Fatalf("Run() error = %v, want exec.ErrNotFound", err)
}
if result.ExitCode != 127 {
t.Errorf("ExitCode = %d, want 127", result.ExitCode)
}
}
func writeExecutable(t *testing.T, contents string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "fake-docker")
if err := os.WriteFile(path, []byte(contents), 0o700); err != nil {
t.Fatal(err)
}
return path
}