Skip to content

Commit af88d8b

Browse files
committed
fix bug
1 parent cb43071 commit af88d8b

File tree

5 files changed

+142
-6
lines changed

5 files changed

+142
-6
lines changed

lib/format.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func formatEnv(n *ExtendedNode, c *Config) string {
214214

215215
// Otherwise, we have a valid env command; fall back to original if parsing fails
216216
originalTrimmed := strings.TrimLeft(n.OriginalMultiline, " \t")
217-
parts := regexp.MustCompile(" ").Split(originalTrimmed, 2)
217+
parts := regexp.MustCompile("[ \t]").Split(originalTrimmed, 2)
218218
if len(parts) < 2 {
219219
return n.OriginalMultiline
220220
}
@@ -255,6 +255,7 @@ func formatShell(content string, hereDoc bool, c *Config) string {
255255
content = strings.Join(lines, "")
256256

257257
content = lineComment.ReplaceAllString(content, "$1`$2#`\\")
258+
// fmt.Printf("Content-1: %s\n", content)
258259

259260
/*
260261
```
@@ -270,10 +271,11 @@ func formatShell(content string, hereDoc bool, c *Config) string {
270271
```
271272
*/
272273

273-
commentContinuation := regexp.MustCompile(`(\\(?:\s*` + "`#.*#`" + `\\){1,}\s*)&&`)
274-
content = commentContinuation.ReplaceAllString(content, "&&$1")
274+
// The (.[^\\]) prevents an edge case with '&& \'. See tests/in/andissue.dockerfile
275+
commentContinuation := regexp.MustCompile(`(\\(?:\s*` + "`#.*#`" + `\\){1,}\s*)&&(.[^\\])`)
276+
content = commentContinuation.ReplaceAllString(content, "&&$1$2")
275277

276-
// log.Printf("Content0: %s\n", content)
278+
// fmt.Printf("Content0: %s\n", content)
277279
lines = strings.SplitAfter(content, "\n")
278280
/**
279281
if the next line is not a comment, and we didn't start with a continuation, don't add the `&&`.
@@ -425,12 +427,12 @@ func formatBasic(n *ExtendedNode, c *Config) string {
425427

426428
value, success := GetHeredoc(n)
427429
if !success {
428-
parts := regexp.MustCompile(" ").Split(originalTrimmed, 2)
430+
parts := regexp.MustCompile("[ \t]").Split(originalTrimmed, 2)
429431
if len(parts) < 2 {
430432
// No argument after directive; just return the directive itself
431433
return strings.ToUpper(n.Value) + "\n"
432434
}
433-
value = parts[1]
435+
value = strings.TrimLeft(parts[1], " \t")
434436
}
435437
return IndentFollowingLines(strings.ToUpper(n.Value)+" "+value, c.IndentSize)
436438
}

tests/in/andissue.dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
RUN foo \
2+
# comment
3+
&& \
4+
# comment 2
5+
bar && \
6+
# comment 3
7+
baz

tests/in/issue32.dockerfile

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
FROM debian:bullseye-slim
2+
LABEL maintainer="... <...>"
3+
4+
COPY sources.list /etc/apt/sources.list
5+
6+
# Upgrade the system + Install all packages
7+
ARG DEBIAN_FRONTEND=noninteractive
8+
# Install all packages below :
9+
RUN apt-get update && \
10+
apt-get install --no-install-recommends -y \
11+
ca-certificates \
12+
imagemagick \
13+
php-bcmath \
14+
php-curl \
15+
php-db \
16+
php-fpm \
17+
php-gd \
18+
php-imagick \
19+
php-intl \
20+
php-ldap \
21+
php-mail \
22+
php-mail-mime \
23+
php-mbstring \
24+
php-mysql \
25+
php-redis \
26+
php-soap \
27+
php-sqlite3 \
28+
php-xml \
29+
php-zip \
30+
ssmtp \
31+
# bind9-host iputils-ping lsof iproute2 netcat-openbsd procps strace tcpdump traceroute \
32+
&& \
33+
# Clean and save space
34+
rm -rf /var/lib/apt/lists/* && \
35+
# Set timezone
36+
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime && \
37+
dpkg-reconfigure tzdata
38+
39+
WORKDIR /var/www/html
40+
41+
ADD https://.../check_mk/agents/check_mk_agent.linux /usr/bin/check_mk_agent
42+
COPY php_fpm_pools /usr/lib/check_mk_agent/plugins/
43+
COPY php_fpm_pools.cfg /etc/check_mk/
44+
RUN chmod 755 /usr/bin/check_mk_agent /usr/lib/check_mk_agent/plugins/*
45+
46+
COPY www.conf /etc/php/7.4/fpm/pool.d/
47+
COPY ssmtp.conf /etc/ssmtp/ssmtp.conf
48+
COPY environment /etc/environment
49+
50+
RUN mkdir -m 0755 /run/php
51+
52+
ENV http_proxy ...
53+
ENV https_proxy ...
54+
ENV no_proxy ...
55+
ENV HTTP_PROXY ...
56+
ENV HTTPS_PROXY ...
57+
ENV NO_PROXY ...
58+
59+
EXPOSE 9000
60+
61+
CMD ["/usr/sbin/php-fpm7.4", "--nodaemonize", "--fpm-config", "/etc/php/7.4/fpm/php-fpm.conf"]

tests/out/andissue.dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
RUN foo \
2+
# comment
3+
# comment 2
4+
&& bar \
5+
# comment 3
6+
&& baz

tests/out/issue32.dockerfile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
FROM debian:bullseye-slim
2+
LABEL maintainer="... <...>"
3+
4+
COPY sources.list /etc/apt/sources.list
5+
6+
# Upgrade the system + Install all packages
7+
ARG DEBIAN_FRONTEND=noninteractive
8+
# Install all packages below :
9+
RUN apt-get update \
10+
&& apt-get install --no-install-recommends -y \
11+
ca-certificates \
12+
imagemagick \
13+
php-bcmath \
14+
php-curl \
15+
php-db \
16+
php-fpm \
17+
php-gd \
18+
php-imagick \
19+
php-intl \
20+
php-ldap \
21+
php-mail \
22+
php-mail-mime \
23+
php-mbstring \
24+
php-mysql \
25+
php-redis \
26+
php-soap \
27+
php-sqlite3 \
28+
php-xml \
29+
php-zip \
30+
ssmtp \
31+
# bind9-host iputils-ping lsof iproute2 netcat-openbsd procps strace tcpdump traceroute \
32+
# Clean and save space
33+
&& rm -rf /var/lib/apt/lists/* \
34+
# Set timezone
35+
&& ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime \
36+
&& dpkg-reconfigure tzdata
37+
38+
WORKDIR /var/www/html
39+
40+
ADD https://.../check_mk/agents/check_mk_agent.linux /usr/bin/check_mk_agent
41+
COPY php_fpm_pools /usr/lib/check_mk_agent/plugins/
42+
COPY php_fpm_pools.cfg /etc/check_mk/
43+
RUN chmod 755 /usr/bin/check_mk_agent /usr/lib/check_mk_agent/plugins/*
44+
45+
COPY www.conf /etc/php/7.4/fpm/pool.d/
46+
COPY ssmtp.conf /etc/ssmtp/ssmtp.conf
47+
COPY environment /etc/environment
48+
49+
RUN mkdir -m 0755 /run/php
50+
51+
ENV http_proxy=...
52+
ENV https_proxy=...
53+
ENV no_proxy=...
54+
ENV HTTP_PROXY=...
55+
ENV HTTPS_PROXY=...
56+
ENV NO_PROXY=...
57+
58+
EXPOSE 9000
59+
60+
CMD ["/usr/sbin/php-fpm7.4", "--nodaemonize", "--fpm-config", "/etc/php/7.4/fpm/php-fpm.conf"]

0 commit comments

Comments
 (0)