forked from jessfraz/dockfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.go
410 lines (351 loc) · 9.15 KB
/
format.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
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/moby/buildkit/frontend/dockerfile/parser"
)
const formatHelp = `Format the Dockerfile(s).`
func (cmd *formatCommand) Name() string { return "fmt" }
func (cmd *formatCommand) Args() string { return "[OPTIONS] DOCKERFILE [DOCKERFILE...]" }
func (cmd *formatCommand) ShortHelp() string { return formatHelp }
func (cmd *formatCommand) LongHelp() string { return formatHelp }
func (cmd *formatCommand) Hidden() bool { return false }
func (cmd *formatCommand) Register(fs *flag.FlagSet) {
fs.BoolVar(&cmd.diff, "diff", false, "display diffs instead of rewriting files")
fs.BoolVar(&cmd.diff, "D", false, "display diffs instead of rewriting files")
fs.BoolVar(&cmd.list, "list", false, "list files whose formatting differs from dockfmt's")
fs.BoolVar(&cmd.list, "l", false, "list files whose formatting differs from dockfmt's")
fs.BoolVar(&cmd.write, "write", false, "write result to (source) file instead of stdout")
fs.BoolVar(&cmd.write, "w", false, "write result to (source) file instead of stdout")
}
type formatCommand struct {
diff bool
list bool
write bool
}
type file struct {
currentLine int
name string
originalFile []byte
}
func (cmd *formatCommand) Run(ctx context.Context, args []string) error {
err := forFile(args, func(f string, nodes []*parser.Node) error {
og, err := ioutil.ReadFile(f)
if err != nil {
return err
}
df := file{
currentLine: 1,
name: f,
originalFile: og,
}
var result string
for _, n := range nodes {
r, err := df.doFmt(n)
if err != nil {
return err
}
result += r
}
// display the diff if requested
if cmd.diff {
d, err := diff(og, []byte(result))
if err != nil {
return fmt.Errorf("computing diff: %s", err)
}
fmt.Printf("diff %s dockfmt/%s\n", f, f)
os.Stdout.Write(d)
}
if cmd.list {
if !bytes.Equal(og, []byte(result)) {
fmt.Fprintln(os.Stdout, f)
}
}
// write to the file
if cmd.write {
// make a temporary backup before overwriting original
bakname, err := backupFile(f+".", og, 0644)
if err != nil {
return err
}
if err := ioutil.WriteFile(f, []byte(result), 0644); err != nil {
os.Rename(bakname, f)
return err
}
if err := os.Remove(bakname); err != nil {
return fmt.Errorf("could not remove backup file %s: %v", bakname, err)
}
}
if !cmd.diff && !cmd.list && !cmd.write {
os.Stdout.Write([]byte(result))
}
return nil
})
return err
}
func (df *file) doFmt(ast *parser.Node) (result string, err error) {
// Get comments between currentLine (last astStartLine) and ast.StartLine
r, err := df.getCommentsUntil(ast.StartLine)
if err != nil {
return "", err
}
result += r
// set the variables for the directive (k) and the value (v)
k := ast.Value
var v string
if ast.Next != nil {
v = ast.Next.Value
}
// capitalize the directive
k = strings.ToUpper(k)
// format per directive
switch k {
case "ADD":
v = fmtCopy(ast.Next)
case "CMD":
v, err = fmtCmd(ast.Next)
if err != nil {
return "", err
}
case "COPY":
v = fmtCopy(ast.Next)
case "ENTRYPOINT":
v, err = fmtCmd(ast.Next)
if err != nil {
return "", err
}
case "RUN":
result += "\n"
v = fmtRun(v)
case "LABEL":
v = fmtLabel(ast.Next)
default:
v = fmtCopy(ast.Next)
}
// print line to the result
result += fmt.Sprintf("%s\t%s\n", k, v)
// move forward
if ast.Next != nil {
df.currentLine = ast.StartLine
}
return
}
func (df *file) getCommentsUntil(until int) (result string, err error) {
re := regexp.MustCompile(`^#.*(\r)?$`)
once := false
comments, err := df.getOriginalLines(df.currentLine, until, df.name)
if err != nil {
return "", err
}
for _, line := range strings.Split(comments, "\n") {
if re.MatchString(line) {
if !once {
result += "\n"
once = false
}
result += line
}
}
return result, nil
}
func (df *file) getOriginalLines(s int, e int, fn string) (string, error) {
scanner := bufio.NewScanner(bytes.NewBuffer(df.originalFile))
scanner.Split(bufio.ScanLines)
var (
i = 1
l string
)
for scanner.Scan() {
if i >= s && i < e {
l += scanner.Text() + "\n"
}
i++
}
return l, nil
}
func getCmd(n *parser.Node, cmd []string) []string {
if n == nil {
return cmd
}
cmd = append(cmd, n.Value)
if len(n.Flags) > 0 {
cmd = append(cmd, n.Flags...)
}
if n.Next != nil {
for node := n.Next; node != nil; node = node.Next {
cmd = append(cmd, node.Value)
if len(node.Flags) > 0 {
cmd = append(cmd, node.Flags...)
}
}
}
return cmd
}
func fmtCmd(node *parser.Node) (string, error) {
cmd := []string{}
cmd = getCmd(node, cmd)
b, err := json.Marshal(cmd)
if err != nil {
return "", err
}
return string(b), nil
}
func fmtCopy(node *parser.Node) string {
cmd := []string{}
cmd = getCmd(node, cmd)
return strings.Join(cmd, "\t")
}
func fmtLabel(node *parser.Node) string {
cmd := []string{}
cmd = getCmd(node, cmd)
return strings.Join(cmd, "=")
}
func fmtRun(s string) string {
// this regex matches single and double quoted strings & ignores escaped chars
/*
(?:
[^\\] // must not begin with escape char "\"
(\\.)* // ignore any escaped chars before the first quote
)(
'(?:\\.|[^\\'])*' // find string(s) that start & end with single quotes & ignore escaped chars
| // or
"(?:\\.|[^\\"])*" // find string(s) that start & end with double quotes & ignore escaped chars
)
*/
regexQuotes, _ := regexp.Compile(`(?:[^\\]((\\.)*))('(?:\\.|[^\\'])*'|"(?:\\.|[^\\"])*")`)
// escape any &'s between quotes
// this should be done first before handling the &&'s outside of quotes
s = regexQuotes.ReplaceAllStringFunc(s, func(q string) string {
// the regex grabs one char before the first quote to ensure the quote isn't escaped
// ignore this first char in case it's an '&'
escaped := strings.Replace(q[1:], "&", "\\&", -1)
return string(q[0]) + escaped
})
s = strings.Replace(s, "apk update && apk add", "apk add --no-cache", -1)
var r string
cmds := strings.Split(s, "&&")
cmds = trimAll(cmds)
for i, c := range cmds {
c = strings.Replace(c, "apk --no-cache add", "apk add", -1)
// handle `apk add` commands
if strings.HasPrefix(c, "apk add") {
c = strings.TrimPrefix(c, "apk add")
// format --no-cache
// we will add it back later
c = strings.Replace(c, "--no-cache", "", -1)
c = strings.Replace(c, "apk add", "", -1)
// recreate the command
c = "apk add --no-cache \\" + "\n" + splitLinesWord(c)
}
// handle `apt-get install` commands
if strings.HasPrefix(c, "apt-get install") {
c = strings.TrimPrefix(c, "apt-get install")
// format -y
// we will add it back later
c = strings.Replace(c, "-y", "", -1)
c = strings.Replace(c, "apt-get install", "", -1)
// recreate the command
c = "apt-get install -y \\" + "\n" + splitLinesWord(c)
}
// return any &'s between quotes back to how they were
c = regexQuotes.ReplaceAllStringFunc(c, func(q string) string {
escaped := strings.Replace(q, "\\&", "&", -1)
return escaped
})
// we aren't on the first line add back the `&&`
if i != 0 {
c = "\t&& " + c
}
// if we aren't on the last line add a `\\n`
if i != len(cmds)-1 {
c += " \\\n"
}
r += c
}
// put `apt-get update && apt-get install` on one-line it's prettier
r = strings.Replace(r, "apt-get update \\\n\t&& apt-get install", "apt-get update && apt-get install", -1)
return r
}
func trimAll(a []string) []string {
for i, v := range a {
a[i] = strings.TrimSpace(v)
}
return a
}
func splitLinesWord(s string) string {
a := strings.Fields(s)
a = trimAll(a)
var r string
for i, v := range a {
r += "\t" + v
// if we aren't on the last line add a `\\n`
if i != len(a)-1 {
r += " \\\n"
}
}
return r
}
func diff(b1, b2 []byte) (data []byte, err error) {
f1, err := ioutil.TempFile("", "dockfmt")
if err != nil {
return
}
defer os.Remove(f1.Name())
defer f1.Close()
f2, err := ioutil.TempFile("", "dockfmt")
if err != nil {
return
}
defer os.Remove(f2.Name())
defer f2.Close()
f1.Write(b1)
f2.Write(b2)
data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
if len(data) > 0 {
// diff exits with a non-zero status when the files don't match.
// Ignore that failure as long as we get output.
err = nil
}
return
}
const chmodSupported = runtime.GOOS != "windows"
// backupFile writes data to a new file named filename<number> with permissions perm,
// with <number randomly chosen such that the file name is unique. backupFile returns
// the chosen file name.
func backupFile(filename string, data []byte, perm os.FileMode) (string, error) {
// create backup file
f, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename))
if err != nil {
return "", err
}
bakname := f.Name()
if chmodSupported {
err = f.Chmod(perm)
if err != nil {
f.Close()
os.Remove(bakname)
return bakname, err
}
}
// write data to backup file
n, err := f.Write(data)
if err == nil && n < len(data) {
err = io.ErrShortWrite
}
if err1 := f.Close(); err == nil {
err = err1
}
return bakname, err
}