forked from cuelang/cue
/
add.go
299 lines (262 loc) · 6.77 KB
/
add.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
// Copyright 2019 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"cuelang.org/go/cue"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/load"
"cuelang.org/go/cue/parser"
"cuelang.org/go/internal"
)
func newAddCmd(c *Command) *cobra.Command {
cmd := &cobra.Command{
// TODO: this command is still experimental, don't show it in
// the documentation just yet.
Hidden: true,
Use: "add <glob> [--list]",
Short: "bulk append to CUE files",
Long: `Append a common snippet of CUE to many files and commit atomically.
`,
RunE: mkRunE(c, runAdd),
}
f := cmd.Flags()
f.Bool(string(flagList), false,
"text executed as Go template with instance info")
f.BoolP(string(flagDryrun), "n", false,
"only run simulation")
f.StringP(string(flagPackage), "p", "", "package to append to")
return cmd
}
func runAdd(cmd *Command, args []string) (err error) {
return doAdd(cmd, args)
}
func doAdd(cmd *Command, args []string) (err error) {
// Offsets at which to restore original files, if any, if any of the
// appends fail.
// Ideally this is placed below where it is used, but we want to make
// absolutely sure that the error variable used in defer is the named
// returned value and not some shadowed value.
originals := []originalFile{}
defer func() {
if err != nil {
restoreOriginals(cmd, originals)
}
}()
// build instance cache
builds := map[string]*build.Instance{}
getBuild := func(path string) *build.Instance {
if b, ok := builds[path]; ok {
return b
}
b := load.Instances([]string{path}, nil)[0]
builds[path] = b
return b
}
// determine file set.
todo := []*fileInfo{}
done := map[string]bool{}
for _, arg := range args {
dir, base := filepath.Split(arg)
dir = filepath.Clean(dir)
matches, err := filepath.Glob(dir)
if err != nil {
return err
}
for _, m := range matches {
if fi, err := os.Stat(m); err != nil || !fi.IsDir() {
continue
}
file := filepath.Join(m, base)
if done[file] {
continue
}
if s := filepath.ToSlash(file); strings.HasPrefix(s, "pkg/") || strings.Contains(s, "/pkg/") {
continue
}
done[file] = true
fi, err := initFile(cmd, file, getBuild)
if err != nil {
return err
}
todo = append(todo, fi)
b := fi.build
if flagList.Bool(cmd) && (b == nil) {
return fmt.Errorf("instance info not available for %s", fi.filename)
}
}
}
// Read text to be appended.
text, err := ioutil.ReadAll(cmd.InOrStdin())
if err != nil {
return err
}
var tmpl *template.Template
if flagList.Bool(cmd) {
tmpl, err = template.New("append").Parse(string(text))
if err != nil {
return err
}
}
for _, fi := range todo {
if tmpl == nil {
fi.contents.Write(text)
continue
}
if err := tmpl.Execute(fi.contents, fi.build); err != nil {
return err
}
}
if flagDryrun.Bool(cmd) {
stdout := cmd.OutOrStdout()
for _, fi := range todo {
fmt.Fprintln(stdout, "---", fi.filename)
_, _ = io.Copy(stdout, fi.contents)
}
return nil
}
// All verified. Execute the todo plan
for _, fi := range todo {
fo, err := fi.appendAndCheck()
if err != nil {
return err
}
originals = append(originals, fo)
}
// Verify resulting builds
for _, fi := range todo {
builds = map[string]*build.Instance{}
b := getBuild(fi.buildArg)
if b.Err != nil {
return b.Err
}
i := cue.Build([]*build.Instance{b})[0]
if i.Err != nil {
return i.Err
}
if err := i.Value().Validate(); err != nil {
return i.Err
}
}
return nil
}
type originalFile struct {
filename string
contents []byte
}
func restoreOriginals(cmd *Command, originals []originalFile) {
for _, fo := range originals {
if err := fo.restore(); err != nil {
fmt.Fprintln(cmd.Stderr(), "Error restoring file: ", err)
}
}
}
func (fo *originalFile) restore() error {
if len(fo.contents) == 0 {
return os.Remove(fo.filename)
}
return ioutil.WriteFile(fo.filename, fo.contents, 0644)
}
type fileInfo struct {
filename string
buildArg string
contents *bytes.Buffer
build *build.Instance
}
func initFile(cmd *Command, file string, getBuild func(path string) *build.Instance) (todo *fileInfo, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("init file: %v", err)
}
}()
dir := filepath.Dir(file)
todo = &fileInfo{file, dir, &bytes.Buffer{}, nil}
if fi, err := os.Stat(file); err != nil {
if !os.IsNotExist(err) {
return nil, err
}
// File does not exist
b := getBuild(dir)
todo.build = b
pkg := flagPackage.String(cmd)
if pkg != "" {
// TODO: do something more intelligent once the package name is
// computed on a module basis, even for empty directories.
b.PkgName = pkg
b.Err = nil
} else {
pkg = b.PkgName
}
if pkg == "" {
return nil, errors.New("must specify package using -p for new files")
}
todo.buildArg = file
fmt.Fprintf(todo.contents, "package %s\n\n", pkg)
} else {
if fi.IsDir() {
return nil, fmt.Errorf("cannot append to directory %s", file)
}
f, err := parser.ParseFile(file, nil)
if err != nil {
return nil, err
}
if _, pkgName, _ := internal.PackageInfo(f); pkgName != "" {
if pkg := flagPackage.String(cmd); pkg != "" && pkgName != pkg {
return nil, fmt.Errorf("package mismatch (%s vs %s) for file %s", pkgName, pkg, file)
}
todo.build = getBuild(dir)
} else {
if pkg := flagPackage.String(cmd); pkg != "" {
return nil, fmt.Errorf("file %s has no package clause but package %s requested", file, pkg)
}
todo.build = getBuild(file)
todo.buildArg = file
}
}
return todo, nil
}
func (fi *fileInfo) appendAndCheck() (fo originalFile, err error) {
// Read original file
b, err := ioutil.ReadFile(fi.filename)
if err == nil {
fo.filename = fi.filename
fo.contents = b
} else if !os.IsNotExist(err) {
return originalFile{}, err
}
if !bytes.HasSuffix(b, []byte("\n")) {
b = append(b, '\n')
}
b = append(b, fi.contents.Bytes()...)
b, err = format.Source(b)
if err != nil {
return originalFile{}, err
}
if err = ioutil.WriteFile(fi.filename, b, 0644); err != nil {
// Just in case, attempt to restore original file.
_ = fo.restore()
return originalFile{}, err
}
return fo, nil
}