summaryrefslogtreecommitdiff
path: root/config.go
blob: 94642911333068bc58d41a3e1e6a85fffac648f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main

import (
	"encoding/json"
	"os"

	"github.com/adrg/xdg"
)

// 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
	}
}