summaryrefslogtreecommitdiff
path: root/internal/taskproxy
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-19 09:26:26 +0200
committerPaul Buetow <paul@buetow.org>2026-03-19 09:26:26 +0200
commit15bc73d103259b492f8b77a422f8649bdf3d7c24 (patch)
tree58a834d1cab371ce9acc2b25780524223c06980c /internal/taskproxy
parent5642eaf74a4a70e5c82646bef3e0dd42846baea8 (diff)
Add ask Taskwarrior wrapper
Diffstat (limited to 'internal/taskproxy')
-rw-r--r--internal/taskproxy/run.go129
-rw-r--r--internal/taskproxy/run_test.go141
2 files changed, 270 insertions, 0 deletions
diff --git a/internal/taskproxy/run.go b/internal/taskproxy/run.go
new file mode 100644
index 0000000..7654b7b
--- /dev/null
+++ b/internal/taskproxy/run.go
@@ -0,0 +1,129 @@
+package taskproxy
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "io"
+ "os/exec"
+ "path/filepath"
+ "strings"
+)
+
+type binaryFinder func() (string, error)
+
+type repoTopLevelDetector func(context.Context) (string, error)
+
+type commandRunner func(context.Context, string, []string, io.Reader, io.Writer, io.Writer) error
+
+type Runner struct {
+ CommandName string
+ findTaskBinary binaryFinder
+ detectRepoRoot repoTopLevelDetector
+ runCommand commandRunner
+}
+
+func NewRunner(commandName string) Runner {
+ return Runner{
+ CommandName: strings.TrimSpace(commandName),
+ findTaskBinary: findTaskBinary,
+ detectRepoRoot: detectRepoRoot,
+ runCommand: runTaskCommand,
+ }
+}
+
+func (r Runner) Run(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
+ runner := normalizeRunner(r)
+ taskPath, err := runner.findTaskBinary()
+ if err != nil {
+ return 1, fmt.Errorf("%s: Taskwarrior binary lookup failed: %w", runner.commandLabel(), err)
+ }
+ repoRoot, err := runner.detectRepoRoot(ctx)
+ if err != nil {
+ return 1, fmt.Errorf("%s: must be run inside a git repository so project:<repo> can be derived: %w", runner.commandLabel(), err)
+ }
+ taskArgs, err := runner.taskArgs(repoRoot, args)
+ if err != nil {
+ return 1, fmt.Errorf("%s: %w", runner.commandLabel(), err)
+ }
+ if err := runner.runCommand(ctx, taskPath, taskArgs, stdin, stdout, stderr); err != nil {
+ return runner.exitCodeFor(err)
+ }
+ return 0, nil
+}
+
+func normalizeRunner(r Runner) Runner {
+ if r.CommandName == "" {
+ r.CommandName = "task"
+ }
+ if r.findTaskBinary == nil {
+ r.findTaskBinary = findTaskBinary
+ }
+ if r.detectRepoRoot == nil {
+ r.detectRepoRoot = detectRepoRoot
+ }
+ if r.runCommand == nil {
+ r.runCommand = runTaskCommand
+ }
+ return r
+}
+
+func (r Runner) commandLabel() string {
+ label := strings.TrimSpace(r.CommandName)
+ if label == "" {
+ return "task"
+ }
+ return label
+}
+
+func (r Runner) taskArgs(repoRoot string, args []string) ([]string, error) {
+ projectName, err := projectNameFromRoot(repoRoot)
+ if err != nil {
+ return nil, err
+ }
+ return append([]string{"project:" + projectName, "+agent"}, args...), nil
+}
+
+func (r Runner) exitCodeFor(err error) (int, error) {
+ var exitErr *exec.ExitError
+ if errors.As(err, &exitErr) {
+ return exitErr.ExitCode(), nil
+ }
+ return 1, fmt.Errorf("%s: failed to run Taskwarrior: %w", r.commandLabel(), err)
+}
+
+func projectNameFromRoot(repoRoot string) (string, error) {
+ projectName := filepath.Base(strings.TrimSpace(repoRoot))
+ if projectName == "" || projectName == "." || projectName == string(filepath.Separator) {
+ return "", fmt.Errorf("could not derive project name from git root %q", repoRoot)
+ }
+ return projectName, nil
+}
+
+func findTaskBinary() (string, error) {
+ path, err := exec.LookPath("task")
+ if err != nil {
+ return "", fmt.Errorf("Taskwarrior binary 'task' not found in PATH; install Taskwarrior and retry")
+ }
+ return path, nil
+}
+
+func detectRepoRoot(ctx context.Context) (string, error) {
+ out, err := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel").Output()
+ if err != nil {
+ return "", fmt.Errorf("must be run inside a git repository so project:<repo> can be derived")
+ }
+ root := strings.TrimSpace(string(out))
+ if root == "" {
+ return "", fmt.Errorf("git returned an empty repository root")
+ }
+ return root, nil
+}
+
+func runTaskCommand(ctx context.Context, name string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
+ cmd := exec.CommandContext(ctx, name, args...)
+ cmd.Stdin = stdin
+ cmd.Stdout = stdout
+ cmd.Stderr = stderr
+ return cmd.Run()
+}
diff --git a/internal/taskproxy/run_test.go b/internal/taskproxy/run_test.go
new file mode 100644
index 0000000..15c5fbb
--- /dev/null
+++ b/internal/taskproxy/run_test.go
@@ -0,0 +1,141 @@
+package taskproxy
+
+import (
+ "bytes"
+ "context"
+ "errors"
+ "io"
+ "os/exec"
+ "reflect"
+ "strings"
+ "testing"
+)
+
+func TestRunnerRun_InjectsProjectFilterAndAgentTag(t *testing.T) {
+ var gotName string
+ var gotArgs []string
+ runner := Runner{
+ CommandName: "ask",
+ findTaskBinary: func() (string, error) { return "/usr/bin/task", nil },
+ detectRepoRoot: func(context.Context) (string, error) { return "/tmp/work/hexai", nil },
+ runCommand: func(_ context.Context, name string, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
+ gotName = name
+ gotArgs = append([]string(nil), args...)
+ return nil
+ },
+ }
+
+ exitCode, err := runner.Run(context.Background(), []string{"list", "limit:1"}, strings.NewReader("in"), &bytes.Buffer{}, &bytes.Buffer{})
+ if err != nil {
+ t.Fatalf("Run returned error: %v", err)
+ }
+ if exitCode != 0 {
+ t.Fatalf("exitCode = %d, want 0", exitCode)
+ }
+ if gotName != "/usr/bin/task" {
+ t.Fatalf("task binary = %q, want /usr/bin/task", gotName)
+ }
+ wantArgs := []string{"project:hexai", "+agent", "list", "limit:1"}
+ if !reflect.DeepEqual(gotArgs, wantArgs) {
+ t.Fatalf("task args = %v, want %v", gotArgs, wantArgs)
+ }
+}
+
+func TestRunnerRun_OutsideGitRepo_IsActionable(t *testing.T) {
+ runner := Runner{
+ CommandName: "ask",
+ findTaskBinary: func() (string, error) { return "/usr/bin/task", nil },
+ detectRepoRoot: func(context.Context) (string, error) { return "", errors.New("git failed") },
+ runCommand: func(context.Context, string, []string, io.Reader, io.Writer, io.Writer) error {
+ t.Fatal("runCommand should not be called when repo detection fails")
+ return nil
+ },
+ }
+
+ exitCode, err := runner.Run(context.Background(), []string{"list"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
+ if exitCode != 1 {
+ t.Fatalf("exitCode = %d, want 1", exitCode)
+ }
+ if err == nil || !strings.Contains(err.Error(), "must be run inside a git repository") {
+ t.Fatalf("expected actionable git-repo error, got %v", err)
+ }
+}
+
+func TestRunnerRun_PreservesTaskwarriorExitCode(t *testing.T) {
+ runner := Runner{
+ CommandName: "ask",
+ findTaskBinary: func() (string, error) { return "/usr/bin/task", nil },
+ detectRepoRoot: func(context.Context) (string, error) { return "/tmp/work/hexai", nil },
+ runCommand: func(context.Context, string, []string, io.Reader, io.Writer, io.Writer) error {
+ return exec.Command("sh", "-c", "exit 7").Run()
+ },
+ }
+
+ exitCode, err := runner.Run(context.Background(), []string{"list"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
+ if err != nil {
+ t.Fatalf("expected nil error for subprocess exit, got %v", err)
+ }
+ if exitCode != 7 {
+ t.Fatalf("exitCode = %d, want 7", exitCode)
+ }
+}
+
+func TestRunnerRun_PreservesStdoutAndStderr(t *testing.T) {
+ var stdout bytes.Buffer
+ var stderr bytes.Buffer
+ runner := Runner{
+ CommandName: "ask",
+ findTaskBinary: func() (string, error) { return "/usr/bin/task", nil },
+ detectRepoRoot: func(context.Context) (string, error) { return "/tmp/work/hexai", nil },
+ runCommand: func(_ context.Context, name string, args []string, stdin io.Reader, out, errOut io.Writer) error {
+ _, _ = io.WriteString(out, "task stdout")
+ _, _ = io.WriteString(errOut, "task stderr")
+ return nil
+ },
+ }
+
+ exitCode, err := runner.Run(context.Background(), []string{"list"}, strings.NewReader(""), &stdout, &stderr)
+ if err != nil {
+ t.Fatalf("Run returned error: %v", err)
+ }
+ if exitCode != 0 {
+ t.Fatalf("exitCode = %d, want 0", exitCode)
+ }
+ if stdout.String() != "task stdout" {
+ t.Fatalf("stdout = %q, want %q", stdout.String(), "task stdout")
+ }
+ if stderr.String() != "task stderr" {
+ t.Fatalf("stderr = %q, want %q", stderr.String(), "task stderr")
+ }
+}
+
+func TestRunnerRun_TaskLookupFailure_IsActionable(t *testing.T) {
+ runner := Runner{
+ CommandName: "ask",
+ findTaskBinary: func() (string, error) { return "", errors.New("not found") },
+ }
+
+ exitCode, err := runner.Run(context.Background(), []string{"list"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
+ if exitCode != 1 {
+ t.Fatalf("exitCode = %d, want 1", exitCode)
+ }
+ if err == nil || !strings.Contains(err.Error(), "Taskwarrior binary lookup failed") {
+ t.Fatalf("expected actionable task lookup error, got %v", err)
+ }
+}
+
+func TestRunnerRun_EmptyRepoName_IsActionable(t *testing.T) {
+ runner := Runner{
+ CommandName: "ask",
+ findTaskBinary: func() (string, error) { return "/usr/bin/task", nil },
+ detectRepoRoot: func(context.Context) (string, error) { return "/", nil },
+ }
+
+ exitCode, err := runner.Run(context.Background(), []string{"list"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
+ if exitCode != 1 {
+ t.Fatalf("exitCode = %d, want 1", exitCode)
+ }
+ if err == nil || !strings.Contains(err.Error(), "could not derive project name") {
+ t.Fatalf("expected actionable project-name error, got %v", err)
+ }
+}