Parse config overrides on cli

This commit is contained in:
Dan Jones 2024-02-09 09:44:35 -06:00
commit 25f5c37243
8 changed files with 169 additions and 32 deletions

View file

@ -1,13 +1,18 @@
package config
import (
"encoding/json"
"fmt"
"os"
fp "path/filepath"
"time"
"codeberg.org/danjones000/my-log/tools"
"github.com/BurntSushi/toml"
)
var ConfigPath string
var Overrides map[string]string
func init() {
conf, _ := os.UserConfigDir()
@ -17,10 +22,32 @@ func init() {
func Load() (Config, error) {
c, _ := DefaultConfig()
_, err := os.Stat(ConfigPath)
if os.IsNotExist(err) {
return c, nil
if !os.IsNotExist(err) {
_, err = toml.DecodeFile(ConfigPath, &c)
if err != nil {
return c, err
}
}
_, err = toml.DecodeFile(ConfigPath, &c)
// @todo get environ
l := ""
for k, v := range Overrides {
val := tools.ParseString(v)
if val == nil {
continue
}
if _, isJson := val.(json.RawMessage); isJson {
continue
}
valout := fmt.Sprintf("%v", val)
if vals, isString := val.(string); isString {
valout = fmt.Sprintf(`"%s"`, vals)
}
if valt, isTime := val.(time.Time); isTime {
valout = valt.Format(time.RFC3339)
}
l = l + "\n" + fmt.Sprintf("%s = %s", k, valout)
}
_, err = toml.Decode(l, &c)
return c, err
}

View file

@ -1,8 +1,10 @@
package config
import mapst "github.com/mitchellh/mapstructure"
type Config struct {
Input Input
Outputs map[string]Output `toml:"output"`
Outputs Outputs `toml:"output"`
}
type Input struct {
@ -11,7 +13,23 @@ type Input struct {
Ext string
}
type Outputs map[string]Output
type Output struct {
Enabled bool
Config map[string]any
}
func (oo Outputs) Stdout() (s Stdout, enabled bool) {
o, ok := oo["stdout"]
if !ok {
return s, false
}
enabled = o.Enabled
mapst.Decode(o.Config, &s)
return
}
type Stdout struct {
Json bool
}