From 3bcd6664a664b2555c67937d57fb64c48c739dd0 Mon Sep 17 00:00:00 2001 From: shoopea Date: Sun, 17 Nov 2024 16:25:39 +0100 Subject: [PATCH] add saving cfg --- admin.go | 12 ++++++++++++ config.go | 32 ++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ version.go | 8 ++++---- 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/admin.go b/admin.go index 33eeb1d..4812da4 100644 --- a/admin.go +++ b/admin.go @@ -113,6 +113,18 @@ func (a *AdminConfig) Run() { }) }) + r.GET("/save", func(c *gin.Context) { + if err := cfg.Save(); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "message": "done", + }) + } + }) + fsys, _ := fs.Sub(assets, "assets/static") r.StaticFS("/assets", http.FS(fsys)) diff --git a/config.go b/config.go index 3be7786..e44f282 100644 --- a/config.go +++ b/config.go @@ -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") diff --git a/go.mod b/go.mod index 6d0ac9e..a79fc92 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/silenceper/pool v1.0.0 github.com/sirupsen/logrus v1.9.3 github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a + github.com/tidwall/pretty v1.2.1 golang.org/x/crypto v0.9.0 ) diff --git a/go.sum b/go.sum index 61708e1..b926397 100644 --- a/go.sum +++ b/go.sum @@ -71,6 +71,8 @@ github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gt github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a h1:SJy1Pu0eH1C29XwJucQo73FrleVK6t4kYz4NVhp34Yw= github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= diff --git a/version.go b/version.go index 4e41053..82e1406 100644 --- a/version.go +++ b/version.go @@ -1,7 +1,7 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "26c324c" +var githash = "e1806fd" var branch = "master" -var buildstamp = "2024-11-17_14:13:25" -var commits = "88" -var version = "26c324c-b88 - 2024-11-17_14:13:25" +var buildstamp = "2024-11-17_15:23:14" +var commits = "89" +var version = "e1806fd-b89 - 2024-11-17_15:23:14"