This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
spec.go
411 lines (379 loc) · 9.44 KB
/
spec.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (c) 2023 Blockwatch Data Inc.
// Author: alex@blockwatch.cc, abdul@blockwatch.cc
package alpha
import (
"encoding/hex"
"encoding/json"
"fmt"
"hash/fnv"
"net/url"
"os"
"path/filepath"
"strings"
"blockwatch.cc/tzgo/internal/compose"
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzgo/tezos"
"gopkg.in/yaml.v3"
)
type Spec struct {
Version string `yaml:"version"`
Accounts []Account `yaml:"accounts,omitempty"`
Variables map[string]string `yaml:"variables,omitempty"`
Pipelines PipelineList `yaml:"pipelines"`
}
func (s Spec) Validate(ctx compose.Context) error {
if s.Version == "" {
return compose.ErrNoVersion
}
if len(s.Pipelines) == 0 {
return compose.ErrNoPipeline
}
for _, v := range s.Pipelines {
if err := v.Validate(ctx); err != nil {
return fmt.Errorf("pipeline %s: %v", v.Name, err)
}
}
return nil
}
type Account struct {
Name string `yaml:"name"`
Id uint `yaml:"id,omitempty"`
}
type Pipeline struct {
Name string `yaml:"-"`
Tasks []Task `yaml:",inline"`
}
type PipelineList []Pipeline
func (l *PipelineList) UnmarshalYAML(node *yaml.Node) error {
// decode named map into list
*l = make([]Pipeline, len(node.Content)/2)
for i, v := range node.Content {
switch v.Kind {
case yaml.ScalarNode:
(*l)[i/2].Name = v.Value
case yaml.SequenceNode:
if err := v.Decode(&(*l)[i/2].Tasks); err != nil {
return err
}
default:
return fmt.Errorf("unexpected yaml node kind=%s value=%s", v.Tag[2:], v.Value)
}
}
return nil
}
func (l PipelineList) MarshalYAML() (any, error) {
// manualy create a list of named map nodes
node := &yaml.Node{
Kind: yaml.MappingNode,
Tag: "!!map",
Content: []*yaml.Node{},
}
for _, v := range l {
var child yaml.Node
if err := child.Encode(v.Tasks); err != nil {
return nil, err
}
node.Content = append(node.Content,
// key
&yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!str",
Value: v.Name,
},
// value
&child,
)
}
return node, nil
}
func (p Pipeline) Hash64() uint64 {
h := fnv.New64()
enc := json.NewEncoder(h)
enc.Encode(p)
return h.Sum64()
}
func (p Pipeline) Len() int {
return len(p.Tasks)
}
func (p Pipeline) Validate(ctx compose.Context) error {
if p.Name == "" {
return compose.ErrNoPipelineName
}
for i, v := range p.Tasks {
if err := v.Validate(ctx); err != nil {
return fmt.Errorf("task %d (%s): %v", i, v.Type, err)
}
}
return nil
}
type Task struct {
Type string `yaml:"task"`
Alias string `yaml:"alias,omitempty"`
Skip bool `yaml:"skip,omitempty"`
Amount uint64 `yaml:"amount,omitempty"`
Script *Script `yaml:"script,omitempty"` // deploy only
Params *Params `yaml:"params,omitempty"` // call only
Source string `yaml:"source,omitempty"`
Destination string `yaml:"destination,omitempty"`
Args map[string]any `yaml:"args,omitempty"` // token_* only
Contents []Task `yaml:"contents,omitempty"` // batch only
WaitMode WaitMode `yaml:"for,omitempty"` // wait only
Value string `yaml:"value,omitempty"` // wait only
Log string `yaml:"log,omitempty"` // log level override
}
func (t Task) Validate(ctx compose.Context) error {
if t.Type == "" {
return compose.ErrNoTaskType
}
if t.Params != nil {
if err := t.Params.Validate(ctx); err != nil {
return fmt.Errorf("params: %v", err)
}
}
if _, ok := ctx.Variables[t.Alias]; !ok && t.Alias != "" {
ctx.AddVariable(t.Alias, tezos.ZeroAddress.String())
}
if t.Script != nil {
if err := t.Script.Validate(ctx); err != nil {
return fmt.Errorf("script: %v", err)
}
}
if t.Type == "batch" {
if len(t.Contents) == 0 {
return fmt.Errorf("empty contents")
}
for _, v := range t.Contents {
if v.Type == "batch" {
return fmt.Errorf("nested batch tasks not allowed")
}
if v.Contents != nil {
return fmt.Errorf("nested contents not allowed")
}
if v.Source != "" && v.Source != t.Source {
return fmt.Errorf("switching source is not allowed in batch contents")
}
if err := v.Validate(ctx); err != nil {
return fmt.Errorf("contents: %v", err)
}
}
} else if t.Contents != nil {
return fmt.Errorf("contents only allowed in batch tasks")
}
return nil
}
type WaitMode byte
const (
WaitModeInvalid WaitMode = iota
WaitModeCycle
WaitModeBlock
WaitModeTime
)
func (m *WaitMode) UnmarshalYAML(node *yaml.Node) error {
return m.UnmarshalText([]byte(node.Value))
}
func (m *WaitMode) UnmarshalText(buf []byte) error {
switch string(buf) {
case "cycle":
*m = WaitModeCycle
case "block":
*m = WaitModeBlock
case "time":
*m = WaitModeTime
case "":
*m = WaitModeInvalid
default:
return fmt.Errorf("invalid wait mode")
}
return nil
}
type ValueSource struct {
File string `yaml:"file,omitempty"`
Url string `yaml:"url,omitempty"`
Value string `yaml:"value,omitempty"`
}
func (v ValueSource) IsUsed() bool {
return len(v.Url)+len(v.File)+len(v.Value) > 0
}
func (v ValueSource) Validate(ctx compose.Context) error {
if !v.IsUsed() {
return fmt.Errorf("required file, url or value")
}
switch {
case v.Url != "":
if u, err := url.Parse(v.Url); err != nil {
return fmt.Errorf("url: %v", err)
} else if u.Host == "" {
return fmt.Errorf("missing url host")
}
case v.File != "":
fname, _, _ := strings.Cut(v.File, "#")
fname = filepath.Join(ctx.Filepath(), fname)
if _, err := os.Stat(fname); err != nil {
return fmt.Errorf("file: %v", err)
}
case v.Value != "":
if !isHex(v.Value) {
var x any
if err := json.Unmarshal([]byte(v.Value), &x); err != nil {
return fmt.Errorf("json value: %v", err)
}
} else {
buf, err := hex.DecodeString(v.Value)
if err != nil {
return fmt.Errorf("bin value: %v", err)
}
var prim micheline.Prim
if err := prim.UnmarshalBinary(buf); err != nil {
return fmt.Errorf("bin value: %v", err)
}
}
}
return nil
}
type Script struct {
ValueSource `yaml:",inline"`
Code *Code `yaml:"code,omitempty"`
Storage *Storage `yaml:"storage,omitempty"`
}
type Code struct {
ValueSource `yaml:",inline"`
// Patch []Patch `yaml:"patch,omitempty"`
}
type Storage struct {
ValueSource `yaml:",inline"`
Args any `yaml:"args,omitempty"`
Patch []Patch `yaml:"patch,omitempty"`
}
func (s Script) Validate(ctx compose.Context) error {
// either script-level source or both storage+code sources are required
if s.ValueSource.IsUsed() {
if err := s.ValueSource.Validate(ctx); err != nil {
return err
}
} else {
if s.Code == nil {
return fmt.Errorf("missing code section")
}
if err := s.Code.ValueSource.Validate(ctx); err != nil {
return err
}
if s.Storage == nil {
return fmt.Errorf("missing storage section")
}
if s.Storage.Args == nil {
// without args, storage must come from a valid source
if err := s.Storage.ValueSource.Validate(ctx); err != nil {
return fmt.Errorf("storage: %v", err)
}
}
}
// if s.Code != nil {
// for _, v := range s.Code.Patch {
// if err := v.Validate(ctx); err != nil {
// return err
// }
// }
// }
if s.Storage != nil {
for _, v := range s.Storage.Patch {
if err := v.Validate(ctx); err != nil {
return err
}
}
if err := checkArgs(ctx, s.Storage.Args); err != nil {
return err
}
}
return nil
}
func checkArgs(ctx compose.Context, args any) error {
if args == nil {
return nil
}
switch v := args.(type) {
case map[string]any:
for n, v := range v {
if err := checkArgs(ctx, v); err != nil {
return fmt.Errorf("arg %s: %v", n, err)
}
}
case []any:
for i, v := range v {
if err := checkArgs(ctx, v); err != nil {
return fmt.Errorf("arg %d: %v", i, err)
}
}
case string:
if _, err := ctx.ResolveString(v); err != nil {
return err
}
case int:
// skip
case bool:
// skip
default:
return fmt.Errorf("unchecked arg type %T", args)
}
return nil
}
type Params struct {
ValueSource `yaml:",inline"`
Entrypoint string `yaml:"entrypoint"`
Args any `yaml:"args,omitempty"`
Patch []Patch `yaml:"patch,omitempty"`
}
func (p Params) Validate(ctx compose.Context) error {
if p.Args == nil {
if err := p.ValueSource.Validate(ctx); err != nil && p.Args == nil {
return err
}
} else {
if err := checkArgs(ctx, p.Args); err != nil {
return err
}
}
if p.Entrypoint == "" {
return fmt.Errorf("missing entrypoint")
}
for _, v := range p.Patch {
if err := v.Validate(ctx); err != nil {
return err
}
}
return nil
}
type Patch struct {
Type string `yaml:"type"`
Key *string `yaml:"key,omitempty"`
Path *string `yaml:"path,omitempty"`
Value *string `yaml:"value"`
Optimized bool `yaml:"optimized"`
}
func (p Patch) Validate(ctx compose.Context) error {
// accept empty string
if p.Key == nil && p.Path == nil {
return fmt.Errorf("patch: required key or path")
}
// accept empty string
if p.Value == nil {
return fmt.Errorf("patch: required value")
}
// type must be correct
oc, err := micheline.ParseOpCode(p.Type)
if err != nil {
return fmt.Errorf("patch: %v", err)
}
if !oc.IsTypeCode() {
return fmt.Errorf("patch: %s is not a valid type code", p.Type)
}
// value must resolve
val, err := ctx.ResolveString(*p.Value)
if err != nil {
return fmt.Errorf("patch: %v", err)
}
// value must parse against type code
if _, err := compose.ParseValue(oc, val); err != nil {
return fmt.Errorf("patch: %v", err)
}
return nil
}