summaryrefslogtreecommitdiff
path: root/internal/askcli/command_write.go
blob: d8dbf5bc2ee954efc1139e64ea20d813a4b00fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package askcli

import (
	"bytes"
	"context"
	"io"
	"strings"
)

func (d *Dispatcher) runSingleTaskCommand(
	ctx context.Context,
	selector string,
	stdout, stderr io.Writer,
	buildArgs func(resolvedTaskSelector) []string,
) (int, error) {
	resolved, _, code, err := d.resolveTaskSelector(ctx, selector, stderr)
	if err != nil {
		writeInfoError(stderr, err)
		return code, nil
	}

	var outBuf bytes.Buffer
	code, err = d.runner.Run(ctx, buildArgs(resolved), nil, &outBuf, io.Discard)
	if code != 0 {
		return code, err
	}

	_, _ = io.WriteString(stdout, FormatSuccess(displayResolvedTaskID(resolved)))
	return 0, nil
}

func (d *Dispatcher) handleDenotate(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
	if len(args) < 3 {
		_, _ = io.WriteString(stderr, "error: ask denotate requires an ID or UUID and text argument\n")
		return 1, nil
	}
	text := args[2]
	return d.runSingleTaskCommand(ctx, args[1], stdout, stderr, func(resolved resolvedTaskSelector) []string {
		return []string{"uuid:" + resolved.UUID, "denotate", text}
	})
}

func (d *Dispatcher) handleModify(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
	if len(args) < 3 {
		_, _ = io.WriteString(stderr, "error: ask modify requires an ID or UUID and modification args\n")
		return 1, nil
	}
	modArgs := args[2:]
	return d.runSingleTaskCommand(ctx, args[1], stdout, stderr, func(resolved resolvedTaskSelector) []string {
		return append([]string{"uuid:" + resolved.UUID, "modify"}, modArgs...)
	})
}

func (d *Dispatcher) handleAnnotate(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
	if len(args) < 3 {
		_, _ = io.WriteString(stderr, "error: ask annotate requires an ID or UUID and note argument\n")
		return 1, nil
	}
	note := strings.Join(args[2:], " ")
	return d.runSingleTaskCommand(ctx, args[1], stdout, stderr, func(resolved resolvedTaskSelector) []string {
		return []string{"uuid:" + resolved.UUID, "annotate", note}
	})
}

func (d *Dispatcher) handleStart(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
	if len(args) < 2 {
		_, _ = io.WriteString(stderr, "error: ask start requires an ID or UUID argument\n")
		return 1, nil
	}
	return d.runSingleTaskCommand(ctx, args[1], stdout, stderr, func(resolved resolvedTaskSelector) []string {
		// uuid:<uuid> is used as the filter so taskwarrior selects the exact task;
		// the action verb follows the filter.
		return []string{"uuid:" + resolved.UUID, "start"}
	})
}

func (d *Dispatcher) handleStop(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
	if len(args) < 2 {
		_, _ = io.WriteString(stderr, "error: ask stop requires an ID or UUID argument\n")
		return 1, nil
	}
	return d.runSingleTaskCommand(ctx, args[1], stdout, stderr, func(resolved resolvedTaskSelector) []string {
		return []string{"uuid:" + resolved.UUID, "stop"}
	})
}

func (d *Dispatcher) handleDone(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
	if len(args) < 2 {
		_, _ = io.WriteString(stderr, "error: ask done requires an ID or UUID argument\n")
		return 1, nil
	}
	return d.runSingleTaskCommand(ctx, args[1], stdout, stderr, func(resolved resolvedTaskSelector) []string {
		return []string{"uuid:" + resolved.UUID, "done"}
	})
}

func (d *Dispatcher) handlePriority(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
	if len(args) < 3 {
		_, _ = io.WriteString(stderr, "error: ask priority requires an ID or UUID and priority (H/M/L)\n")
		return 1, nil
	}
	priority := args[2]
	return d.runSingleTaskCommand(ctx, args[1], stdout, stderr, func(resolved resolvedTaskSelector) []string {
		return []string{"uuid:" + resolved.UUID, "modify", "priority:" + priority}
	})
}

func (d *Dispatcher) handleTag(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
	if len(args) < 3 {
		_, _ = io.WriteString(stderr, "error: ask tag requires an ID or UUID and +/-tag\n")
		return 1, nil
	}
	tag := args[2]
	return d.runSingleTaskCommand(ctx, args[1], stdout, stderr, func(resolved resolvedTaskSelector) []string {
		return []string{"uuid:" + resolved.UUID, "modify", tag}
	})
}