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

import "context"

type taskScopeMode int

const (
	taskScopeAgent taskScopeMode = iota
	taskScopeNoAgent
)

type taskScopeContextKey struct{}

func contextWithTaskScope(ctx context.Context, scope taskScopeMode) context.Context {
	if scope == taskScopeAgent {
		return ctx
	}
	return context.WithValue(ctx, taskScopeContextKey{}, scope)
}

func taskScopeFromContext(ctx context.Context) taskScopeMode {
	if ctx == nil {
		return taskScopeAgent
	}
	scope, ok := ctx.Value(taskScopeContextKey{}).(taskScopeMode)
	if !ok {
		return taskScopeAgent
	}
	return scope
}

func taskScopeFilter(scope taskScopeMode) string {
	if scope == taskScopeNoAgent {
		return "-agent"
	}
	return "+agent"
}

func parseTaskScopePrefix(args []string) (taskScopeMode, []string) {
	if len(args) == 0 {
		return taskScopeAgent, nil
	}
	if isTaskScopePrefix(args[0]) {
		return taskScopeNoAgent, args[1:]
	}
	return taskScopeAgent, args
}

func isTaskScopePrefix(arg string) bool {
	switch arg {
	case "na", "no-agent":
		return true
	default:
		return false
	}
}

func trimTaskScopePrefix(args []string) []string {
	if len(args) == 0 || !isTaskScopePrefix(args[0]) {
		return args
	}
	return args[1:]
}