diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2020-10-04 22:58:03 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2020-10-04 22:58:03 +0200 |
commit | 5b456afb49bfa6ff1567510bc1d9362377d32216 (patch) | |
tree | e9f13973251405095dd8b0a31930ca10caa57163 /config.go | |
parent | 1f700deeb1a26ab289178b76518a33faa3f51545 (diff) | |
download | startpage-5b456afb49bfa6ff1567510bc1d9362377d32216.tar.gz startpage-5b456afb49bfa6ff1567510bc1d9362377d32216.tar.bz2 startpage-5b456afb49bfa6ff1567510bc1d9362377d32216.zip |
New config format and new features
We're now using json as a config format instead of our weird own format.
The weather icon is now optional, simply don't define WeatherPlace in your
config.
You can now specify which subreddit to get background images from. The
default is still /r/EarthPorn.
Diffstat (limited to 'config.go')
-rw-r--r-- | config.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/config.go b/config.go new file mode 100644 index 0000000..c32364e --- /dev/null +++ b/config.go @@ -0,0 +1,63 @@ +package main + +import ( + "encoding/json" + "github.com/adrg/xdg" + "os" +) + +// Config contains all configuration options that are read from .config/startpage/config.json +type Config struct { + // The place for which to get the weather data. If omitted, no weather will be shown + WeatherPlace string + + // A list of links to show + Links []Link + + // If set, background images can be saved here + BackgroundSavepath string + + // If set, this limits the background image size, the default is DEFAULT_BACKGROUND_MAXDIM + BackgroundMaxdim *int + + // Get background images from this subreddit. Defaults to "EarthPorn" + ImageSubreddit string +} + +func LoadConfig() (*Config, error) { + path, err := xdg.ConfigFile("startpage/config.json") + if err != nil { + return nil, err + } + + file, err := os.Open(path) + switch { + case err == nil: + // All OK, we can continue + case os.IsNotExist(err): + return &Config{}, nil + default: + return nil, err + } + + defer file.Close() + + decoder := json.NewDecoder(file) + var config Config + err = decoder.Decode(&config) + if err == nil { + return &config, nil + } else { + return nil, err + } +} + +const DEFAULT_BACKGROUND_MAXDIM = 2500 + +func (c Config) GetBackgroundMaxdim() int { + if c.BackgroundMaxdim == nil { + return DEFAULT_BACKGROUND_MAXDIM + } else { + return *c.BackgroundMaxdim + } +} |