A .env file in the working directory is now optional
This commit is contained in:
1 parent
0b1952bd69
commit
1e3acaeff3
5 files changed
+30
-9
No files matched your search
@@ -73,11 +73,17 @@ func TestConfigFlag(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingEnvFile(t *testing.T) {
|
||||
// A .env file is optional, but one that can't be parsed is an error.
|
||||
func TestEnvFile(t *testing.T) {
|
||||
cmdtest.Setup(t, appConfig)
|
||||
if err := os.Remove(".env"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := cmdtest.Run(t, newRoot, "", "level"); err != nil {
|
||||
t.Errorf("without a .env file: %v; want no error", err)
|
||||
}
|
||||
|
||||
cmdtest.WriteFile(t, ".env", "NOT VALID\n")
|
||||
_, err := cmdtest.Run(t, newRoot, "", "level")
|
||||
cmdtest.ErrContains(t, err, ".env")
|
||||
}
|
||||
@@ -8,8 +8,8 @@ environment variables, merged through [Viper](https://github.com/spf13/viper).
|
||||
## Loading order
|
||||
`Init(cfgFile, &conf)` builds the configuration in this order:
|
||||
|
||||
1. Loads `.env` from the working directory into the environment. A missing
|
||||
`.env` file is an error.
|
||||
1. Loads `.env` from the working directory into the environment, if there
|
||||
is one. A `.env` file that can't be read or parsed is an error.
|
||||
2. Sets the defaults from the `default` struct tags as Viper defaults.
|
||||
3. Reads the config file: `cfgFile` if given, otherwise the first
|
||||
`config.yaml` found in `/etc/job-search` then the working directory. If no
|
||||
|
||||
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -34,9 +35,13 @@ func isExplicitlySet(key string) bool {
|
||||
}
|
||||
|
||||
// loadEnv loads environment variables from a .env file in the working
|
||||
// directory.
|
||||
// directory, if there is one. One that can't be read or parsed is an error.
|
||||
func loadEnv() error {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
err := godotenv.Load()
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid .env file: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -141,9 +141,19 @@ func TestInitKeepsCommandLineLevel(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A .env file is optional.
|
||||
func TestInitWithoutEnvFile(t *testing.T) {
|
||||
initDir(t, nil, testConfigYAML)
|
||||
var conf JobSearchConfig
|
||||
if err := Init("", &conf); err != nil {
|
||||
t.Fatalf("err = %v; want nil without a .env file", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitErrors(t *testing.T) {
|
||||
t.Run("no .env file", func(t *testing.T) {
|
||||
initDir(t, nil, testConfigYAML)
|
||||
t.Run("invalid .env file", func(t *testing.T) {
|
||||
dir := initDir(t, nil, testConfigYAML)
|
||||
writeFile(t, filepath.Join(dir, ".env"), "NOT VALID\n")
|
||||
var conf JobSearchConfig
|
||||
if err := Init("", &conf); err == nil ||
|
||||
!strings.Contains(err.Error(), ".env") {
|
||||
|
||||
Reference in new issue
Block a user