mirror of
https://github.com/offen/docker-volume-backup.git
synced 2026-04-20 23:52:41 +02:00
This reverts commit 911fc5a223.
This commit is contained in:
@@ -4,14 +4,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/offen/docker-volume-backup/internal/errwrap"
|
||||
"github.com/offen/envconfig"
|
||||
"mvdan.cc/sh/shell"
|
||||
)
|
||||
|
||||
type configStrategy string
|
||||
@@ -100,7 +99,11 @@ func loadConfigsFromEnvFiles(directory string) ([]*Config, error) {
|
||||
continue
|
||||
}
|
||||
p := filepath.Join(directory, item.Name())
|
||||
envFile, err := source(p)
|
||||
f, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
return nil, errwrap.Wrap(err, fmt.Sprintf("error reading %s", item.Name()))
|
||||
}
|
||||
envFile, err := godotenv.Unmarshal(os.ExpandEnv(string(f)))
|
||||
if err != nil {
|
||||
return nil, errwrap.Wrap(err, fmt.Sprintf("error reading config file %s", p))
|
||||
}
|
||||
@@ -122,17 +125,3 @@ func loadConfigsFromEnvFiles(directory string) ([]*Config, error) {
|
||||
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
// source tries to mimic the pre v2.37.0 behavior of calling
|
||||
// `set +a; source $path; set -a` and returns the env vars as a map
|
||||
func source(path string) (map[string]string, error) {
|
||||
vars, err := shell.SourceFile(context.Background(), path)
|
||||
if err != nil {
|
||||
return nil, errwrap.Wrap(err, "error sourcing conf file")
|
||||
}
|
||||
result := map[string]string{}
|
||||
for key, value := range vars {
|
||||
result[key] = value.String()
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSource(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expectError bool
|
||||
expectedOutput map[string]string
|
||||
}{
|
||||
{
|
||||
"default",
|
||||
"testdata/default.env",
|
||||
false,
|
||||
map[string]string{
|
||||
"FOO": "bar",
|
||||
"BAZ": "qux",
|
||||
},
|
||||
},
|
||||
{
|
||||
"not found",
|
||||
"testdata/nope.env",
|
||||
true,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"braces",
|
||||
"testdata/braces.env",
|
||||
false,
|
||||
map[string]string{
|
||||
"FOO": "qux",
|
||||
"BAR": "xxx",
|
||||
"BAZ": "",
|
||||
},
|
||||
},
|
||||
{
|
||||
"expansion",
|
||||
"testdata/expansion.env",
|
||||
false,
|
||||
map[string]string{
|
||||
"BAR": "xxx",
|
||||
"FOO": "xxx",
|
||||
"BAZ": "xxx",
|
||||
"QUX": "yyy",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
os.Setenv("QUX", "yyy")
|
||||
defer os.Unsetenv("QUX")
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
result, err := source(test.input)
|
||||
if (err != nil) != test.expectError {
|
||||
t.Errorf("Unexpected error value %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(test.expectedOutput, result) {
|
||||
t.Errorf("Expected %v, got %v", test.expectedOutput, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
3
cmd/backup/testdata/braces.env
vendored
3
cmd/backup/testdata/braces.env
vendored
@@ -1,3 +0,0 @@
|
||||
FOO=${bar:-qux}
|
||||
BAR=xxx
|
||||
BAZ=$NOPE
|
||||
2
cmd/backup/testdata/default.env
vendored
2
cmd/backup/testdata/default.env
vendored
@@ -1,2 +0,0 @@
|
||||
FOO=bar
|
||||
BAZ=qux
|
||||
4
cmd/backup/testdata/expansion.env
vendored
4
cmd/backup/testdata/expansion.env
vendored
@@ -1,4 +0,0 @@
|
||||
BAR=xxx
|
||||
FOO=${BAR}
|
||||
BAZ=$BAR
|
||||
QUX=${QUX}
|
||||
Reference in New Issue
Block a user