From b01a98682ad7881c997fe5fc0770ef984013111f Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Fri, 17 Apr 2020 21:53:15 +0200 Subject: Split up main.go --- internal/config/yaml.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 internal/config/yaml.go (limited to 'internal/config/yaml.go') diff --git a/internal/config/yaml.go b/internal/config/yaml.go new file mode 100644 index 0000000..335dede --- /dev/null +++ b/internal/config/yaml.go @@ -0,0 +1,71 @@ +package config + +import "fmt" + +type Group struct { + Group string + Feeds []configGroupFeed +} + +type configGroupFeed struct { + Target *string + Feed `yaml:",inline"` + Group `yaml:",inline"` +} + +func (grpFeed *configGroupFeed) isGroup() bool { + return grpFeed.Group.Group != "" +} + +func (grpFeed *configGroupFeed) isFeed() bool { + return grpFeed.Name != "" +} + +func (grpFeed *configGroupFeed) target() string { + if grpFeed.Target != nil { + return *grpFeed.Target + } + if grpFeed.Name != "" { + return grpFeed.Name + } + + return grpFeed.Group.Group +} + +func appTarget(target, app string) string { + if target == "" { + return app + } + + if app == "" { + return target + } + + return target + "/" + app +} + +// Parse the group structure and populate the `Target` fields in the feeds +func buildFeeds(cfg []configGroupFeed, target string, feeds Feeds) error { + for _, f := range cfg { + target := appTarget(target, f.target()) + switch { + case f.isFeed() && f.isGroup(): + return fmt.Errorf("Entry with Target %s is both a Feed and a group", target) + + case f.isFeed(): + name := f.Feed.Name + if _, ok := feeds[name]; !ok { + return fmt.Errorf("Duplicate Feed Name '%s'", name) + } + f.Feed.Target = target + feeds[name] = &f.Feed + + case f.isGroup(): + if err := buildFeeds(f.Group.Feeds, target, feeds); err != nil { + return err + } + } + } + + return nil +} -- cgit v1.2.3-54-g00ecf