/
config.go
51 lines (43 loc) · 959 Bytes
/
config.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
package main
import (
"io/ioutil"
"github.com/google/uuid"
"github.com/pelletier/go-toml"
)
type Config struct {
Title string `toml:"title"`
DeviceID string `toml:"device_id"`
Host string `toml:"host"`
Port int `toml:"port"`
SerialPort string `toml:"serial_port"`
}
func (self *Config) Fetch(file string) error {
b, err := ioutil.ReadFile(file)
if nil != err {
return err
}
return self.Unmarshal(string(b))
}
func (self *Config) Save(file string) error {
contents, err := self.Marshal()
if nil != err {
return err
}
return ioutil.WriteFile(file, []byte(contents), 0644)
}
func (self *Config) Unmarshal(data string) error {
return toml.Unmarshal([]byte(data), self)
}
func (self Config) Marshal() (string, error) {
b, err := toml.Marshal(self)
if nil != err {
return "", err
}
return string(b), nil
}
func NewConfig() *Config {
return &Config{
Title: "Node",
DeviceID: uuid.New().String(),
}
}