From f306a6b5981f93562f3eed2087ee9c53fb01520b Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 22 Mar 2026 20:14:18 +0200 Subject: Implement 'ask dep add/rm/list' subcommands --- internal/askcli/command_dep.go | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 internal/askcli/command_dep.go (limited to 'internal/askcli/command_dep.go') 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 \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 \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 +} -- cgit v1.2.3