-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
While I'm not sure there is a formal dotenv specification, I've used a library that's meant to be compatible with other language libraries, which should make things pretty familiar and compatible. I've also attempted to model the semantics used by Docker Compose for file configuration, although 100% compatibility is probably not a guarantee this project will ever make.
- Loading branch information
Showing
12 changed files
with
458 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package runner | ||
|
||
import ( | ||
"maps" | ||
"os" | ||
|
||
"github.com/joho/godotenv" | ||
|
||
"github.com/rliebz/tusk/marshal" | ||
) | ||
|
||
// EnvFile is a dotenv file that should be parsed during configuration start. | ||
type EnvFile struct { | ||
Path string `yaml:"path"` | ||
Required bool `yaml:"required"` | ||
} | ||
|
||
// UnmarshalYAML allows a string to represent a required path. | ||
func (f *EnvFile) UnmarshalYAML(unmarshal func(any) error) error { | ||
var path string | ||
pathCandidate := marshal.UnmarshalCandidate{ | ||
Unmarshal: func() error { return unmarshal(&path) }, | ||
Assign: func() { *f = EnvFile{Path: path, Required: true} }, | ||
} | ||
|
||
type envFileType EnvFile // Use new type to avoid recursion | ||
var envFileItem envFileType | ||
envFileCandidate := marshal.UnmarshalCandidate{ | ||
Unmarshal: func() error { return unmarshal(&envFileItem) }, | ||
Assign: func() { *f = EnvFile(envFileItem) }, | ||
} | ||
|
||
return marshal.UnmarshalOneOf(pathCandidate, envFileCandidate) | ||
} | ||
|
||
// loadEnvFiles sets env vars from a set of file configs. Values are not | ||
// overridden. | ||
// | ||
// If no files are specified, it will load from an optional default of .env. | ||
// If an empty list is specified, no files will be loaded. | ||
func loadEnvFiles(envFiles []EnvFile) error { | ||
// An explicit [] is an obvious attempt to remove the default, so check only | ||
// for nilness. | ||
if envFiles == nil { | ||
envFiles = []EnvFile{{Path: ".env", Required: false}} | ||
} | ||
|
||
envMap := make(map[string]string) | ||
for _, envFile := range envFiles { | ||
m, err := readEnvFile(envFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
maps.Copy(envMap, m) | ||
} | ||
|
||
for k, v := range envMap { | ||
if _, ok := os.LookupEnv(k); !ok { | ||
if err := os.Setenv(k, v); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func readEnvFile(envFile EnvFile) (map[string]string, error) { | ||
m, err := godotenv.Read(envFile.Path) | ||
switch { | ||
case !envFile.Required && os.IsNotExist(err): | ||
case err != nil: | ||
return nil, err | ||
} | ||
|
||
return m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package runner | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/rliebz/ghost" | ||
"github.com/rliebz/ghost/be" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func TestEnvFile_UnmarshalYAML(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data string | ||
want EnvFile | ||
}{ | ||
{ | ||
name: "empty", | ||
data: ``, | ||
want: EnvFile{}, | ||
}, | ||
{ | ||
name: "string", | ||
data: `"dot.env"`, | ||
want: EnvFile{ | ||
Path: "dot.env", | ||
Required: true, | ||
}, | ||
}, | ||
{ | ||
name: "object required", | ||
data: `{path: dot.env, required: true}`, | ||
want: EnvFile{ | ||
Path: "dot.env", | ||
Required: true, | ||
}, | ||
}, | ||
{ | ||
name: "object not required", | ||
data: `{path: dot.env, required: false}`, | ||
want: EnvFile{ | ||
Path: "dot.env", | ||
Required: false, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := ghost.New(t) | ||
|
||
var envFile EnvFile | ||
err := yaml.UnmarshalStrict([]byte(tt.data), &envFile) | ||
g.NoError(err) | ||
|
||
g.Should(be.Equal(envFile, tt.want)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_loadEnvFiles(t *testing.T) { | ||
t.Run("default used", func(t *testing.T) { | ||
g := ghost.New(t) | ||
stashEnv(t) | ||
tmpdir := useTempDir(t) | ||
|
||
t.Setenv("BAZ", "bazvalue") | ||
|
||
// Just use a little bit of all the fancy syntax | ||
data := []byte(` | ||
# comment | ||
FOO=foovalue | ||
export BAR=barvalue | ||
BAZ=newvalue | ||
QUUX=${FOO} | ||
`) | ||
|
||
err := os.WriteFile(filepath.Join(tmpdir, ".env"), data, 0o644) | ||
g.NoError(err) | ||
|
||
err = loadEnvFiles(nil) | ||
g.NoError(err) | ||
|
||
g.Should(be.All( | ||
be.Equal(os.Getenv("FOO"), "foovalue"), | ||
be.Equal(os.Getenv("BAR"), "barvalue"), | ||
be.Equal(os.Getenv("BAZ"), "bazvalue"), | ||
be.Equal(os.Getenv("QUUX"), "foovalue"), | ||
)) | ||
}) | ||
|
||
t.Run("default not found", func(t *testing.T) { | ||
g := ghost.New(t) | ||
stashEnv(t) | ||
useTempDir(t) | ||
|
||
err := loadEnvFiles(nil) | ||
g.NoError(err) | ||
}) | ||
|
||
t.Run("dev null", func(t *testing.T) { | ||
g := ghost.New(t) | ||
stashEnv(t) | ||
useTempDir(t) | ||
|
||
err := loadEnvFiles([]EnvFile{{Path: "/dev/null"}}) | ||
g.NoError(err) | ||
}) | ||
|
||
t.Run("empty list", func(t *testing.T) { | ||
g := ghost.New(t) | ||
stashEnv(t) | ||
tmpdir := useTempDir(t) | ||
|
||
t.Setenv("BAZ", "bazvalue") | ||
|
||
err := os.WriteFile(filepath.Join(tmpdir, ".env"), []byte(`FOO=foovalue`), 0o644) | ||
g.NoError(err) | ||
|
||
err = loadEnvFiles([]EnvFile{}) | ||
g.NoError(err) | ||
|
||
g.Should(be.Zero(os.Getenv("FOO"))) | ||
}) | ||
|
||
t.Run("required found", func(t *testing.T) { | ||
g := ghost.New(t) | ||
stashEnv(t) | ||
tmpdir := useTempDir(t) | ||
|
||
err := os.WriteFile(filepath.Join(tmpdir, ".env"), []byte("FOO=foovalue"), 0o644) | ||
g.NoError(err) | ||
|
||
err = loadEnvFiles([]EnvFile{ | ||
{Path: ".env", Required: true}, | ||
}) | ||
g.NoError(err) | ||
|
||
g.Should(be.Equal(os.Getenv("FOO"), "foovalue")) | ||
}) | ||
|
||
t.Run("required not found", func(t *testing.T) { | ||
g := ghost.New(t) | ||
stashEnv(t) | ||
useTempDir(t) | ||
|
||
err := loadEnvFiles([]EnvFile{ | ||
{Path: ".env", Required: true}, | ||
}) | ||
g.Should(be.ErrorContaining(err, "open .env:")) | ||
g.Should(be.True(os.IsNotExist(err))) | ||
}) | ||
|
||
t.Run("overrides earlier values", func(t *testing.T) { | ||
g := ghost.New(t) | ||
stashEnv(t) | ||
tmpdir := useTempDir(t) | ||
|
||
err := os.WriteFile(filepath.Join(tmpdir, "1.env"), []byte("FOO=one"), 0o644) | ||
g.NoError(err) | ||
err = os.WriteFile(filepath.Join(tmpdir, "2.env"), []byte("FOO=two"), 0o644) | ||
g.NoError(err) | ||
err = os.WriteFile(filepath.Join(tmpdir, "3.env"), []byte("BAR=three"), 0o644) | ||
g.NoError(err) | ||
|
||
err = loadEnvFiles([]EnvFile{ | ||
{Path: "1.env"}, | ||
{Path: "2.env"}, | ||
{Path: "3.env"}, | ||
}) | ||
g.NoError(err) | ||
|
||
g.Should(be.Equal(os.Getenv("FOO"), "two")) | ||
g.Should(be.Equal(os.Getenv("BAR"), "three")) | ||
}) | ||
} |
Oops, something went wrong.