Skip to content

Commit 6a00dd4

Browse files
committed
Split command with '&&' correctly
1 parent f4c9505 commit 6a00dd4

File tree

6 files changed

+43
-32
lines changed

6 files changed

+43
-32
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.3.5")
17+
fmt.Println("dockerfmt 0.3.6")
1818
},
1919
}

js/format.wasm

865 KB
Binary file not shown.

js/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reteps/dockerfmt",
3-
"version": "0.3.5",
3+
"version": "0.3.6",
44
"type": "module",
55
"description": "",
66
"repository": "git+https://github.com/reteps/dockerfmt/tree/main/js",
@@ -31,7 +31,7 @@
3131
"scripts": {
3232
"//": "Requires tinygo 0.38.0 or later",
3333
"build": "npm run build-go && npm run build-js",
34-
"build-go": "tinygo build -o format.wasm -target wasm --no-debug -scheduler=none",
34+
"build-go": "tinygo build -o format.wasm -target wasm --no-debug",
3535
"build-js": "tsc && cp format.wasm wasm_exec.js dist",
3636
"format": "prettier --write \"**/*.{js,ts,json}\""
3737
},

lib/format.go

+26-29
Original file line numberDiff line numberDiff line change
@@ -458,39 +458,26 @@ func getCmd(n *ExtendedNode, shouldSplitNode bool) []string {
458458
return cmd
459459
}
460460

461-
func formatEntrypoint(n *ExtendedNode, c *Config) string {
462-
// this can technically change behavior. https://docs.docker.com/reference/dockerfile/#understand-how-cmd-and-entrypoint-interact
463-
isJSON, ok := n.Node.Attributes["json"]
464-
if !ok {
465-
isJSON = false
461+
func shouldRunInShell(node string) bool {
462+
// https://docs.docker.com/reference/dockerfile/#entrypoint
463+
parts, err := shlex.Split(node)
464+
if err != nil {
465+
log.Fatalf("Error splitting: %s\n", node)
466466
}
467-
if !isJSON {
468-
// https://docs.docker.com/reference/dockerfile/#entrypoint
469-
node := n.Next.Node.Value
470-
parts, err := shlex.Split(node)
471-
if err != nil {
472-
log.Fatalf("Error splitting: %s\n", node)
473-
}
474467

475-
doNotSplit := false
476-
// This is a simplistic check to determine if we need to run in a full shell.
477-
for _, part := range parts {
478-
if part == "&&" || part == ";" || part == "||" {
479-
doNotSplit = true
480-
break
481-
}
482-
}
483-
484-
if doNotSplit {
485-
n.Next.Node.Flags = append(n.Next.Node.Flags, []string{"/bin/sh", "-c"}...)
486-
// Hacky workaround to tell getCmd to not split the command
487-
if n.Node.Attributes == nil {
488-
n.Node.Attributes = make(map[string]bool)
489-
}
490-
n.Node.Attributes["json"] = true
468+
needsShell := false
469+
// This is a simplistic check to determine if we need to run in a full shell.
470+
for _, part := range parts {
471+
if part == "&&" || part == ";" || part == "||" {
472+
needsShell = true
473+
break
491474
}
492475
}
493-
// printAST(n, 0)
476+
477+
return needsShell
478+
}
479+
func formatEntrypoint(n *ExtendedNode, c *Config) string {
480+
// this can technically change behavior. https://docs.docker.com/reference/dockerfile/#understand-how-cmd-and-entrypoint-interact
494481
return formatCmd(n, c)
495482
}
496483
func formatCmd(n *ExtendedNode, c *Config) string {
@@ -499,6 +486,16 @@ func formatCmd(n *ExtendedNode, c *Config) string {
499486
if !ok {
500487
isJSON = false
501488
}
489+
490+
if !isJSON {
491+
doNotSplit := shouldRunInShell(n.Node.Next.Value)
492+
if doNotSplit {
493+
n.Next.Node.Flags = append(n.Next.Node.Flags, []string{"/bin/sh", "-c"}...)
494+
// Hacky workaround to tell getCmd to not split the command
495+
isJSON = true
496+
}
497+
}
498+
502499
cmd := getCmd(n.Next, !isJSON)
503500
b, err := Marshal(cmd)
504501
if err != nil {

tests/in/crash.dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM nginx
2+
WORKDIR /app
3+
ARG PROJECT_DIR=/
4+
ARG NGINX_CONF=nginx.conf
5+
COPY $NGINX_CONF /etc/nginx/conf.d/nginx.conf
6+
COPY $PROJECT_DIR /app
7+
CMD mkdir --parents /var/log/nginx && nginx -g "daemon off;"

tests/out/crash.dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM nginx
2+
WORKDIR /app
3+
ARG PROJECT_DIR=/
4+
ARG NGINX_CONF=nginx.conf
5+
COPY $NGINX_CONF /etc/nginx/conf.d/nginx.conf
6+
COPY $PROJECT_DIR /app
7+
CMD ["/bin/sh", "-c", "mkdir --parents /var/log/nginx && nginx -g \"daemon off;\""]

0 commit comments

Comments
 (0)