-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
writefile.go
110 lines (89 loc) · 1.91 KB
/
writefile.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
package os
import (
"fmt"
"os"
"cuelang.org/go/cue"
hofcontext "github.com/hofstadter-io/hof/flow/context"
)
type WriteFile struct{}
func NewWriteFile(val cue.Value) (hofcontext.Runner, error) {
return &WriteFile{}, nil
}
func (T *WriteFile) Run(ctx *hofcontext.Context) (interface{}, error) {
v := ctx.Value
var fn string // filename
var bs []byte // contents
var m int64 // mode
var a bool // append
ferr := func() error {
ctx.CUELock.Lock()
defer func() {
ctx.CUELock.Unlock()
}()
var err error
f := v.LookupPath(cue.ParsePath("filename"))
if !f.Exists() {
return fmt.Errorf("missing required field 'filename'")
}
fn, err = f.String()
if err != nil {
return err
}
// switch on c's type to fill appropriately
c := v.LookupPath(cue.ParsePath("contents"))
if !c.Exists() {
return fmt.Errorf("missing required field 'contents'")
}
switch k := c.IncompleteKind(); k {
case cue.StringKind:
s, err := c.Bytes()
if err != nil {
return err
}
bs = []byte(s)
case cue.BytesKind:
bs, err = c.Bytes()
if err != nil {
return err
}
default:
return fmt.Errorf("Unsupported content type in WriteFile task: %q", k)
}
mode := v.LookupPath(cue.ParsePath("mode"))
if !mode.Exists() {
return fmt.Errorf("missing required field 'mode'")
}
m, err = mode.Int64()
if err != nil {
return err
}
av := v.LookupPath(cue.ParsePath("append"))
if av.Exists() {
a, err = av.Bool()
if err != nil {
return err
}
}
return nil
}()
if ferr != nil {
return nil, ferr
}
// todo, check failure modes, fill, not return error?
// (in all tasks)
om := os.O_CREATE | os.O_WRONLY
if a {
om = om | os.O_APPEND
} else {
om = om | os.O_TRUNC
}
f, err := os.OpenFile(fn, om, os.FileMode(m))
if err != nil {
return nil, err
}
defer f.Close()
if _, err := f.Write(bs); err != nil {
return nil, err
}
return nil, err
}