From 289d877e414336e3dcf2f44f84ea9dae073a0372 Mon Sep 17 00:00:00 2001 From: Bastien Riviere Date: Fri, 1 Sep 2023 17:53:47 +0200 Subject: [PATCH] feat: add config format --- config.example.scfg | 19 +++++ config/config.go | 173 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 + go.sum | 3 + 4 files changed, 197 insertions(+) create mode 100644 config.example.scfg create mode 100644 config/config.go create mode 100644 go.sum diff --git a/config.example.scfg b/config.example.scfg new file mode 100644 index 0000000..cae9030 --- /dev/null +++ b/config.example.scfg @@ -0,0 +1,19 @@ +# listening address for the http server +http-address 0.0.0.0:8080 +log-level info +# text or json +log-format text + +ntfy { + server "https://ntfy.sh" +} + +handler "/flux" { + type "flux" + topic "/flux" +} + +# handler "/alertmanager" { +# type "alertmanager" +# topic "/infra" +# } diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..45a35f2 --- /dev/null +++ b/config/config.go @@ -0,0 +1,173 @@ +package config + +import ( + "errors" + "fmt" + + "git.sr.ht/~emersion/go-scfg" +) + +type HandlerType int + +const ( + HandlerNone HandlerType = iota + HandlerFlux +) + +type LogFormat int + +const ( + LogFormatText LogFormat = iota + LogFormatJson +) + +type Config struct { + HTTPAddress string + LogLevel string + LogFormat LogFormat + + Ntfy ntfy + Handlers map[string]handler +} + +type ntfy struct { + Server string + DefaultTopic string +} + +func (n *ntfy) readConfig(cfg scfg.Block) error { + if err := readString(cfg, "server", &n.Server); err != nil { + return err + } + + return readString(cfg, "default-topic", &n.DefaultTopic) +} + +type handler struct { + Type HandlerType + Topic string +} + +func (h *handler) readConfig(cfg scfg.Block) error { + var err error + if h.Type, err = readHandlerType(cfg.Get("type")); err != nil { + return err + } + + return readStringRequired(cfg, "topic", &h.Topic) +} + +func ReadConfig(path string) (Config, error) { + cfg, err := scfg.Load(path) + if err != nil { + return Config{}, err + } + + config := Config{ + HTTPAddress: "127.0.0.1:8080", + LogLevel: "info", + LogFormat: LogFormatText, + + Ntfy: ntfy{ + Server: "https://ntfy.sh", + }, + Handlers: make(map[string]handler), + } + + if err := readString(cfg, "http-address", &config.HTTPAddress); err != nil { + return config, err + } + + if err := readString(cfg, "log-level", &config.LogLevel); err != nil { + return config, err + } + + if err := readLogFormat(cfg, "log-format", &config.LogFormat); err != nil { + return config, err + } + + d := cfg.Get("ntfy") + if d != nil { + if err := config.Ntfy.readConfig(d.Children); err != nil { + return config, err + } + } + + ds := cfg.GetAll("handler") + for _, d := range ds { + var key string + if err := d.ParseParams(&key); err != nil { + return config, err + } + + var h handler + if err := h.readConfig(d.Children); err != nil { + return config, err + } + + config.Handlers[key] = h + } + + for _, block := range cfg { + fmt.Printf("%+v\n", block) + + } + return config, nil +} + +func readHandlerType(d *scfg.Directive) (HandlerType, error) { + if d == nil { + return HandlerNone, errors.New("handler.type is missing") + } + + var ty string + + if err := d.ParseParams(&ty); err != nil { + return HandlerNone, err + } + + switch ty { + case "flux": + return HandlerFlux, nil + default: + return HandlerNone, fmt.Errorf("invalid handler type %q", ty) + } +} + +func readLogFormat(cfg scfg.Block, key string, val *LogFormat) error { + d := cfg.Get(key) + if d == nil { + return nil + } + + var format string + if err := d.ParseParams(&format); err != nil { + return err + } + + switch format { + case "text": + *val = LogFormatText + case "json": + *val = LogFormatJson + default: + return fmt.Errorf("invalid log format %q", format) + } + return nil +} + +func readString(cfg scfg.Block, key string, val *string) error { + d := cfg.Get(key) + if d != nil { + return d.ParseParams(val) + } + return nil +} + +func readStringRequired(cfg scfg.Block, key string, val *string) error { + d := cfg.Get(key) + if d != nil { + return d.ParseParams(val) + } + return fmt.Errorf("missing key %q", key) +} diff --git a/go.mod b/go.mod index d1cdf88..9fdda8d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module forge.babariviere.com/babariviere/ntfy-bridge go 1.20 + +require git.sr.ht/~emersion/go-scfg v0.0.0-20230828131541-76adf4aeafd7 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..db251b4 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +git.sr.ht/~emersion/go-scfg v0.0.0-20230828131541-76adf4aeafd7 h1:Cxfw56Srn4yJ2qPUuUvG9qQoQl45U7SsCjSmeTN63Xw= +git.sr.ht/~emersion/go-scfg v0.0.0-20230828131541-76adf4aeafd7/go.mod h1:ybgvEJTIx5XbaspSviB3KNa6OdPmAZqDoSud7z8fFlw= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=