52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
|
package cfg
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/go-errors/errors"
|
||
|
"github.com/ilyakaznacheev/cleanenv"
|
||
|
|
||
|
"gensokyo.cafe/xmnt/util"
|
||
|
)
|
||
|
|
||
|
type CfgDef struct {
|
||
|
CredentialStore []string `yaml:"credential_store" env-default:"$HOME/.vault2/data_encryption"`
|
||
|
}
|
||
|
|
||
|
func (c *CfgDef) expand() {
|
||
|
for i, path := range c.CredentialStore {
|
||
|
c.CredentialStore[i] = os.ExpandEnv(path)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var Cfg *CfgDef
|
||
|
|
||
|
func LoadAuto() error {
|
||
|
loc, err := os.UserConfigDir()
|
||
|
if err != nil {
|
||
|
return errors.WrapPrefix(err, "cannot obtain user config dir", 0)
|
||
|
}
|
||
|
path := filepath.Join(loc, "xmnt", "xmnt.yml")
|
||
|
|
||
|
loadFromFile, err := util.FileExists(path)
|
||
|
if err != nil {
|
||
|
return errors.WrapPrefix(err, "cannot read config file", 0)
|
||
|
}
|
||
|
|
||
|
cfg := &CfgDef{}
|
||
|
if loadFromFile {
|
||
|
if err = cleanenv.ReadConfig(path, cfg); err != nil {
|
||
|
return errors.WrapPrefix(err, "cannot read config file", 0)
|
||
|
}
|
||
|
} else {
|
||
|
if err = cleanenv.ReadEnv(cfg); err != nil {
|
||
|
return errors.WrapPrefix(err, "cannot read config from env", 0)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cfg.expand()
|
||
|
Cfg = cfg
|
||
|
return nil
|
||
|
}
|