Skip to content

Commit 9558942

Browse files
authored
fix: fix unicode escaping install commandss (#10)
1 parent cf9bf72 commit 9558942

File tree

6 files changed

+14
-56
lines changed

6 files changed

+14
-56
lines changed

runtime/php.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,12 @@ func (d *PHP) GenerateDockerfile(path string) ([]byte, error) {
123123
See https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile`, *version, installCMD, buildCMD, startCMD),
124124
)
125125

126-
if installCMD != "" {
127-
installCMDJSON, _ := json.Marshal(installCMD)
128-
installCMD = string(installCMDJSON)
129-
}
130-
131-
if buildCMD != "" {
132-
buildCMDJSON, _ := json.Marshal(buildCMD)
133-
buildCMD = string(buildCMDJSON)
134-
}
135-
136-
if startCMD != "" {
137-
startCMDJSON, _ := json.Marshal(startCMD)
138-
startCMD = string(startCMDJSON)
139-
}
140-
141126
var buf bytes.Buffer
142127
if err := tmpl.Option("missingkey=zero").Execute(&buf, map[string]string{
143128
"Version": *version,
144-
"InstallCMD": installCMD,
145-
"BuildCMD": buildCMD,
146-
"StartCMD": startCMD,
129+
"InstallCMD": safeCommand(installCMD),
130+
"BuildCMD": safeCommand(buildCMD),
131+
"StartCMD": safeCommand(startCMD),
147132
}); err != nil {
148133
return nil, fmt.Errorf("Failed to execute template")
149134
}
@@ -160,7 +145,7 @@ COPY . .
160145
161146
ARG INSTALL_CMD={{.InstallCMD}}
162147
ARG BUILD_CMD={{.BuildCMD}}
163-
RUN if [ ! -z "${INSTALL_CMD}" ]; then $INSTALL_CMD; fi
148+
RUN if [ ! -z "${INSTALL_CMD}" ]; then echo "${INSTALL_CMD}" > dep.sh; sh dep.sh; fi
164149
RUN if [ ! -z "${BUILD_CMD}" ]; then $BUILD_CMD; fi
165150
166151
FROM php:${VERSION}-apache AS runtime

runtime/php_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestPHPGenerateDockerfile(t *testing.T) {
6060
{
6161
name: "PHP project with composer",
6262
path: "../testdata/php-composer",
63-
expected: []any{`ARG VERSION=5.3`, `ARG INSTALL_CMD="composer update \u0026\u0026 composer install --prefer-dist --no-dev --optimize-autoloader --no-interaction"`, regexp.MustCompile(`^ARG BUILD_CMD=$`), `ARG START_CMD="apache2-foreground`},
63+
expected: []any{`ARG VERSION=5.3`, `ARG INSTALL_CMD="composer update && composer install --prefer-dist --no-dev --optimize-autoloader --no-interaction"`, regexp.MustCompile(`^ARG BUILD_CMD=$`), `ARG START_CMD="apache2-foreground`},
6464
},
6565
{
6666
name: "PHP project with NPM",

runtime/python.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package runtime
33
import (
44
"bufio"
55
"bytes"
6-
"encoding/json"
76
"fmt"
87
"log/slog"
98
"os"
@@ -146,21 +145,11 @@ func (d *Python) GenerateDockerfile(path string) ([]byte, error) {
146145
See https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile`, *version, installCMD, startCMD),
147146
)
148147

149-
if installCMD != "" {
150-
installCMDJSON, _ := json.Marshal(installCMD)
151-
installCMD = string(installCMDJSON)
152-
}
153-
154-
if startCMD != "" {
155-
startCMDJSON, _ := json.Marshal(startCMD)
156-
startCMD = string(startCMDJSON)
157-
}
158-
159148
var buf bytes.Buffer
160149
if err := tmpl.Option("missingkey=zero").Execute(&buf, map[string]string{
161150
"Version": *version,
162-
"InstallCMD": installCMD,
163-
"StartCMD": startCMD,
151+
"InstallCMD": safeCommand(installCMD),
152+
"StartCMD": safeCommand(startCMD),
164153
}); err != nil {
165154
return nil, fmt.Errorf("Failed to execute template")
166155
}
@@ -178,7 +167,7 @@ RUN chown -R nonroot:nonroot /app
178167
179168
COPY --chown=nonroot:nonroot . .
180169
ARG INSTALL_CMD={{.InstallCMD}}
181-
RUN if [ ! -z "${INSTALL_CMD}" ]; then $INSTALL_CMD; fi
170+
RUN if [ ! -z "${INSTALL_CMD}" ]; then echo "${INSTALL_CMD}" > dep.sh; sh dep.sh; fi
182171
183172
ENV PORT=8080
184173
ENV PYTHONDONTWRITEBYTECODE=1

runtime/python_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestPythonGenerateDockerfile(t *testing.T) {
103103
path: "../testdata/python-pyproject",
104104
expected: []any{
105105
`ARG VERSION=3.12`,
106-
`ARG INSTALL_CMD="pip install --upgrade build setuptools \u0026\u0026 pip install .`,
106+
`ARG INSTALL_CMD="pip install --upgrade build setuptools && pip install .`,
107107
`ARG START_CMD="python -m pyproject"`,
108108
},
109109
},

runtime/ruby.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package runtime
33
import (
44
"bufio"
55
"bytes"
6-
"encoding/json"
76
"fmt"
87
"log/slog"
98
"os"
@@ -117,27 +116,12 @@ func (d *Ruby) GenerateDockerfile(path string) ([]byte, error) {
117116
See https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile`, *version, packageManager, installCMD, buildCMD, startCMD),
118117
)
119118

120-
if installCMD != "" {
121-
installCMDJSON, _ := json.Marshal(installCMD)
122-
installCMD = string(installCMDJSON)
123-
}
124-
125-
if buildCMD != "" {
126-
buildCMDJSON, _ := json.Marshal(buildCMD)
127-
buildCMD = string(buildCMDJSON)
128-
}
129-
130-
if startCMD != "" {
131-
startCMDJSON, _ := json.Marshal(startCMD)
132-
startCMD = string(startCMDJSON)
133-
}
134-
135119
var buf bytes.Buffer
136120
if err := tmpl.Option("missingkey=zero").Execute(&buf, map[string]string{
137121
"Version": *version,
138-
"InstallCMD": installCMD,
139-
"BuildCMD": buildCMD,
140-
"StartCMD": startCMD,
122+
"InstallCMD": safeCommand(installCMD),
123+
"BuildCMD": safeCommand(buildCMD),
124+
"StartCMD": safeCommand(startCMD),
141125
}); err != nil {
142126
return nil, fmt.Errorf("Failed to execute template")
143127
}
@@ -159,7 +143,7 @@ ENV NODE_ENV=production
159143
RUN chown -R nonroot:nonroot /app
160144
COPY --chown=nonroot:nonroot . .
161145
162-
RUN if [ ! -z "${INSTALL_CMD}" ]; then $INSTALL_CMD; fi
146+
RUN if [ ! -z "${INSTALL_CMD}" ]; then echo "${INSTALL_CMD}" > dep.sh; sh dep.sh; fi
163147
RUN if [ ! -z "${BUILD_CMD}" ]; then $BUILD_CMD; fi
164148
165149
ENV PORT=8080

runtime/ruby_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestRubyGenerateDockerfile(t *testing.T) {
9292
path: "../testdata/ruby-rails",
9393
expected: []any{
9494
`ARG VERSION=3.1`,
95-
`ARG INSTALL_CMD="bundle install \u0026\u0026 corepack enable pnpm \u0026\u0026 pnpm i --frozen-lockfile"`,
95+
`ARG INSTALL_CMD="bundle install && corepack enable pnpm && pnpm i --frozen-lockfile"`,
9696
`ARG BUILD_CMD="bundle exec rake assets:precompile"`,
9797
`ARG START_CMD="bundle exec rails server -b 0.0.0.0 -p ${PORT}`,
9898
},

0 commit comments

Comments
 (0)