Skip to content

Commit

Permalink
Configuration support for both JSON and YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
prfalken authored and stefanwichmann committed Apr 13, 2019
1 parent 4bf0952 commit 7503100
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ _testmain.go

# Kelvin files
dist
kelvin
config.json
config.yaml

# macOS
.DS_Store
.vscode
40 changes: 30 additions & 10 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
// SOFTWARE.
package main

import "io/ioutil"
import "encoding/json"
import "os"
import "errors"
import "time"
import "fmt"
import "crypto/sha256"
import log "github.com/Sirupsen/logrus"
import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"time"

log "github.com/Sirupsen/logrus"
"github.com/ghodss/yaml"
)

// Bridge respresents the hue bridge in your system.
type Bridge struct {
Expand Down Expand Up @@ -166,12 +170,20 @@ func (configuration *Configuration) Write() error {
return nil
}
log.Debugf("⚙ Configuration changed. Saving to %v", configuration.ConfigurationFile)
json, err := json.MarshalIndent(configuration, "", " ")
raw, err := json.MarshalIndent(configuration, "", " ")
if err != nil {
return err
}

err = ioutil.WriteFile(configuration.ConfigurationFile, json, 0644)
// Convert JSON to YAML if needed
if isYAMLFile(configuration.ConfigurationFile) {
raw, err = yaml.JSONToYAML(raw)
if err != nil {
return err
}
}

err = ioutil.WriteFile(configuration.ConfigurationFile, raw, 0644)
if err != nil {
return err
}
Expand All @@ -192,6 +204,14 @@ func (configuration *Configuration) Read() error {
return err
}

// Convert YAML to JSON if needed
if isYAMLFile(configuration.ConfigurationFile) {
raw, err = yaml.YAMLToJSON(raw)
if err != nil {
return err
}
}

err = json.Unmarshal(raw, configuration)
if err != nil {
return err
Expand Down
54 changes: 54 additions & 0 deletions configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"testing"
)

func TestReadOK(t *testing.T) {
correctfiles := []string{
"testdata/config-example.json",
"testdata/config-example.yaml",
}
for _, testFile := range correctfiles {
c := Configuration{}
c.ConfigurationFile = testFile
err := c.Read()
if err != nil {
t.Fatalf("Could not read correct configuration file : %v with error : %v", c.ConfigurationFile, err)
}
}
}

func TestReadError(t *testing.T) {
wrongfiles := []string{
"", // no file passed
"testdata/", // not a regular file
"testdata/config-bad-wrongFormat.json",
"testdata/config-bad-wrongFormat.yaml",
}
for _, testFile := range wrongfiles {
c := Configuration{}
c.ConfigurationFile = testFile
err := c.Read()
if err == nil {
t.Errorf("reading [%v] file should return an error", c.ConfigurationFile)
}
}
}

func TestWriteOK(t *testing.T) {
correctfiles := []string{
"testdata/config-example.json",
"testdata/config-example.yaml",
}
for _, testFile := range correctfiles {
c := Configuration{}
c.ConfigurationFile = testFile
_ = c.Read()
c.Hash = ""
err := c.Write()
if err != nil {
t.Errorf("Could not write configuration to correct file : %v", c.ConfigurationFile)
}
}
}
50 changes: 50 additions & 0 deletions testdata/config-bad-wrongFormat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
zzzzzzzz{
"version": 1,
"bridge": {
"ip": "192.168.10.37",
"username": "lbCDGagZZ7JEYQX5iGxrjMIx2jIROgpXfsSjHmCv"
},
"location": {
"latitude": 53.5553,
"longitude": 9.995
},
"webinterface": {
"enabled": false,
"port": 8080
},
"schedules": [
{
"name": "default",
"associatedDeviceIDs": [
1,
2,
3,
4,
5,
6
],
"enableWhenLightsAppear": true,
"defaultColorTemperature": 2750,
"defaultBrightness": 100,
"beforeSunrise": [
{
"time": "4:00",
"colorTemperature": 2000,
"brightness": 60
}
],
"afterSunset": [
{
"time": "20:00",
"colorTemperature": 2300,
"brightness": 80
},
{
"time": "22:00",
"colorTemperature": 2000,
"brightness": 60
}
]
}
]
}
34 changes: 34 additions & 0 deletions testdata/config-bad-wrongFormat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[][]dqiwdijqwd
bridge:
ip: 192.168.10.37
username: lbCDGagZZ7JEYQX5iGxrjMIx2jIROgpXfsSjHmCv
location:
latitude: 53.5553
longitude: 9.995
schedules:
- afterSunset:
- brightness: 80
colorTemperature: 2300
time: "20:00"
- brightness: 60
colorTemperature: 2000
time: "22:00"
associatedDeviceIDs:
- 1
- 2
- 3
- 4
- 5
- 6
beforeSunrise:
- brightness: 60
colorTemperature: 2000
time: "4:00"
defaultBrightness: 100
defaultColorTemperature: 2750
enableWhenLightsAppear: true
name: default
version: 1
webinterface:
enabled: false
port: 8080
50 changes: 50 additions & 0 deletions testdata/config-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"version": 1,
"bridge": {
"ip": "192.168.10.37",
"username": "lbCDGagZZ7JEYQX5iGxrjMIx2jIROgpXfsSjHmCv"
},
"location": {
"latitude": 53.5553,
"longitude": 9.995
},
"webinterface": {
"enabled": false,
"port": 8080
},
"schedules": [
{
"name": "default",
"associatedDeviceIDs": [
1,
2,
3,
4,
5,
6
],
"enableWhenLightsAppear": true,
"defaultColorTemperature": 2750,
"defaultBrightness": 100,
"beforeSunrise": [
{
"time": "4:00",
"colorTemperature": 2000,
"brightness": 60
}
],
"afterSunset": [
{
"time": "20:00",
"colorTemperature": 2300,
"brightness": 80
},
{
"time": "22:00",
"colorTemperature": 2000,
"brightness": 60
}
]
}
]
}
33 changes: 33 additions & 0 deletions testdata/config-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
bridge:
ip: 192.168.10.37
username: lbCDGagZZ7JEYQX5iGxrjMIx2jIROgpXfsSjHmCv
location:
latitude: 53.5553
longitude: 9.995
schedules:
- afterSunset:
- brightness: 80
colorTemperature: 2300
time: "20:00"
- brightness: 60
colorTemperature: 2000
time: "22:00"
associatedDeviceIDs:
- 1
- 2
- 3
- 4
- 5
- 6
beforeSunrise:
- brightness: 60
colorTemperature: 2000
time: "4:00"
defaultBrightness: 100
defaultColorTemperature: 2750
enableWhenLightsAppear: true
name: default
version: 1
webinterface:
enabled: false
port: 8080
8 changes: 8 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,11 @@ func durationUntilNextDay() time.Duration {
endOfDay = endOfDay.Add(1 * time.Second)
return time.Until(endOfDay)
}

func isYAMLFile(filename string) bool {
fileExt := filepath.Ext(filename)
if fileExt == ".yaml" || fileExt == ".yml" {
return true
}
return false
}

0 comments on commit 7503100

Please sign in to comment.