mirror of https://github.com/status-im/fathom.git
Merge pull request #96 from SimePel/master
config: split up Parse into LoadEnv & Parse func. Thanks @SimePel!
This commit is contained in:
commit
6a27475faf
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
// LoadEnv loads env values from the supplied file
|
||||
func LoadEnv(file string) {
|
||||
if file == "" {
|
||||
log.Warn("Missing configuration file. Using defaults.")
|
||||
return
|
||||
}
|
||||
|
||||
if file != "" {
|
||||
absfile, _ := filepath.Abs(file)
|
||||
|
||||
// check if file exists
|
||||
_, err := os.Stat(absfile)
|
||||
absFile, _ := filepath.Abs(file)
|
||||
_, 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 {
|
||||
log.Warnf("Error reading configuration. File `%s` does not exist.", file)
|
||||
return
|
||||
}
|
||||
|
||||
if fileNotExists {
|
||||
if file != ".env" {
|
||||
log.Fatalf("Error reading configuration. File `%s` does not exist.", file)
|
||||
}
|
||||
} else {
|
||||
log.Printf("Configuration file: %s", absFile)
|
||||
|
||||
// read file into env values
|
||||
err = godotenv.Load(absfile)
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue