38 lines
560 B
Go
38 lines
560 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
)
|
|
|
|
type config struct {
|
|
DSN string `json:"dsn"`
|
|
Topic string `json:"topic"`
|
|
}
|
|
|
|
func parseEnvConfig() (cfg config) {
|
|
const defaultPath = "./cli.json"
|
|
|
|
data, err := os.ReadFile(defaultPath)
|
|
if err != nil {
|
|
if !os.IsNotExist(err) {
|
|
panic(err.Error())
|
|
}
|
|
} else {
|
|
err := json.Unmarshal(data, &cfg)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
}
|
|
|
|
if dsn := os.Getenv("SMTH_DSN"); dsn != "" {
|
|
cfg.DSN = dsn
|
|
}
|
|
|
|
if topic := os.Getenv("SMTH_TOPIC"); topic != "" {
|
|
cfg.Topic = topic
|
|
}
|
|
|
|
return cfg
|
|
}
|