github.com/wtfutil/wtf@v0.43.0/modules/clocks/settings.go (about) 1 package clocks 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 = false 11 defaultTitle = "Clocks" 12 ) 13 14 // Settings defines the configuration properties for this module 15 type Settings struct { 16 *cfg.Common 17 18 dateFormat string `help:"The format of the date string for all clocks." values:"Any valid Go date layout which is handled by Time.Format. Defaults to Jan 2."` 19 timeFormat string `help:"The format of the time string for all clocks." values:"Any valid Go time layout which is handled by Time.Format. Defaults to 15:04 MST."` 20 locations []Clock `help:"Defines the timezones for the world clocks that you want to display. key is a unique label that will be displayed in the UI. value is a timezone name." values:"Any TZ database timezone."` 21 sort string `help:"Defines the display order of the clocks in the widget." values:"'alphabetical', 'chronological', or 'natural. 'alphabetical' will sort in ascending order by key, 'chronological' will sort in ascending order by date/time, 'natural' will keep ordering as per the config."` 22 } 23 24 // NewSettingsFromYAML creates a new settings instance from a YAML config block 25 func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings { 26 settings := Settings{ 27 Common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig), 28 29 dateFormat: ymlConfig.UString("dateFormat", utils.SimpleDateFormat), 30 timeFormat: ymlConfig.UString("timeFormat", utils.SimpleTimeFormat), 31 locations: buildLocations(ymlConfig), 32 sort: ymlConfig.UString("sort"), 33 } 34 35 return &settings 36 } 37 38 func buildLocations(ymlConfig *config.Config) []Clock { 39 clocks := []Clock{} 40 locations, err := ymlConfig.Map("locations") 41 if err == nil { 42 for k, v := range locations { 43 name := k 44 zone := v.(string) 45 clock, err := BuildClock(name, zone) 46 if err == nil { 47 clocks = append(clocks, clock) 48 } 49 } 50 return clocks 51 } 52 53 listLocations := ymlConfig.UList("locations") 54 for _, location := range listLocations { 55 if location, ok := location.(map[string]interface{}); ok { 56 for k, v := range location { 57 name := k 58 zone := v.(string) 59 clock, err := BuildClock(name, zone) 60 if err == nil { 61 clocks = append(clocks, clock) 62 } 63 } 64 } 65 } 66 return clocks 67 }