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

import (
	"context"
	"strings"
)

type taskScopeMode int

const (
	taskScopeAgent taskScopeMode = iota
	taskScopeNoAgent
)

type taskScopeContextKey struct{}

type taskProjectContextKey struct{}

type taskProjectContextValue struct {
	project string
}

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 contextWithTaskProject(ctx context.Context, project string) context.Context {
	if ctx == nil {
		ctx = context.Background()
	}
	return context.WithValue(ctx, taskProjectContextKey{}, taskProjectContextValue{project: project})
}

func taskProjectFromContext(ctx context.Context) (string, bool) {
	if ctx == nil {
		return "", false
	}
	value, ok := ctx.Value(taskProjectContextKey{}).(taskProjectContextValue)
	if !ok {
		return "", false
	}
	return value.project, true
}

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

func parseTaskScopePrefix(args []string) (taskScopeMode, []string) {
	scope, _, _, remaining := parseTaskPrefixes(args)
	return scope, remaining
}

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

func isTaskProjectPrefix(arg string) bool {
	return strings.HasPrefix(arg, "proj:")
}

func trimTaskScopePrefix(args []string) []string {
	return trimTaskPrefixes(args)
}

func trimTaskPrefixes(args []string) []string {
	_, _, _, remaining := parseTaskPrefixes(args)
	return remaining
}

func parseTaskPrefixes(args []string) (taskScopeMode, string, bool, []string) {
	scope := taskScopeAgent
	projectName := ""
	projectSet := false
	for len(args) > 0 {
		switch {
		case isTaskScopePrefix(args[0]):
			scope = taskScopeNoAgent
			args = args[1:]
		case isTaskProjectPrefix(args[0]):
			projectName = args[0][len("proj:"):]
			projectSet = true
			args = args[1:]
		default:
			return scope, projectName, projectSet, args
		}
	}
	return scope, projectName, projectSet, nil
}