add saving cfg

This commit is contained in:
shoopea
2024-11-17 16:25:39 +01:00
parent e1806fd27a
commit 3bcd6664a6
5 changed files with 51 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/tailscale/hujson"
"github.com/tidwall/pretty"
)
type Config struct {
@@ -180,6 +181,37 @@ func LoadConfigByte(conf []byte) (*Config, error) {
return c, nil
}
// Save config
func (c *Config) Save() error {
log.WithFields(log.Fields{}).Debugf("starting")
defer log.WithFields(log.Fields{}).Debugf("done")
cfgMx.Lock()
defer cfgMx.Unlock()
b, err := json.Marshal(cfg)
if err != nil {
log.WithFields(log.Fields{"error": err}).Errorf("")
return err
}
r := pretty.Pretty(b)
f, err := os.Open(*cfgFile)
if err != nil {
log.WithFields(log.Fields{"error": err}).Errorf("")
return err
}
if _, err := f.Write(r); err != nil {
log.WithFields(log.Fields{"error": err}).Errorf("")
return err
}
return nil
}
// Run config
func (c *Config) Run() {
log.WithFields(log.Fields{}).Debugf("starting")