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

This commit is contained in:
Frederik Ring 2024-02-23 17:53:04 +01:00 committed by GitHub
parent e8307a2b5b
commit 9b768c71e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/offen/docker-volume-backup/internal/errwrap" "github.com/offen/docker-volume-backup/internal/errwrap"
@ -136,6 +137,10 @@ func source(path string) (map[string]string, error) {
scanner := bufio.NewScanner(f) scanner := bufio.NewScanner(f)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "#") {
continue
}
withExpansion, err := shell.Expand(line, nil) withExpansion, err := shell.Expand(line, nil)
if err != nil { if err != nil {
return nil, errwrap.Wrap(err, "error expanding env") return nil, errwrap.Wrap(err, "error expanding env")

View File

@ -49,6 +49,15 @@ func TestSource(t *testing.T) {
"QUX": "yyy", "QUX": "yyy",
}, },
}, },
{
"comments",
"testdata/comments.env",
false,
map[string]string{
"BAR": "xxx",
"BAZ": "yyy",
},
},
} }
os.Setenv("QUX", "yyy") os.Setenv("QUX", "yyy")

7
cmd/backup/testdata/comments.env vendored Normal file
View File

@ -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

View File

@ -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" NAME="$EXPANSION_VALUE"
BACKUP_CRON_EXPRESSION="*/1 * * * *" BACKUP_CRON_EXPRESSION="*/1 * * * *"