Skip to content

Commit 9b768c7

Browse files
authored
Lines from conf files that are comments should not be passed to shell.Expand (#374)
1 parent e8307a2 commit 9b768c7

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

cmd/backup/config_provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"os"
1010
"path/filepath"
11+
"strings"
1112

1213
"github.com/joho/godotenv"
1314
"github.com/offen/docker-volume-backup/internal/errwrap"
@@ -136,6 +137,10 @@ func source(path string) (map[string]string, error) {
136137
scanner := bufio.NewScanner(f)
137138
for scanner.Scan() {
138139
line := scanner.Text()
140+
line = strings.TrimSpace(line)
141+
if strings.HasPrefix(line, "#") {
142+
continue
143+
}
139144
withExpansion, err := shell.Expand(line, nil)
140145
if err != nil {
141146
return nil, errwrap.Wrap(err, "error expanding env")

cmd/backup/config_provider_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ func TestSource(t *testing.T) {
4949
"QUX": "yyy",
5050
},
5151
},
52+
{
53+
"comments",
54+
"testdata/comments.env",
55+
false,
56+
map[string]string{
57+
"BAR": "xxx",
58+
"BAZ": "yyy",
59+
},
60+
},
5261
}
5362

5463
os.Setenv("QUX", "yyy")

cmd/backup/testdata/comments.env

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This is a comment about `why` things are here
2+
# FOO="${bar:-qux}"
3+
# e.g. `backup-$HOSTNAME-%Y-%m-%dT%H-%M-%S.tar.gz`. Expansion happens before`
4+
5+
BAR=xxx
6+
7+
BAZ=$QUX

test/confd/01backup.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
# This is a comment
2+
# NOT=$(docker ps -aq)
3+
# e.g. `backup-$HOSTNAME-%Y-%m-%dT%H-%M-%S.tar.gz`. Expansion happens before`
4+
15
NAME="$EXPANSION_VALUE"
26
BACKUP_CRON_EXPRESSION="*/1 * * * *"

0 commit comments

Comments
 (0)