Skip to content
Snippets Groups Projects
Unverified Commit 7496e53a authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Initial pass as parsing config.

parent be1365a0
Branches
No related tags found
No related merge requests found
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())
}
}
---
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/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment