From 415442e6e76393f2dd157d5b02fb3cb914353058 Mon Sep 17 00:00:00 2001 From: Jared Lunde Date: Fri, 17 May 2024 08:12:34 -0600 Subject: [PATCH] fix: fix unicode escaping install commandss --- runtime/php.go | 23 ++++------------------- runtime/php_test.go | 2 +- runtime/python.go | 17 +++-------------- runtime/python_test.go | 2 +- runtime/ruby.go | 24 ++++-------------------- runtime/ruby_test.go | 2 +- 6 files changed, 14 insertions(+), 56 deletions(-) diff --git a/runtime/php.go b/runtime/php.go index 4f04ec4..2135971 100644 --- a/runtime/php.go +++ b/runtime/php.go @@ -123,27 +123,12 @@ func (d *PHP) GenerateDockerfile(path string) ([]byte, error) { See https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile`, *version, installCMD, buildCMD, startCMD), ) - if installCMD != "" { - installCMDJSON, _ := json.Marshal(installCMD) - installCMD = string(installCMDJSON) - } - - if buildCMD != "" { - buildCMDJSON, _ := json.Marshal(buildCMD) - buildCMD = string(buildCMDJSON) - } - - if startCMD != "" { - startCMDJSON, _ := json.Marshal(startCMD) - startCMD = string(startCMDJSON) - } - var buf bytes.Buffer if err := tmpl.Option("missingkey=zero").Execute(&buf, map[string]string{ "Version": *version, - "InstallCMD": installCMD, - "BuildCMD": buildCMD, - "StartCMD": startCMD, + "InstallCMD": safeCommand(installCMD), + "BuildCMD": safeCommand(buildCMD), + "StartCMD": safeCommand(startCMD), }); err != nil { return nil, fmt.Errorf("Failed to execute template") } @@ -160,7 +145,7 @@ COPY . . ARG INSTALL_CMD={{.InstallCMD}} ARG BUILD_CMD={{.BuildCMD}} -RUN if [ ! -z "${INSTALL_CMD}" ]; then $INSTALL_CMD; fi +RUN if [ ! -z "${INSTALL_CMD}" ]; then echo "${INSTALL_CMD}" > dep.sh; sh dep.sh; fi RUN if [ ! -z "${BUILD_CMD}" ]; then $BUILD_CMD; fi FROM php:${VERSION}-apache AS runtime diff --git a/runtime/php_test.go b/runtime/php_test.go index a821682..bcac585 100644 --- a/runtime/php_test.go +++ b/runtime/php_test.go @@ -60,7 +60,7 @@ func TestPHPGenerateDockerfile(t *testing.T) { { name: "PHP project with composer", path: "../testdata/php-composer", - 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`}, + 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`}, }, { name: "PHP project with NPM", diff --git a/runtime/python.go b/runtime/python.go index d0be3fa..978dedd 100644 --- a/runtime/python.go +++ b/runtime/python.go @@ -3,7 +3,6 @@ package runtime import ( "bufio" "bytes" - "encoding/json" "fmt" "log/slog" "os" @@ -146,21 +145,11 @@ func (d *Python) GenerateDockerfile(path string) ([]byte, error) { See https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile`, *version, installCMD, startCMD), ) - if installCMD != "" { - installCMDJSON, _ := json.Marshal(installCMD) - installCMD = string(installCMDJSON) - } - - if startCMD != "" { - startCMDJSON, _ := json.Marshal(startCMD) - startCMD = string(startCMDJSON) - } - var buf bytes.Buffer if err := tmpl.Option("missingkey=zero").Execute(&buf, map[string]string{ "Version": *version, - "InstallCMD": installCMD, - "StartCMD": startCMD, + "InstallCMD": safeCommand(installCMD), + "StartCMD": safeCommand(startCMD), }); err != nil { return nil, fmt.Errorf("Failed to execute template") } @@ -178,7 +167,7 @@ RUN chown -R nonroot:nonroot /app COPY --chown=nonroot:nonroot . . ARG INSTALL_CMD={{.InstallCMD}} -RUN if [ ! -z "${INSTALL_CMD}" ]; then $INSTALL_CMD; fi +RUN if [ ! -z "${INSTALL_CMD}" ]; then echo "${INSTALL_CMD}" > dep.sh; sh dep.sh; fi ENV PORT=8080 ENV PYTHONDONTWRITEBYTECODE=1 diff --git a/runtime/python_test.go b/runtime/python_test.go index 3561730..4e82e4e 100644 --- a/runtime/python_test.go +++ b/runtime/python_test.go @@ -103,7 +103,7 @@ func TestPythonGenerateDockerfile(t *testing.T) { path: "../testdata/python-pyproject", expected: []any{ `ARG VERSION=3.12`, - `ARG INSTALL_CMD="pip install --upgrade build setuptools \u0026\u0026 pip install .`, + `ARG INSTALL_CMD="pip install --upgrade build setuptools && pip install .`, `ARG START_CMD="python -m pyproject"`, }, }, diff --git a/runtime/ruby.go b/runtime/ruby.go index 9f3a604..3ac11fb 100644 --- a/runtime/ruby.go +++ b/runtime/ruby.go @@ -3,7 +3,6 @@ package runtime import ( "bufio" "bytes" - "encoding/json" "fmt" "log/slog" "os" @@ -117,27 +116,12 @@ func (d *Ruby) GenerateDockerfile(path string) ([]byte, error) { See https://flexstack.com/docs/languages-and-frameworks/autogenerate-dockerfile`, *version, packageManager, installCMD, buildCMD, startCMD), ) - if installCMD != "" { - installCMDJSON, _ := json.Marshal(installCMD) - installCMD = string(installCMDJSON) - } - - if buildCMD != "" { - buildCMDJSON, _ := json.Marshal(buildCMD) - buildCMD = string(buildCMDJSON) - } - - if startCMD != "" { - startCMDJSON, _ := json.Marshal(startCMD) - startCMD = string(startCMDJSON) - } - var buf bytes.Buffer if err := tmpl.Option("missingkey=zero").Execute(&buf, map[string]string{ "Version": *version, - "InstallCMD": installCMD, - "BuildCMD": buildCMD, - "StartCMD": startCMD, + "InstallCMD": safeCommand(installCMD), + "BuildCMD": safeCommand(buildCMD), + "StartCMD": safeCommand(startCMD), }); err != nil { return nil, fmt.Errorf("Failed to execute template") } @@ -159,7 +143,7 @@ ENV NODE_ENV=production RUN chown -R nonroot:nonroot /app COPY --chown=nonroot:nonroot . . -RUN if [ ! -z "${INSTALL_CMD}" ]; then $INSTALL_CMD; fi +RUN if [ ! -z "${INSTALL_CMD}" ]; then echo "${INSTALL_CMD}" > dep.sh; sh dep.sh; fi RUN if [ ! -z "${BUILD_CMD}" ]; then $BUILD_CMD; fi ENV PORT=8080 diff --git a/runtime/ruby_test.go b/runtime/ruby_test.go index 62c70da..aca0db9 100644 --- a/runtime/ruby_test.go +++ b/runtime/ruby_test.go @@ -92,7 +92,7 @@ func TestRubyGenerateDockerfile(t *testing.T) { path: "../testdata/ruby-rails", expected: []any{ `ARG VERSION=3.1`, - `ARG INSTALL_CMD="bundle install \u0026\u0026 corepack enable pnpm \u0026\u0026 pnpm i --frozen-lockfile"`, + `ARG INSTALL_CMD="bundle install && corepack enable pnpm && pnpm i --frozen-lockfile"`, `ARG BUILD_CMD="bundle exec rake assets:precompile"`, `ARG START_CMD="bundle exec rails server -b 0.0.0.0 -p ${PORT}`, },