-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_args.go
101 lines (84 loc) · 2.66 KB
/
parse_args.go
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
package main
import (
"context"
"fmt"
"github.com/hephbuild/heph/bootstrap"
"github.com/hephbuild/heph/graph"
"github.com/hephbuild/heph/log/log"
"github.com/hephbuild/heph/scheduler"
"github.com/hephbuild/heph/specs"
"github.com/hephbuild/heph/targetrun"
)
func parseTargetFromArgs(ctx context.Context, args []string) (bootstrap.SchedulerBootstrap, *graph.Target, error) {
if len(args) != 1 {
return bootstrap.SchedulerBootstrap{}, nil, fmt.Errorf("expected one arg")
}
bs, err := schedulerInit(ctx, func(bootstrap.BaseBootstrap) error {
return bootstrap.BlockReadStdin(args)
})
if err != nil {
return bs, nil, err
}
rrs, err := parseTargetsAndArgsWithScheduler(ctx, bs.Scheduler, args, parseTargetsAndArgsOptions{
useStdin: false,
onlyExplicit: true,
})
if err != nil {
return bs, nil, err
}
if len(rrs) != 1 {
log.Debugf("no rrs: %v", args)
s := ""
if len(args) > 0 {
s = args[0]
}
return bs, nil, specs.NewTargetNotFoundError(s, bs.Graph.Targets())
}
return bs, rrs[0].Target, nil
}
func parseTargetsAndArgs(ctx context.Context, args []string, matcherFilter func(m specs.Matcher) error) (bootstrap.SchedulerBootstrap, targetrun.Requests, error) {
bs, err := schedulerInit(ctx, func(bootstrap.BaseBootstrap) error {
return bootstrap.BlockReadStdin(args)
})
if err != nil {
return bootstrap.SchedulerBootstrap{}, nil, err
}
rrs, err := parseTargetsAndArgsWithScheduler(ctx, bs.Scheduler, args, parseTargetsAndArgsOptions{
useStdin: true,
onlyExplicit: false,
matcherFilter: matcherFilter,
})
if err != nil {
return bootstrap.SchedulerBootstrap{}, nil, err
}
return bs, rrs, err
}
type parseTargetsAndArgsOptions struct {
useStdin bool
onlyExplicit bool
matcherFilter func(m specs.Matcher) error
}
func parseTargetsAndArgsWithScheduler(ctx context.Context, e *scheduler.Scheduler, args []string, opts parseTargetsAndArgsOptions) (targetrun.Requests, error) {
m, targs, err := bootstrap.ParseTargetAddrsAndArgs(args, opts.useStdin)
if err != nil {
return nil, err
}
if opts.matcherFilter != nil {
err := opts.matcherFilter(m)
if err != nil {
return nil, err
}
}
if opts.onlyExplicit {
if !specs.IsMatcherExplicit(m) {
return nil, fmt.Errorf("only explicit selector allowed (only exact target)")
}
}
return generateRRs(ctx, e, m, targs)
}
func generateRRs(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher, targs []string) (targetrun.Requests, error) {
return bootstrap.GenerateRRs(ctx, e, m, targs, getRROpts(), *plain, !*noGen)
}
func query(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher) ([]specs.Target, error) {
return bootstrap.Query(ctx, e, m, *plain, !*noGen)
}