diff --git a/cmd/fathom/main.go b/cmd/fathom/main.go index aab41d7..18184ed 100644 --- a/cmd/fathom/main.go +++ b/cmd/fathom/main.go @@ -54,7 +54,8 @@ func main() { func before(c *cli.Context) error { configFile := c.String("config") - app.config = config.Parse(configFile) + config.LoadEnv(configFile) + app.config = config.Parse() app.database = datastore.New(app.config.Database) return nil } diff --git a/pkg/config/config.go b/pkg/config/config.go index a23df8f..b288116 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -17,40 +17,37 @@ type Config struct { Secret string } -// Parses the supplied file + environment into a Config struct -func Parse(file string) *Config { - var cfg Config - var err error - - if file != "" { - absfile, _ := filepath.Abs(file) - - // check if file exists - _, err := os.Stat(absfile) - fileNotExists := os.IsNotExist(err) - - if file == ".env" && fileNotExists { - log.Warnf("Missing configuration file. Using defaults.") - } else { - log.Printf("Configuration file: %s", absfile) - } - - if fileNotExists { - if file != ".env" { - log.Fatalf("Error reading configuration. File `%s` does not exist.", file) - } - } else { - // read file into env values - err = godotenv.Load(absfile) - if err != nil { - log.Fatalf("Error parsing configuration file: %s", err) - } - } - +// LoadEnv loads env values from the supplied file +func LoadEnv(file string) { + if file == "" { + log.Warn("Missing configuration file. Using defaults.") + return } + absFile, _ := filepath.Abs(file) + _, err := os.Stat(absFile) + fileNotExists := os.IsNotExist(err) + + if fileNotExists { + log.Warnf("Error reading configuration. File `%s` does not exist.", file) + return + } + + log.Printf("Configuration file: %s", absFile) + + // read file into env values + err = godotenv.Load(absFile) + if err != nil { + log.Fatalf("Error parsing configuration file: %s", err) + } +} + +// Parse environment into a Config struct +func Parse() *Config { + var cfg Config + // with config file loaded into env values, we can now parse env into our config struct - err = envconfig.Process("Fathom", &cfg) + err := envconfig.Process("Fathom", &cfg) if err != nil { log.Fatalf("Error parsing configuration from environment: %s", err) } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 4b47bb4..6070fc8 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1,30 +1,52 @@ package config import ( + "io/ioutil" "os" "testing" ) +func TestLoadEnv(t *testing.T) { + before := len(os.Environ()) + LoadEnv("") + LoadEnv("1230") + after := len(os.Environ()) + + if before != after { + t.Errorf("Expected the same number of env values") + } + + data := []byte("FATHOM_DATABASE_DRIVER=\"sqlite3\"") + ioutil.WriteFile("env_values", data, 0644) + defer os.Remove("env_values") + + LoadEnv("env_values") + + got := os.Getenv("FATHOM_DATABASE_DRIVER") + if got != "sqlite3" { + t.Errorf("Expected %v, got %v", "sqlite3", got) + } +} + func TestParse(t *testing.T) { // empty config, should not fatal - cfg := Parse("") + cfg := Parse() if cfg.Secret == "" { t.Errorf("expected secret, got empty string") } secret := "my-super-secret-string" os.Setenv("FATHOM_SECRET", secret) - cfg = Parse("") + cfg = Parse() if cfg.Secret != secret { t.Errorf("Expected %#v, got %#v", secret, cfg.Secret) } os.Setenv("FATHOM_DATABASE_DRIVER", "sqlite") - cfg = Parse("") + cfg = Parse() if cfg.Database.Driver != "sqlite3" { t.Errorf("expected %#v, got %#v", "sqlite3", cfg.Database.Driver) } - } func TestRandomString(t *testing.T) {