/
confighelper.go
110 lines (91 loc) · 3.01 KB
/
confighelper.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright © 2018 Christopher Greaves <cjgreaves97@hotmail.co.uk>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package confighelper
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/Jeffail/gabs"
)
// Setting is a simple struct to represent a setting in a Conf. This is used when trying to Get or Set values in the Conf.
type Setting struct {
Name string
Value string
}
// Conf encompasses the anonymous json object for a template config.
type Conf struct {
raw *gabs.Container
}
// New will create a new Conf using the contents contain in the file found in "path"
func New(path string) (*Conf, error) {
err := validateSettingsPath(path)
if err != nil {
return nil, fmt.Errorf("Error ocurred validating settings path. Error: %v", err.Error())
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Error ocurred reading settings file. Error: %v", err.Error())
}
jsonParsed, err := gabs.ParseJSON(data)
if err != nil {
return nil, err
}
conf := Conf{raw: jsonParsed}
return &conf, nil
}
// GetAllValues gets all of the overridable variables from the Conf
func (c *Conf) GetAllValues() ([]Setting, error) {
children, err := c.raw.ChildrenMap()
if err != nil {
return nil, fmt.Errorf("Error ocurred getting root children from settings file. Error: %v", err.Error())
}
var sets []Setting
getValuesOrCallChildren(children, &sets, "")
return sets, nil
}
// SetValues will take an array of settings to put each one into the Conf. If the setting already exists it will update the value, else it will add the new setting.
func (c *Conf) SetValues(settings []Setting) error {
for _, setting := range settings {
_, err := c.raw.SetP(setting.Value, setting.Name)
if err != nil {
return err
}
}
return nil
}
// Object Returns the Conf as an anonymous object.
func (c *Conf) Object() interface{} {
return c.raw.Data()
}
func validateSettingsPath(path string) error {
if _, err := os.Stat(path); err != nil {
return errors.New("Path to file does not exist")
}
if val := filepath.Ext(path); val != ".json" {
return errors.New("Extension must be '.json'")
}
return nil
}
func getValuesOrCallChildren(children map[string]*gabs.Container, sets *[]Setting, objPath string) {
for child := range children {
nextChildren, _ := children[child].ChildrenMap()
if len(nextChildren) < 1 {
*sets = append(*sets, Setting{Name: objPath + child, Value: children[child].Data().(string)})
} else {
getValuesOrCallChildren(nextChildren, sets, objPath+child+".")
}
}
}