feat: xmnt

This commit is contained in:
Yiyang Kang 2022-10-07 01:19:04 +08:00
parent 00b3e4b24f
commit 5a998b713c
16 changed files with 1481 additions and 0 deletions

51
cfg/cfg.go Normal file
View file

@ -0,0 +1,51 @@
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
}