diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-22 20:14:18 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-22 20:14:18 +0200 |
| commit | f306a6b5981f93562f3eed2087ee9c53fb01520b (patch) | |
| tree | e4979125dd9372c5f915d7c098e4fca154e1699a | |
| parent | 9a6f6c0b747cee47eb61220d94a5fb7aaadd0e2b (diff) | |
Implement 'ask dep add/rm/list' subcommands
| -rw-r--r-- | internal/askcli/command_dep.go | 89 | ||||
| -rw-r--r-- | internal/askcli/command_dep_test.go | 75 | ||||
| -rw-r--r-- | internal/askcli/dispatch.go | 4 | ||||
| -rw-r--r-- | internal/askcli/dispatch_test.go | 4 |
4 files changed, 171 insertions, 1 deletions
diff --git a/internal/askcli/command_dep.go b/internal/askcli/command_dep.go new file mode 100644 index 0000000..a7df0cb --- /dev/null +++ b/internal/askcli/command_dep.go @@ -0,0 +1,89 @@ +package askcli + +import ( + "bytes" + "context" + "fmt" + "io" + "strings" +) + +func (d Dispatcher) handleDep(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) { + if len(args) < 2 { + io.WriteString(stderr, "error: ask dep requires an operation (add/rm/list) and arguments\n") + return 1, nil + } + op := args[1] + switch op { + case "add", "rm": + return d.handleDepAddRm(ctx, args, stdout, stderr) + case "list": + return d.handleDepList(ctx, args, stdout, stderr) + default: + fmt.Fprintf(stderr, "error: ask dep: unknown operation %q (use add, rm, or list)\n", op) + return 1, nil + } +} + +func (d Dispatcher) handleDepAddRm(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) { + if len(args) < 4 { + io.WriteString(stderr, "error: ask dep add/rm requires <uuid> <dep-uuid>\n") + return 1, nil + } + uuid := args[2] + if IsNumericID(uuid) { + io.WriteString(stderr, RejectNumericID()) + return 1, nil + } + depUUID := args[3] + if IsNumericID(depUUID) { + io.WriteString(stderr, RejectNumericID()) + return 1, nil + } + op := args[1] + var modArg string + if op == "add" { + modArg = "depends:" + depUUID + } else { + modArg = "depends:-" + depUUID + } + var outBuf bytes.Buffer + code, err := d.runner.Run(ctx, []string{"modify", uuid, modArg}, nil, &outBuf, io.Discard) + if code != 0 { + return code, err + } + io.WriteString(stdout, FormatSuccess(uuid)) + return 0, nil +} + +func (d Dispatcher) handleDepList(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) { + if len(args) < 3 { + io.WriteString(stderr, "error: ask dep list requires <uuid>\n") + return 1, nil + } + uuid := args[2] + if IsNumericID(uuid) { + io.WriteString(stderr, RejectNumericID()) + return 1, nil + } + var outBuf bytes.Buffer + code, err := d.runner.Run(ctx, []string{"info", uuid}, nil, &outBuf, stderr) + if code != 0 { + return code, err + } + tasks, err := ParseTaskExport(&outBuf) + if err != nil { + return 1, nil + } + if len(tasks) == 0 { + io.WriteString(stdout, "no dependencies\n") + return 0, nil + } + task := tasks[0] + if len(task.Depends) == 0 { + io.WriteString(stdout, "no dependencies\n") + } else { + io.WriteString(stdout, strings.Join(task.Depends, "\n")+"\n") + } + return 0, nil +} diff --git a/internal/askcli/command_dep_test.go b/internal/askcli/command_dep_test.go new file mode 100644 index 0000000..c77bc1a --- /dev/null +++ b/internal/askcli/command_dep_test.go @@ -0,0 +1,75 @@ +package askcli + +import ( + "bytes" + "context" + "io" + "strings" + "testing" +) + +func TestHandleDep_AddSuccess(t *testing.T) { + d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { + return 0, nil + }}) + var stdout, stderr bytes.Buffer + code, _ := d.Dispatch(context.Background(), []string{"dep", "add", "uuid-1", "uuid-2"}, nil, &stdout, &stderr) + if code != 0 { + t.Fatalf("dep add code = %d, want 0", code) + } + if !strings.Contains(stdout.String(), "ok") || !strings.Contains(stdout.String(), "uuid-1") { + t.Fatalf("stdout = %q, want ok + uuid", stdout.String()) + } +} + +func TestHandleDep_RmSuccess(t *testing.T) { + d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { + return 0, nil + }}) + var stdout, stderr bytes.Buffer + code, _ := d.Dispatch(context.Background(), []string{"dep", "rm", "uuid-1", "uuid-2"}, nil, &stdout, &stderr) + if code != 0 { + t.Fatalf("dep rm code = %d, want 0", code) + } +} + +func TestHandleDep_ListSuccess(t *testing.T) { + jsonData := `[{"uuid":"uuid-1","description":"Task","status":"pending","priority":"M","tags":[],"urgency":10,"depends":["dep-1","dep-2"]}]` + d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { + if args[0] == "info" { + io.WriteString(stdout, jsonData) + } + return 0, nil + }}) + var stdout, stderr bytes.Buffer + code, _ := d.Dispatch(context.Background(), []string{"dep", "list", "uuid-1"}, nil, &stdout, &stderr) + if code != 0 { + t.Fatalf("dep list code = %d, want 0", code) + } + output := stdout.String() + if !strings.Contains(output, "dep-1") || !strings.Contains(output, "dep-2") { + t.Fatalf("stdout = %q, want deps", output) + } +} + +func TestHandleDep_UnknownOp(t *testing.T) { + d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { + return 0, nil + }}) + var stdout, stderr bytes.Buffer + code, _ := d.Dispatch(context.Background(), []string{"dep", "unknown", "uuid-1", "uuid-2"}, nil, &stdout, &stderr) + if code != 1 { + t.Fatalf("dep unknown code = %d, want 1", code) + } +} + +func TestHandleDep_NumericUUID(t *testing.T) { + d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { + return 0, nil + }}) + var stdout, stderr bytes.Buffer + code, _ := d.Dispatch(context.Background(), []string{"dep", "add", "123", "uuid-2"}, nil, &stdout, &stderr) + if code != 1 { + t.Fatalf("dep add code = %d, want 1 for numeric UUID", code) + } +} diff --git a/internal/askcli/dispatch.go b/internal/askcli/dispatch.go index b0c9e18..399e346 100644 --- a/internal/askcli/dispatch.go +++ b/internal/askcli/dispatch.go @@ -28,8 +28,10 @@ func (d Dispatcher) Dispatch(ctx context.Context, args []string, stdin io.Reader } subcommand := args[0] switch subcommand { - case "add", "list", "info", "dep", "export": + case "add", "list", "info", "export": return d.runner.Run(ctx, args, stdin, stdout, stderr) + case "dep": + return d.handleDep(ctx, args, stdout, stderr) case "urgency": return d.handleUrgency(ctx, stdout, stderr) case "annotate": diff --git a/internal/askcli/dispatch_test.go b/internal/askcli/dispatch_test.go index 8631f4c..0a04490 100644 --- a/internal/askcli/dispatch_test.go +++ b/internal/askcli/dispatch_test.go @@ -70,6 +70,7 @@ func TestDispatcher_AllSubcommandsReachExecutor(t *testing.T) { "done": {"done", "test-uuid"}, "priority": {"priority", "test-uuid", "H"}, "tag": {"tag", "test-uuid", "+cli"}, + "dep": {"dep", "list", "test-uuid"}, } for _, sub := range subcommands { var stdout, stderr bytes.Buffer @@ -79,6 +80,9 @@ func TestDispatcher_AllSubcommandsReachExecutor(t *testing.T) { if args[0] == "export" { io.WriteString(stdout_, "[]") } + if args[0] == "info" { + io.WriteString(stdout_, "[]") + } return 0, nil }}) args := []string{sub} |
