-
Notifications
You must be signed in to change notification settings - Fork 3
/
plan.go
51 lines (44 loc) · 1021 Bytes
/
plan.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
package climate
import (
"context"
"runtime"
"strings"
"github.com/avamsi/climate/internal"
)
type funcPlan struct {
reflection
}
func (fp *funcPlan) Execute(ctx context.Context, md *internal.Metadata) error {
var (
name = runtime.FuncForPC(fp.v().Pointer()).Name()
dot = strings.LastIndex(name, ".")
)
pkgPath, name := name[:dot], name[dot+1:]
fcb := &funcCommandBuilder{
name,
fp.reflection,
md.Lookup(pkgPath, name),
}
cmd := fcb.build()
return cmd.run(ctx)
}
type structPlan struct {
reflection
subcommands []*structPlan
}
func (sp *structPlan) buildRecursive(parent *reflection, md *internal.Metadata) *command {
scb := &structCommandBuilder{
sp.reflection,
parent,
md.LookupType(sp.t()),
}
cmd := scb.build()
for _, sub := range sp.subcommands {
cmd.addCommand(sub.buildRecursive(&sp.reflection, md))
}
return cmd
}
func (sp *structPlan) Execute(ctx context.Context, m *internal.Metadata) error {
root := sp.buildRecursive(nil, m) // no parent
return root.run(ctx)
}