forked from xxf098/actionflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.go
152 lines (131 loc) · 3.12 KB
/
do.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
package actionflow
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load"
"github.com/xxf098/actionflow/plan"
"github.com/xxf098/actionflow/plan/task"
)
func doPlan[T PlanLoader](ctx context.Context, p string, action string) error {
targetPath := getTargetPath([]string{action})
var t T
daggerPlan, err := t.load(ctx, p)
if err != nil {
return err
}
err = daggerPlan.Do(ctx, targetPath)
if err != nil {
return err
}
return nil
}
// load files in dir path
func Do(ctx context.Context, p string, action string) error {
// targetPath := getTargetPath([]string{action})
// daggerPlan, err := loadPlan(ctx, p)
// if err != nil {
// return err
// }
// err = daggerPlan.Do(ctx, targetPath)
// if err != nil {
// return err
// }
// return nil
return doPlan[DirPlanLoader](ctx, p, action)
}
// load one file
func doFlowTest(cueFile string, action string) error {
return doPlan[FilePlanLoader](context.Background(), cueFile, action)
}
type PlanLoader interface {
load(ctx context.Context, p string) (*plan.Plan, error)
}
type DirPlanLoader struct {
}
func (l DirPlanLoader) load(ctx context.Context, planPath string) (*plan.Plan, error) {
absPlanPath, err := filepath.Abs(planPath)
if err != nil {
return nil, err
}
_, err = os.Stat(absPlanPath)
if err != nil {
return nil, err
}
os.Chdir(absPlanPath)
return plan.Load(ctx, plan.Config{
Args: []string{absPlanPath},
})
}
type FilePlanLoader struct {
}
func (l FilePlanLoader) load(ctx context.Context, cueFile string) (*plan.Plan, error) {
v := plan.LoadFile(cueFile)
if v.Err() != nil {
return nil, v.Err()
}
iter, _ := v.Fields()
for iter.Next() {
fmt.Println(iter.Label())
}
return plan.NewPlan(v), nil
}
func getTargetPath(args []string) cue.Path {
selectors := []cue.Selector{plan.ActionSelector}
for _, arg := range args {
selectors = append(selectors, cue.Str(arg))
}
return cue.MakePath(selectors...)
}
// run a action in cue
// TODO: run action with dependency
func doTest(filePath string, actionName string) (*cue.Value, error) {
ctx := cuecontext.New()
entrypoints := []string{filePath}
bis := load.Instances(entrypoints, nil)
var output *cue.Value
var err error
selectors := []cue.Selector{cue.Str("actions")}
for _, v := range strings.Split(actionName, ".") {
selectors = append(selectors, cue.Str(v))
}
for _, bi := range bis {
if bi.Err != nil {
fmt.Println("Error during load:", bi.Err)
err = bi.Err
continue
}
value := ctx.BuildInstance(bi)
if value.Err() != nil {
fmt.Println("Error during build:", value.Err())
err = value.Err()
continue
}
p := cue.MakePath(selectors...)
a := value.LookupPath(p)
if !a.Exists() {
err = fmt.Errorf("path not found")
continue
}
taskType, actionValue := task.LookupAction(&a)
if len(taskType) < 1 {
err = fmt.Errorf("task not found")
continue
}
fmt.Println(taskType)
t := task.New(taskType)
if t == nil {
continue
}
var err error
output, err = t.Run(context.Background(), actionValue)
if err != nil {
fmt.Println(err)
}
}
return output, err
}