Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lines from conf files that are comments should not be passed to shell.Expand #374

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/backup/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/joho/godotenv"
"github.com/offen/docker-volume-backup/internal/errwrap"
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions cmd/backup/config_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions cmd/backup/testdata/comments.env
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/confd/01backup.env
Original file line number Diff line number Diff line change
@@ -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 * * * *"
Loading