diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-20 01:01:13 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-20 01:01:13 +0200 |
commit | 161dd4405474cc905e6912bab243509a3a6f88db (patch) | |
tree | 6648d425f4ab64325cb6419e619a88d9af61e5ec /internal | |
parent | 431a8ddb0c18b0781cba1d01eda3645b361f1b94 (diff) | |
download | feed2imap-go-161dd4405474cc905e6912bab243509a3a6f88db.tar.gz feed2imap-go-161dd4405474cc905e6912bab243509a3a6f88db.tar.bz2 feed2imap-go-161dd4405474cc905e6912bab243509a3a6f88db.zip |
GlobalOptions
Diffstat (limited to 'internal')
-rw-r--r-- | internal/config/config.go | 42 | ||||
-rw-r--r-- | internal/yaml/yaml.go | 10 | ||||
-rw-r--r-- | internal/yaml/yaml_test.go | 52 |
3 files changed, 79 insertions, 25 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 0cebf34..6497ce1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,8 +1,28 @@ package config +import ( + "os" + "os/user" + "runtime" + "strings" +) + type Map map[string]interface{} +type GlobalOptions struct { + Timeout int `yaml:"timeout"` + DefaultEmail string `yaml:"default-email"` + Target string `yaml:"target"` +} + +var DefaultGlobalOptions = GlobalOptions{ + Timeout: 30, + DefaultEmail: username() + "@" + hostname(), + Target: "", +} + type Config struct { + GlobalOptions GlobalConfig Map } @@ -10,3 +30,25 @@ type Options struct { MinFreq int `yaml:"min-frequency"` InclImages *bool `yaml:"include-images"` } + +func hostname() (hostname string) { + hostname, err := os.Hostname() + if err != nil { + hostname = "localhost" + } + return +} + +func username() string { + u, err := user.Current() + switch { + case err != nil: + return "user" + case runtime.GOOS == "windows": + // the domain is attached -- remove it again + split := strings.Split(u.Username, "\\") + return split[len(split)-1] + default: + return u.Username + } +} diff --git a/internal/yaml/yaml.go b/internal/yaml/yaml.go index 8cbace7..3317f5d 100644 --- a/internal/yaml/yaml.go +++ b/internal/yaml/yaml.go @@ -11,8 +11,9 @@ import ( ) type config struct { - GlobalConfig C.Map `yaml:",inline"` - Feeds []configGroupFeed + C.GlobalOptions `yaml:",inline"` + GlobalConfig C.Map `yaml:",inline"` + Feeds []configGroupFeed } type group struct { @@ -52,9 +53,10 @@ func (grpFeed *configGroupFeed) target() string { } func parse(buf []byte) (config, error) { - var parsedCfg config + parsedCfg := config{GlobalOptions: C.DefaultGlobalOptions} + if err := yaml.Unmarshal(buf, &parsedCfg); err != nil { - return parsedCfg, fmt.Errorf("while unmarshalling: %w", err) + return config{}, fmt.Errorf("while unmarshalling: %w", err) } //fmt.Printf("--- parsedCfg:\n%+v\n\n", parsedCfg) diff --git a/internal/yaml/yaml_test.go b/internal/yaml/yaml_test.go index 0e9ef08..a71b95b 100644 --- a/internal/yaml/yaml_test.go +++ b/internal/yaml/yaml_test.go @@ -129,20 +129,34 @@ func TestBuildFeeds(tst *testing.T) { } } +func defaultConfig(feeds []configGroupFeed, global C.Map) config { + return config{ + GlobalOptions: C.DefaultGlobalOptions, + GlobalConfig: global, + Feeds: feeds, + } +} + //noinspection GoNilness,GoNilness func TestParse(tst *testing.T) { tests := []struct { - name string - inp string - wantErr bool - feeds []configGroupFeed - globalConfig C.Map + name string + inp string + wantErr bool + config config }{ {name: "Empty", - inp: "", wantErr: false, feeds: nil, globalConfig: nil}, + inp: "", wantErr: false, config: defaultConfig(nil, nil)}, {name: "Trash", inp: "Something", wantErr: true}, {name: "Simple config", - inp: "something: 1\nsomething_else: 2", wantErr: false, feeds: nil, globalConfig: C.Map{"something": 1, "something_else": 2}}, + inp: "something: 1\nsomething_else: 2", wantErr: false, config: defaultConfig(nil, C.Map{"something": 1, "something_else": 2})}, + {name: "Known config", + inp: "whatever: 2\ndefault-email: foo@foobar.de\ntimeout: 60\nsomething: 1", wantErr: false, config: func() config { + c := defaultConfig(nil, C.Map{"something": 1, "whatever": 2}) + c.Timeout = 60 + c.DefaultEmail = "foo@foobar.de" + return c + }()}, {name: "Config with feed", inp: ` something: 1 @@ -153,9 +167,8 @@ feeds: include-images: true unknown-option: foo `, - wantErr: false, - globalConfig: C.Map{"something": 1}, - feeds: []configGroupFeed{ + wantErr: false, + config: defaultConfig([]configGroupFeed{ {Target: s("bar"), Feed: feed{ Name: "Foo", Url: "whatever", @@ -163,7 +176,7 @@ feeds: MinFreq: 0, InclImages: b(true), }, - }}}}, + }}}, C.Map{"something": 1})}, {name: "Feeds", inp: ` @@ -177,7 +190,7 @@ feeds: include-images: false `, wantErr: false, - feeds: []configGroupFeed{ + config: defaultConfig([]configGroupFeed{ {Target: nil, Feed: feed{ Name: "Foo", Url: "whatever", @@ -194,7 +207,7 @@ feeds: InclImages: b(false), }, }}, - }, + }, nil), }, {name: "Empty Group", inp: ` @@ -203,7 +216,7 @@ feeds: target: bla `, wantErr: false, - feeds: []configGroupFeed{{Target: s("bla"), Group: group{"Foo", nil}}}, + config: defaultConfig([]configGroupFeed{{Target: s("bla"), Group: group{"Foo", nil}}}, nil), }, {name: "Feeds and Groups", inp: ` @@ -222,7 +235,7 @@ feeds: - group: G3 `, wantErr: false, - feeds: []configGroupFeed{ + config: defaultConfig([]configGroupFeed{ {Target: nil, Feed: feed{ Name: "Foo", Url: "whatever", @@ -240,7 +253,7 @@ feeds: {Target: nil, Group: group{Group: "G3"}}, }}, }, - }, + }, nil), }, } @@ -252,11 +265,8 @@ feeds: tst.Errorf("parse() error = %v, wantErr %v", err, tt.wantErr) return } - if !reflect.DeepEqual(got.Feeds, tt.feeds) { - tst.Errorf("parse() got = %v, want %v", got.Feeds, tt.feeds) - } - if !reflect.DeepEqual(got.GlobalConfig, tt.globalConfig) { - tst.Errorf("parse() got = %v, want %v", got.GlobalConfig, tt.globalConfig) + if err == nil && !reflect.DeepEqual(got, tt.config) { + tst.Errorf("parse() got = %#v, want %#v", got, tt.config) } }) } |