package loader

import (
	"context"
	"errors"
	"os"
	"os/exec"
)

// uiRunner executes the standalone UI artifact and returns its exit code.
type uiRunner interface {
	Run(context.Context, string) (int, error)
}

type execRunner struct{}

func (execRunner) Run(ctx context.Context, path string) (int, error) {
	cmd := exec.CommandContext(ctx, path)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin

	err := cmd.Run()
	if err == nil {
		return 0, nil
	}

	var exitErr *exec.ExitError
	if errors.As(err, &exitErr) {
		return exitErr.ExitCode(), nil
	}

	return -1, err
}
