Skip to content

Commit b2df033

Browse files
committed
Resolve #18
1 parent a136775 commit b2df033

File tree

8 files changed

+37
-11
lines changed

8 files changed

+37
-11
lines changed

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ var versionCmd = &cobra.Command{
1414
Use: "version",
1515
Short: "Print the version number of dockerfmt",
1616
Run: func(cmd *cobra.Command, args []string) {
17-
fmt.Println("dockerfmt 0.2.7")
17+
fmt.Println("dockerfmt 0.2.8")
1818
},
1919
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/reteps/dockerfmt
33
go 1.24.1
44

55
require (
6+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
67
github.com/moby/buildkit v0.20.2
78
github.com/spf13/cobra v1.9.1
89
mvdan.cc/sh/v3 v3.11.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
99
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
1010
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
1111
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
12+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
13+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
1214
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
1315
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
1416
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=

js/format.wasm

11.5 KB
Binary file not shown.

js/package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reteps/dockerfmt",
3-
"version": "0.2.7",
3+
"version": "0.2.8",
44
"type": "module",
55
"description": "",
66
"repository": "git+https://github.com/reteps/dockerfmt/tree/main/js",

lib/format.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"regexp"
1111
"strings"
1212

13+
"github.com/google/shlex"
1314
"github.com/moby/buildkit/frontend/dockerfile/command"
1415
"github.com/moby/buildkit/frontend/dockerfile/parser"
1516
"mvdan.cc/sh/v3/syntax"
@@ -300,7 +301,7 @@ func formatRun(n *ExtendedNode, c *Config) string {
300301
var jsonItems []string
301302
err := json.Unmarshal([]byte(content), &jsonItems)
302303
if err == nil {
303-
out, err := json.Marshal(jsonItems)
304+
out, err := Marshal(jsonItems)
304305
if err != nil {
305306
panic(err)
306307
}
@@ -328,25 +329,42 @@ func formatBasic(n *ExtendedNode, c *Config) string {
328329
return IndentFollowingLines(strings.ToUpper(n.Value)+" "+parts[1], c.IndentSize)
329330
}
330331

332+
// Marshal is a UTF-8 friendly marshaler. Go's json.Marshal is not UTF-8
333+
// friendly because it replaces the valid UTF-8 and JSON characters "&". "<",
334+
// ">" with the "slash u" unicode escaped forms (e.g. \u0026). It preemptively
335+
// escapes for HTML friendliness. Where text may include any of these
336+
// characters, json.Marshal should not be used. Playground of Go breaking a
337+
// title: https://play.golang.org/p/o2hiX0c62oN
338+
// Source: https://stackoverflow.com/a/69502657/5684541
339+
func Marshal(i interface{}) ([]byte, error) {
340+
buffer := &bytes.Buffer{}
341+
encoder := json.NewEncoder(buffer)
342+
encoder.SetEscapeHTML(false)
343+
err := encoder.Encode(i)
344+
return bytes.TrimRight(buffer.Bytes(), "\n"), err
345+
}
346+
331347
func getCmd(n *ExtendedNode) []string {
332348
cmd := []string{}
333349
for node := n; node != nil; node = node.Next {
334350
// Split value by whitespace
335-
parts := regexp.MustCompile("[ \t]+").Split(strings.Trim(node.Value, " \t"), -1)
336-
// Remove empty parts
337-
338-
cmd = append(cmd, parts...)
351+
rawValue := strings.Trim(node.Value, " \t")
339352
if len(node.Flags) > 0 {
340-
cmd = append(cmd, node.Flags...)
353+
rawValue += " " + strings.Join(node.Flags, " ")
341354
}
355+
parts, err := shlex.Split(rawValue)
356+
if err != nil {
357+
log.Fatalf("Error splitting: %s\n", node.Value)
358+
}
359+
cmd = append(cmd, parts...)
342360
}
343361
// log.Printf("getCmd: %v\n", cmd)
344362
return cmd
345363
}
346364

347365
func formatCmd(n *ExtendedNode, c *Config) string {
348366
cmd := getCmd(n.Next)
349-
b, err := json.Marshal(cmd)
367+
b, err := Marshal(cmd)
350368
if err != nil {
351369
return ""
352370
}

tests/quoting.dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM hackernews
2+
# https://news.ycombinator.com/item?id=43630653
3+
ENTRYPOINT service ssh restart && bash
4+
5+
ENTRYPOINT sh -c 'service ssh restart && bash'

0 commit comments

Comments
 (0)