mirror of https://github.com/status-im/fathom.git
add separate function for loading env values from file
This commit is contained in:
parent
2ef550bc27
commit
64b131f7f5
|
@ -54,7 +54,8 @@ func main() {
|
||||||
|
|
||||||
func before(c *cli.Context) error {
|
func before(c *cli.Context) error {
|
||||||
configFile := c.String("config")
|
configFile := c.String("config")
|
||||||
app.config = config.Parse(configFile)
|
LoadEnv(configFile)
|
||||||
|
app.config = config.Parse()
|
||||||
app.database = datastore.New(app.config.Database)
|
app.database = datastore.New(app.config.Database)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,40 +17,37 @@ type Config struct {
|
||||||
Secret string
|
Secret string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parses the supplied file + environment into a Config struct
|
// LoadEnv loads env values from the supplied file
|
||||||
func Parse(file string) *Config {
|
func LoadEnv(file string) {
|
||||||
var cfg Config
|
if file == "" {
|
||||||
var err error
|
log.Warn("Missing configuration file. Using defaults.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if file != "" {
|
absFile, _ := filepath.Abs(file)
|
||||||
absfile, _ := filepath.Abs(file)
|
_, err := os.Stat(absFile)
|
||||||
|
|
||||||
// check if file exists
|
|
||||||
_, err := os.Stat(absfile)
|
|
||||||
fileNotExists := os.IsNotExist(err)
|
fileNotExists := os.IsNotExist(err)
|
||||||
|
|
||||||
if file == ".env" && fileNotExists {
|
if fileNotExists {
|
||||||
log.Warnf("Missing configuration file. Using defaults.")
|
log.Warnf("Error reading configuration. File `%s` does not exist.", file)
|
||||||
} else {
|
return
|
||||||
log.Printf("Configuration file: %s", absfile)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if fileNotExists {
|
log.Printf("Configuration file: %s", absFile)
|
||||||
if file != ".env" {
|
|
||||||
log.Fatalf("Error reading configuration. File `%s` does not exist.", file)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// read file into env values
|
// read file into env values
|
||||||
err = godotenv.Load(absfile)
|
err = godotenv.Load(absFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error parsing configuration file: %s", err)
|
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
|
// 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 {
|
if err != nil {
|
||||||
log.Fatalf("Error parsing configuration from environment: %s", err)
|
log.Fatalf("Error parsing configuration from environment: %s", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue