Files
Nip-05-api/internal/messages/messages.go
2026-04-29 02:35:00 +00:00

53 lines
1.0 KiB
Go

package messages
import (
"errors"
"fmt"
"log/slog"
"os"
"gopkg.in/yaml.v3"
)
var ErrUnknownEvent = errors.New("unknown event type")
type Templates struct {
templates map[string]string
}
func Load(path string) (*Templates, error) {
t := &Templates{templates: defaultTemplates()}
b, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
slog.Warn("messages file not found, using embedded defaults", "path", path)
return t, nil
}
return nil, fmt.Errorf("read messages file: %w", err)
}
parsed := map[string]string{}
if err := yaml.Unmarshal(b, &parsed); err != nil {
return nil, fmt.Errorf("parse messages file: %w", err)
}
for k, v := range parsed {
t.templates[k] = v
}
return t, nil
}
func (t *Templates) Get(eventType string) (string, error) {
tmpl, ok := t.templates[eventType]
if !ok {
return "", fmt.Errorf("%w: %s", ErrUnknownEvent, eventType)
}
return tmpl, nil
}
func (t *Templates) Has(eventType string) bool {
_, ok := t.templates[eventType]
return ok
}