summaryrefslogtreecommitdiff
path: root/internal/askcli/command_urgency.go
blob: a228f27856cdc5a3548bccaa779c0c3fdde87f80 (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
package askcli

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"sort"
)

func (d Dispatcher) handleUrgency(ctx context.Context, stdout, stderr io.Writer) (int, error) {
	var outBuf bytes.Buffer
	code, err := d.runner.Run(ctx, []string{"export"}, nil, &outBuf, stderr)
	if code != 0 {
		return code, err
	}
	tasks, err := ParseTaskExport(&outBuf)
	if err != nil {
		fmt.Fprintf(stderr, "error: failed to parse task data: %v\n", err)
		return 1, nil
	}
	sort.Slice(tasks, func(i, j int) bool {
		return tasks[i].Urgency > tasks[j].Urgency
	})
	if d.jsonOutput {
		data, err := json.Marshal(tasks)
		if err != nil {
			fmt.Fprintf(stderr, "error: failed to marshal JSON: %v\n", err)
			return 1, nil
		}
		stdout.Write(data)
		io.WriteString(stdout, "\n")
	} else {
		io.WriteString(stdout, FormatTaskList(tasks))
	}
	return 0, nil
}