-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
49 lines (42 loc) · 1016 Bytes
/
configuration.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
package main
import (
"encoding/json"
"io/ioutil"
"strings"
)
type Configuration struct {
Port int `json:"port"`
Root string `json:"root"`
RamlSuffix string `json:"raml_suffix"`
}
func (c *Configuration) Save() error {
if !strings.HasSuffix(c.Root, "/") {
c.Root = c.Root + "/"
}
file, err := json.Marshal(c)
if err != nil {
return err
}
err = ioutil.WriteFile("./conf.json", file, 0644)
return err
}
func NewBasicConfiguration() Configuration {
return Configuration{Root: ".", Port: 3000, RamlSuffix: "raml"}
}
func NewConfiguration(root, ramlSuffix string, port int) Configuration {
return Configuration{Root: root, RamlSuffix: ramlSuffix, Port: port}
}
func ReadConfiguration() (Configuration, error) {
file, err := ioutil.ReadFile("conf.json")
if err != nil {
return Configuration{}, err
}
var conf Configuration
err = json.Unmarshal(file, &conf)
if err == nil {
if !strings.HasSuffix(conf.Root, "/") {
conf.Root = conf.Root + "/"
}
}
return conf, err
}