diff --git a/config.example.scfg b/config.example.scfg index cae9030..1ccec26 100644 --- a/config.example.scfg +++ b/config.example.scfg @@ -6,6 +6,12 @@ log-format text ntfy { server "https://ntfy.sh" + # optional username + password + # username test + # password test + + # or access token + access-token "..." } handler "/flux" { diff --git a/config/config.go b/config/config.go index 45a35f2..2b832f4 100644 --- a/config/config.go +++ b/config/config.go @@ -10,10 +10,17 @@ import ( type HandlerType int const ( - HandlerNone HandlerType = iota - HandlerFlux + HandlerFlux HandlerType = iota + 1 ) +func (h HandlerType) String() string { + switch h { + case HandlerFlux: + return "flux" + } + panic("unreachable") +} + type LogFormat int const ( @@ -33,6 +40,9 @@ type Config struct { type ntfy struct { Server string DefaultTopic string + AccessToken string + Username string + Password string } func (n *ntfy) readConfig(cfg scfg.Block) error { @@ -40,7 +50,20 @@ func (n *ntfy) readConfig(cfg scfg.Block) error { return err } - return readString(cfg, "default-topic", &n.DefaultTopic) + if err := readString(cfg, "default-topic", &n.DefaultTopic); err != nil { + return err + } + + // TODO: handle env var in config + if err := readString(cfg, "access-token", &n.AccessToken); err != nil { + return err + } + + if err := readString(cfg, "username", &n.Username); err != nil { + return err + } + + return readString(cfg, "password", &n.Password) } type handler struct { @@ -108,29 +131,25 @@ func ReadConfig(path string) (Config, error) { 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") + return 0, errors.New("handler.type is missing") } var ty string if err := d.ParseParams(&ty); err != nil { - return HandlerNone, err + return 0, err } switch ty { case "flux": return HandlerFlux, nil default: - return HandlerNone, fmt.Errorf("invalid handler type %q", ty) + return 0, fmt.Errorf("invalid handler type %q", ty) } }