diff --git a/config/main.go b/config/main.go new file mode 100644 index 0000000000000000000000000000000000000000..bd3050ef659e66b6c16efe8d474abdbef8bff9e8 --- /dev/null +++ b/config/main.go @@ -0,0 +1,118 @@ +package main + +import ( + "fmt" + "os" + + "gopkg.in/yaml.v3" +) + +type ParamsInterface interface { + Name() string + OnClick() string +} + +type Module struct { + Module string `yaml:"module"` + Name string `yaml:"name"` + OnClick string `yaml:"on-click"` + Params ParamsInterface `yaml:"-"` +} + +type M Module + +type Params struct { + *M `yaml:",inline"` + Params yaml.Node `yaml:"params"` +} + +type DateMod struct { + name string + onclick string + Format string `yaml:"format"` +} + +type TextMod struct { + name string + onclick string + //Name string `yaml:"-"` + //OnClick string `yaml:"-"` + Text string `yaml:"text"` + Color string `yaml:"color"` +} + +func NewDate(m *Module) *DateMod { + d := &DateMod{ + name: m.Name, + onclick: m.OnClick, + } + if d.name == "" { + d.name = "date" + } + return d +} + +func (d *DateMod) Name() string { + return d.name +} + +func (d *DateMod) OnClick() string { + return d.onclick +} + +func NewText(m *Module) *TextMod { + t := &TextMod{ + name: m.Name, + onclick: m.OnClick, + } + if t.name == "" { + t.name = "text" + } + return t +} + +func (t *TextMod) Name() string { + return t.name +} + +func (t *TextMod) OnClick() string { + return t.onclick +} + +type Modules struct { + Modules []Module `yaml:"modules"` +} + +func (m *Module) UnmarshalYAML(node *yaml.Node) error { + params := &Params{M: (*M)(m)} + if err := node.Decode(params); err != nil { + return err + } + + switch params.Module { + case "date": + m.Params = NewDate(m) + case "text": + m.Params = NewText(m) + default: + panic("module unknown") + } + return params.Params.Decode(m.Params) +} + +func main() { + config, err := os.Open(os.Args[1]) + if err != nil { + panic(err) + } + defer config.Close() + configParser := yaml.NewDecoder(config) + cfg := &Modules{} + err = configParser.Decode(&cfg) + if err != nil { + panic(err) + } + for _, m := range cfg.Modules { + fmt.Printf("%s:%s:%s\n", m.Module, m.Name, m.Params.Name()) + } +} diff --git a/doc/config.sample.yaml b/doc/config.sample.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b03bd3b17ac6d7142c7ed4539c41d25de264a7cb --- /dev/null +++ b/doc/config.sample.yaml @@ -0,0 +1,12 @@ +--- +modules: + - module: text + name: post + params: + text: "post" + color: "#11ff11" + on-click: xdg-open https://mastodon.ie/ + - module: date + params: + format: 06-05-04 15:02 + on-click: xdg-open https://calendar.google.com/