/
argtarg.go
73 lines (62 loc) · 1.83 KB
/
argtarg.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
package fab
import (
"context"
"fmt"
"github.com/bobg/errors"
"gopkg.in/yaml.v3"
)
// ArgTarget produces a target with associated arguments
// as a list of strings,
// suitable for parsing with the [flag] package.
// When the target runs,
// its arguments are available from the context using [GetArgs].
//
// It is JSON-encodable
// (and therefore usable as the subtarget in [Files])
// if its subtarget is.
//
// An ArgTarget target may be specified in YAML using the tag !ArgTarget,
// which introduces a sequence.
// The first element of the sequence is a target or target name.
// The remaining elements of the sequence are interpreted byu [YAMLStringListFromNodes]
// to produce the arguments for the target.
func ArgTarget(target Target, args ...string) Target {
return &argTarget{
Target: target,
Args: args,
}
}
type argTarget struct {
Target Target
Args []string
}
var _ Target = &argTarget{}
// Run implements Target.Run.
func (a *argTarget) Run(ctx context.Context, con *Controller) error {
ctx = WithArgs(ctx, a.Args...)
return con.Run(ctx, a.Target)
}
// Desc implements Target.Desc.
func (*argTarget) Desc() string {
return "ArgTarget"
}
func argTargetDecoder(con *Controller, node *yaml.Node, dir string) (Target, error) {
if node.Kind != yaml.SequenceNode {
return nil, BadYAMLNodeKindError{Got: node.Kind, Want: yaml.SequenceNode}
}
if len(node.Content) == 0 {
return nil, fmt.Errorf("no child nodes")
}
target, err := con.YAMLTarget(node.Content[0], dir)
if err != nil {
return nil, errors.Wrap(err, "YAML error in target child of AllTarget node")
}
args, err := con.YAMLStringListFromNodes(node.Content[1:], dir)
if err != nil {
return nil, errors.Wrap(err, "YAML error in ArgTarget args")
}
return ArgTarget(target, args...), nil
}
func init() {
RegisterYAMLTarget("ArgTarget", argTargetDecoder)
}