-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun.go
162 lines (137 loc) · 3.71 KB
/
run.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package flow
import (
go_ctx "context"
"fmt"
"os"
"strings"
"sync"
// "time"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"github.com/hofstadter-io/cuetils/cmd/cuetils/flags"
"github.com/hofstadter-io/cuetils/flow/context"
"github.com/hofstadter-io/cuetils/flow/flow"
_ "github.com/hofstadter-io/cuetils/flow/tasks" // ensure tasks register
"github.com/hofstadter-io/cuetils/structural"
// "github.com/hofstadter-io/cuetils/utils"
)
/*
Input is to rigid
- reads from disk (can we workaround upstream?)
-
*/
func Run(globs []string, opts *flags.RootPflagpole, popts *flags.FlowFlagpole) ([]structural.GlobResult, error) {
return run(globs, opts, popts)
}
// refactor out single/multi
func run(globs []string, opts *flags.RootPflagpole, popts *flags.FlowFlagpole) ([]structural.GlobResult, error) {
ctx := cuecontext.New()
ins, err := structural.ReadGlobs(globs, ctx, nil)
if err != nil {
s := structural.FormatCueError(err)
return nil, fmt.Errorf("Error: %s", s)
}
if len(ins) == 0 {
return nil, fmt.Errorf("no inputs found", '\n')
}
// sharedCtx := buildSharedContext
// (refactor/flow/many) find flows
flows := []*flow.Flow{}
for _, in := range ins {
// (refactor/flow/solo)
val := in.Value
// (temp), give each own context (created in here), or maybe by flag? Need at least the shared mutex
taskCtx, err := buildRootContext(val, opts, popts)
// taskCtx, err := buildRootContext(sharedContex, val, opts, popts)
if err != nil {
return nil, err
}
// this might be buggy?
val, err = injectTags(val, popts.Tags)
if err != nil {
return nil, err
}
// lets just print
if popts.List {
tags, secrets, errs := getTagsAndSecrets(val)
if len(errs) > 0 {
return nil, fmt.Errorf("in getTags: %v", errs)
}
if len(tags) > 0 {
fmt.Println("tags:\n==============")
for _, v := range tags {
printHelpValue(v, "tag")
}
fmt.Println()
}
if len(secrets) > 0 {
fmt.Println("secrets:\n==============")
for _, v := range secrets {
printHelpValue(v, "secret")
}
fmt.Println()
}
fmt.Println("flows:\n==============")
err = listFlows(val, opts, popts)
if err != nil {
return nil, err
}
continue
}
ps, err := findFlows(taskCtx, val, opts, popts)
if err != nil {
return nil, err
}
flows = append(flows, ps...)
}
if popts.List {
return nil, nil
}
if len(flows) == 0 {
return nil, fmt.Errorf("no flows found")
}
// start all of the flows
// TODO, use wait group, accume errors, flag for failure modes
for _, flow := range flows {
err := flow.Start()
if err != nil {
return nil, err
}
}
//time.Sleep(time.Second)
//fmt.Println("done")
return nil, nil
}
func printHelpValue(v cue.Value, attr string) {
path := v.Path()
docs := v.Doc()
if len(docs) > 0 {
for _, d := range docs {
fmt.Println("//", strings.TrimSpace(d.Text()))
}
}
fmt.Printf("%s: %# v %v\n", path, v, v.Attribute(attr))
}
var walkOptions = []cue.Option{
cue.Attributes(true),
cue.Concrete(false),
cue.Definitions(true),
cue.Hidden(true),
cue.Optional(true),
cue.Docs(true),
}
func buildRootContext(val cue.Value, opts *flags.RootPflagpole, popts *flags.FlowFlagpole) (*context.Context, error) {
// lookup the secret label in val
// and build a filter write for stdout / stderr
c := &context.Context{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Context: go_ctx.Background(),
CUELock: new(sync.Mutex),
ValStore: new(sync.Map),
Mailbox: new(sync.Map),
DebugTasks: popts.DebugTasks,
}
return c, nil
}