From af957a67eb34430d5788bf1d2cb8e1aa1e46f3ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Sat, 22 Aug 2020 15:57:19 +0200 Subject: #26: Introduce new option `auto-target` --- pkg/config/config.go | 2 ++ pkg/config/yaml.go | 15 +++++++++------ pkg/config/yaml_test.go | 41 ++++++++++++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index e98a1aa..31012a1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -22,6 +22,7 @@ type GlobalOptions struct { Target Url `yaml:"target"` Parts []string `yaml:"parts"` MaxFailures int `yaml:"max-failures"` + AutoTarget bool `yaml:"auto-target"` } // Default global options @@ -31,6 +32,7 @@ var DefaultGlobalOptions = GlobalOptions{ DefaultEmail: username() + "@" + Hostname(), Target: Url{}, Parts: []string{"text", "html"}, + AutoTarget: true, } // Per feed options diff --git a/pkg/config/yaml.go b/pkg/config/yaml.go index 85b6bd0..9dd74a9 100644 --- a/pkg/config/yaml.go +++ b/pkg/config/yaml.go @@ -50,7 +50,7 @@ func (grpFeed *configGroupFeed) isFeed() bool { return grpFeed.Feed.Name != "" || grpFeed.Feed.Url != "" || len(grpFeed.Feed.Exec) > 0 } -func (grpFeed *configGroupFeed) target() string { +func (grpFeed *configGroupFeed) target(autoTarget bool) string { tag := grpFeed.Target.ShortTag() switch tag { case strTag: @@ -58,7 +58,10 @@ func (grpFeed *configGroupFeed) target() string { case nullTag: return "" case emptyTag: - // tag not set: continue on + if !autoTarget { + return "" + } + // tag not set and autoTarget is on: continue on default: panic("unexpected tag " + tag + " for target node") } @@ -122,7 +125,7 @@ func (cfg *Config) parse(in io.Reader) error { cfg.fixGlobalOptions(parsedCfg.GlobalConfig) - if err := buildFeeds(parsedCfg.Feeds, []string{}, cfg.Feeds, &cfg.FeedOptions); err != nil { + if err := buildFeeds(parsedCfg.Feeds, []string{}, cfg.Feeds, &cfg.FeedOptions, cfg.AutoTarget); err != nil { return fmt.Errorf("while parsing: %w", err) } @@ -185,9 +188,9 @@ func buildOptions(globalFeedOptions *Options, options Map) (feedOptions Options, } // Fetch the group structure and populate the `targetStr` fields in the feeds -func buildFeeds(cfg []configGroupFeed, target []string, feeds Feeds, globalFeedOptions *Options) error { +func buildFeeds(cfg []configGroupFeed, target []string, feeds Feeds, globalFeedOptions *Options, autoTarget bool) error { for _, f := range cfg { - target := appTarget(target, f.target()) + target := appTarget(target, f.target(autoTarget)) switch { case f.isFeed() && f.isGroup(): return fmt.Errorf("Entry with targetStr %s is both a Feed and a group", target) @@ -224,7 +227,7 @@ func buildFeeds(cfg []configGroupFeed, target []string, feeds Feeds, globalFeedO log.Warnf("Unknown option '%s' for group '%s'. Ignored!", optName, f.Group.Group) } - if err := buildFeeds(f.Group.Feeds, target, feeds, &opt); err != nil { + if err := buildFeeds(f.Group.Feeds, target, feeds, &opt, autoTarget); err != nil { return err } } diff --git a/pkg/config/yaml_test.go b/pkg/config/yaml_test.go index fdc5e80..09f721d 100644 --- a/pkg/config/yaml_test.go +++ b/pkg/config/yaml_test.go @@ -61,11 +61,12 @@ func TestBuildOptions(tst *testing.T) { func TestBuildFeeds(tst *testing.T) { tests := []struct { - name string - wantErr bool - target string - feeds []configGroupFeed - result Feeds + name string + wantErr bool + target string + feeds []configGroupFeed + result Feeds + noAutoTarget bool }{ {name: "Empty input", wantErr: false, target: "", feeds: nil, result: Feeds{}}, {name: "Empty Feed", wantErr: true, target: "", @@ -93,12 +94,24 @@ func TestBuildFeeds(tst *testing.T) { }, result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep.foo")}}, }, + {name: "Simple With Target and NoAutoTarget", wantErr: false, target: "moep", noAutoTarget: true, + feeds: []configGroupFeed{ + {Target: n("foo"), Feed: feed{Name: "muh"}}, + }, + result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep.foo")}}, + }, {name: "Simple Without Target", wantErr: false, target: "moep", feeds: []configGroupFeed{ {Feed: feed{Name: "muh"}}, }, result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep.muh")}}, }, + {name: "Simple Without Target and NoAutoTarget", wantErr: false, target: "moep", noAutoTarget: true, + feeds: []configGroupFeed{ + {Feed: feed{Name: "muh"}}, + }, + result: Feeds{"muh": &Feed{Name: "muh", Target: t("moep")}}, + }, {name: "Simple With Nil Target", wantErr: false, target: "moep", feeds: []configGroupFeed{ {Target: yaml.Node{Tag: "!!null"}, Feed: feed{Name: "muh"}}, @@ -141,6 +154,20 @@ func TestBuildFeeds(tst *testing.T) { "F3": &Feed{Name: "F3", Target: t("G1.F3")}, }, }, + {name: "Simple Group, NoAutoTarget", wantErr: false, target: "IN", noAutoTarget: true, + feeds: []configGroupFeed{ + {Group: group{Group: "G1", Feeds: []configGroupFeed{ + {Target: n("bar"), Feed: feed{Name: "F1"}}, + {Target: n(""), Feed: feed{Name: "F2"}}, + {Feed: feed{Name: "F3"}}, + }}}, + }, + result: Feeds{ + "F1": &Feed{Name: "F1", Target: t("IN.bar")}, + "F2": &Feed{Name: "F2", Target: t("IN")}, + "F3": &Feed{Name: "F3", Target: t("IN")}, + }, + }, {name: "Nested Groups", wantErr: false, target: "", feeds: []configGroupFeed{ {Group: group{Group: "G1", Feeds: []configGroupFeed{ @@ -165,7 +192,7 @@ func TestBuildFeeds(tst *testing.T) { tst.Run(tt.name, func(tst *testing.T) { var feeds = Feeds{} var opts = Options{} - err := buildFeeds(tt.feeds, t(tt.target), feeds, &opts) + err := buildFeeds(tt.feeds, t(tt.target), feeds, &opts, !tt.noAutoTarget) if (err != nil) != tt.wantErr { tst.Errorf("buildFeeds() error = %v, wantErr %v", err, tt.wantErr) return @@ -248,7 +275,7 @@ feeds: Target: n("bar"), Feed: feed{ Name: "Foo", - Exec: []string{"whatever", "-i", "http://foo.bar"}, + Exec: []string{"whatever", "-i", "http://foo.bar"}, }, Options: Map{"include-images": true, "unknown-option": "foo"}, }}, nil)}, -- cgit v1.2.3