-
Notifications
You must be signed in to change notification settings - Fork 23
/
files.go
170 lines (136 loc) · 3.43 KB
/
files.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
package dalec
import (
goerrors "errors"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/moby/buildkit/client/llb"
"github.com/pkg/errors"
)
const (
defaultFilePerms = 0o644
defaultDirPerms = 0o755
)
func (d *SourceInlineDir) PopulateAt(p string) llb.StateOption {
return func(st llb.State) llb.State {
perms := d.Permissions.Perm()
if perms == 0 {
perms = defaultDirPerms
}
st = st.File(llb.Mkdir(p, perms, llb.WithUIDGID(int(d.UID), int(d.GID))))
sorted := SortMapKeys(d.Files)
for _, k := range sorted {
f := d.Files[k]
st = st.With(f.PopulateAt(filepath.Join(p, k)))
}
return st
}
}
func (f *SourceInlineFile) PopulateAt(p string) llb.StateOption {
return func(st llb.State) llb.State {
perms := f.Permissions.Perm()
if perms == 0 {
perms = defaultFilePerms
}
return st.File(
llb.Mkfile(p, perms, []byte(f.Contents), llb.WithUIDGID(int(f.UID), int(f.GID))),
)
}
}
func (s *SourceInline) validate(subpath string) (retErr error) {
var errs []error
if s.File == nil && s.Dir == nil {
errs = append(errs, errors.New("inline source is missing contents to inline"))
}
if s.File != nil && s.Dir != nil {
errs = append(errs, errors.New("inline source variant cannot have both a file and dir set"))
}
if s.Dir != nil {
if err := s.Dir.validate(); err != nil {
errs = append(errs, err)
}
}
if s.File != nil {
if subpath != "" {
errs = append(errs, errors.New("inline file source cannot have a path set"))
}
if err := s.File.validate(); err != nil {
errs = append(errs, err)
}
}
return goerrors.Join(errs...)
}
func (s *SourceInlineDir) validate() error {
var errs []error
if s.UID < 0 {
errs = append(errs, errors.Errorf("uid %d must be non-negative", s.UID))
}
if s.GID < 0 {
errs = append(errs, errors.Errorf("gid %d must be non-negative", s.GID))
}
for k, f := range s.Files {
if strings.ContainsRune(k, os.PathSeparator) {
errs = append(errs, errors.Wrapf(sourceNamePathSeparatorError, "file %q", k))
}
if err := f.validate(); err != nil {
errs = append(errs, errors.Wrapf(err, "file %q", k))
}
}
return goerrors.Join(errs...)
}
func (s *SourceInlineFile) validate() error {
var errs []error
if s.UID < 0 {
errs = append(errs, errors.Errorf("uid %d must be non-negative", s.UID))
}
if s.GID < 0 {
errs = append(errs, errors.Errorf("gid %d must be non-negative", s.GID))
}
return goerrors.Join(errs...)
}
func (s *SourceInline) Doc(w io.Writer, name string) {
if s.File != nil {
s.File.Doc(w, name)
}
if s.Dir != nil {
s.Dir.Doc(w, name)
}
}
func (s *SourceInlineFile) Doc(w io.Writer, name string) {
fmt.Fprintln(w, ` cat << EOF > `+name+`
`+s.Contents+`
EOF`)
if s.UID != 0 {
fmt.Fprintln(w, ` chown `+strconv.Itoa(s.UID)+" "+name)
}
if s.GID != 0 {
fmt.Fprintln(w, ` chgrp `+strconv.Itoa(s.GID)+" "+name)
}
perms := s.Permissions.Perm()
if perms == 0 {
perms = 0o644
}
fmt.Fprintf(w, " chmod %o %s\n", perms.Perm(), name)
}
func (s *SourceInlineDir) Doc(w io.Writer, name string) {
fmt.Fprintln(w, ` mkdir -p `+name)
if s.UID != 0 {
fmt.Fprintln(w, ` chown `+strconv.Itoa(s.UID)+" "+name)
}
if s.GID != 0 {
fmt.Fprintln(w, ` chgrp `+strconv.Itoa(s.GID)+" "+name)
}
perms := s.Permissions.Perm()
if perms == 0 {
perms = 0o644
}
sorted := SortMapKeys(s.Files)
for _, k := range sorted {
v := s.Files[k]
v.Doc(w, filepath.Join(name, k))
}
fmt.Fprintf(w, " chmod %o %s\n", perms.Perm(), name)
}