implement
This commit is contained in:
37
cmd/simple/config.go
Normal file
37
cmd/simple/config.go
Normal file
@ -0,0 +1,37 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user