github.com/wtfutil/wtf@v0.43.0/modules/feedreader/settings.go (about) 1 package feedreader 2 3 import ( 4 "github.com/olebedev/config" 5 "github.com/wtfutil/wtf/cfg" 6 "github.com/wtfutil/wtf/utils" 7 ) 8 9 const ( 10 defaultFocusable = true 11 defaultTitle = "Feed Reader" 12 ) 13 14 type colors struct { 15 source string `help:"Color to use for feed source titles." optional:"true" default:"green"` 16 publishDate string `help:"Color to use for publish dates." optional:"true" default:"orange"` 17 } 18 19 // auth stores [username, password]-credentials for private RSS feeds using Basic Auth 20 type auth struct { 21 username string 22 password string 23 } 24 25 // Settings defines the configuration properties for this module 26 type Settings struct { 27 *cfg.Common 28 29 colors 30 31 feeds []string `help:"An array of RSS and Atom feed URLs"` 32 feedLimit int `help:"The maximum number of stories to display for each feed"` 33 showSource bool `help:"Wether or not to show feed source in front of item titles." values:"true or false" optional:"true" default:"true"` 34 showPublishDate bool `help:"Wether or not to show publish date in front of item titles." values:"true or false" optional:"true" default:"false"` 35 dateFormat string `help:"Date format to use for publish dates" values:"Any valid Go time layout which is handled by Time.Format" optional:"true" default:"Jan 02"` 36 credentials map[string]auth `help:"Map of private feed URLs with required authentication credentials"` 37 disableHTTP2 bool `help:"Wether or not to use the HTTP/2 protocol. Certain sites, such as reddit.com, will not work unless HTTP/2 is disabled." values:"true or false" optional:"true" default:"false"` 38 userAgent string `help:"HTTP User-Agent to use when fetching RSS feeds." optional:"true"` 39 } 40 41 // NewSettingsFromYAML creates a new settings instance from a YAML config block 42 func NewSettingsFromYAML(name string, ymlConfig, globalConfig *config.Config) *Settings { 43 settings := &Settings{ 44 Common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig), 45 feeds: utils.ToStrs(ymlConfig.UList("feeds")), 46 feedLimit: ymlConfig.UInt("feedLimit", -1), 47 showSource: ymlConfig.UBool("showSource", true), 48 showPublishDate: ymlConfig.UBool("showPublishDate", false), 49 dateFormat: ymlConfig.UString("dateFormat", "Jan 02"), 50 credentials: make(map[string]auth), 51 disableHTTP2: ymlConfig.UBool("disableHTTP2", false), 52 userAgent: ymlConfig.UString("userAgent", "wtfutil (https://github.com/wtfutil/wtf)"), 53 } 54 55 settings.colors.source = ymlConfig.UString("colors.source", "green") 56 settings.colors.publishDate = ymlConfig.UString("colors.publishDate", "orange") 57 58 // If feeds cannot be parsed as list try parsing as a map with username+password fields 59 if len(settings.feeds) == 0 { 60 credentials := make(map[string]auth) 61 feeds := make([]string, 0) 62 for url, creds := range ymlConfig.UMap("feeds") { 63 parsed, ok := creds.(map[string]interface{}) 64 if !ok { 65 continue 66 } 67 68 user, ok := parsed["username"].(string) 69 if !ok { 70 continue 71 } 72 pass, ok := parsed["password"].(string) 73 if !ok { 74 continue 75 } 76 77 credentials[url] = auth{ 78 username: user, 79 password: pass, 80 } 81 feeds = append(feeds, url) 82 } 83 settings.feeds = feeds 84 settings.credentials = credentials 85 } 86 87 return settings 88 }