From 9b768c71e6de60ab0264f199b5b5abf911ae6354 Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Fri, 23 Feb 2024 17:53:04 +0100 Subject: [PATCH] Lines from conf files that are comments should not be passed to shell.Expand (#374) --- cmd/backup/config_provider.go | 5 +++++ cmd/backup/config_provider_test.go | 9 +++++++++ cmd/backup/testdata/comments.env | 7 +++++++ test/confd/01backup.env | 4 ++++ 4 files changed, 25 insertions(+) create mode 100644 cmd/backup/testdata/comments.env diff --git a/cmd/backup/config_provider.go b/cmd/backup/config_provider.go index fb42e0da..5cf82afe 100644 --- a/cmd/backup/config_provider.go +++ b/cmd/backup/config_provider.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/joho/godotenv" "github.com/offen/docker-volume-backup/internal/errwrap" @@ -136,6 +137,10 @@ func source(path string) (map[string]string, error) { scanner := bufio.NewScanner(f) for scanner.Scan() { line := scanner.Text() + line = strings.TrimSpace(line) + if strings.HasPrefix(line, "#") { + continue + } withExpansion, err := shell.Expand(line, nil) if err != nil { return nil, errwrap.Wrap(err, "error expanding env") diff --git a/cmd/backup/config_provider_test.go b/cmd/backup/config_provider_test.go index 234de675..5e7fc07e 100644 --- a/cmd/backup/config_provider_test.go +++ b/cmd/backup/config_provider_test.go @@ -49,6 +49,15 @@ func TestSource(t *testing.T) { "QUX": "yyy", }, }, + { + "comments", + "testdata/comments.env", + false, + map[string]string{ + "BAR": "xxx", + "BAZ": "yyy", + }, + }, } os.Setenv("QUX", "yyy") diff --git a/cmd/backup/testdata/comments.env b/cmd/backup/testdata/comments.env new file mode 100644 index 00000000..4ee84378 --- /dev/null +++ b/cmd/backup/testdata/comments.env @@ -0,0 +1,7 @@ +# This is a comment about `why` things are here +# FOO="${bar:-qux}" + # e.g. `backup-$HOSTNAME-%Y-%m-%dT%H-%M-%S.tar.gz`. Expansion happens before` + +BAR=xxx + +BAZ=$QUX diff --git a/test/confd/01backup.env b/test/confd/01backup.env index d55f1728..5ccaa522 100644 --- a/test/confd/01backup.env +++ b/test/confd/01backup.env @@ -1,2 +1,6 @@ +# This is a comment +# NOT=$(docker ps -aq) +# e.g. `backup-$HOSTNAME-%Y-%m-%dT%H-%M-%S.tar.gz`. Expansion happens before` + NAME="$EXPANSION_VALUE" BACKUP_CRON_EXPRESSION="*/1 * * * *"