aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 1402c177cfbd2df0666303deb82c2c3e7b46ed28 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main

import (
	"errors"
	"io/ioutil"
	"log"
	"strings"

	"github.com/BurntSushi/xdg"
	"gopkg.in/yaml.v2"
)

const XDG_CONFIG_NAME = "phlux"

type BridgeConfig struct {
	BridgeID string `yaml:"id"`
	Token    string `yaml:"token"`
}

type PhluxConfig struct {
	Latitude  float64        `yaml:"latitude"`
	Longitude float64        `yaml:"longitude"`
	Bridges   []BridgeConfig `yaml:"bridges"`
}

func (c *PhluxConfig) Read() {
	var paths xdg.Paths
	configFile, err := paths.ConfigFile(XDG_CONFIG_NAME)
	if err != nil {
		log.Printf("No config file found: %s\n", err.Error())
		return
	}
	yamlFile, err := ioutil.ReadFile(configFile)
	if err != nil {
		log.Printf("Error reading config file %s: %s\n", configFile, err.Error())
		return
	}
	err = yaml.Unmarshal(yamlFile, c)
	if err != nil {
		log.Printf("Error unmarshalling yaml: %s\n", err.Error())
		return
	}
}

func (c *PhluxConfig) GetBridgeToken(id string) (string, error) {
	lc := strings.ToLower(id)
	for _, bridge := range c.Bridges {
		if strings.ToLower(bridge.BridgeID) == lc {
			return bridge.Token, nil
		}
	}
	return "", errors.New("No token found for bridge " + id)
}

func (c *PhluxConfig) SetBridgeToken(id, token string) {
	for _, bridge := range c.Bridges {
		if bridge.BridgeID == id {
			bridge.Token = token
			return
		}
	}
	c.Bridges = append(c.Bridges, BridgeConfig{
		BridgeID: id,
		Token:    token,
	})
}

func (c *PhluxConfig) Save() (err error) {
	out, err := yaml.Marshal(c)
	if err != nil {
		log.Printf("Error marshalling config: %s\n", err.Error())
	}
	var paths xdg.Paths
	configFile, err := paths.ConfigFile(XDG_CONFIG_NAME)
	if err != nil {
		log.Printf("No config file found: %s\n", err.Error())
		return
	}
	err = ioutil.WriteFile(configFile, out, 0600)
	if err != nil {
		log.Printf("Error writing config file %s: %s\n", configFile, err.Error())
		return
	}
	return
}