-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
59 lines (50 loc) · 1.29 KB
/
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
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"os"
"time"
)
type TagConfig struct {
Name string `yaml:"name"`
Desc string `yaml:"desc"`
Address uint16 `yaml:"address"`
Operation string `yaml:"operation"`
}
type Config struct {
DeviceUrl string `yaml:"device-url"`
DeviceId uint8 `yaml:"device-id" default:"16"`
Speed uint `yaml:"speed" default:"19200"`
Timeout time.Duration `yaml:"timeout" default:"1s"`
PollingTime time.Duration `yaml:"polling-time" default:"1s"`
ReadPeriod time.Duration `yaml:"read-period" default:"10ms"`
Tags []TagConfig `yaml:"tags"`
}
func NewConfig(configPath string) (config *Config, err error) {
// Create config structure
config = &Config{}
// Open config file
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
// Init new YAML decode
d := yaml.NewDecoder(file)
// Start YAML decoding from file
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
// ValidateConfigPath just makes sure, that the path provided is a file,
// that can be read
func ValidateConfigPath(path string) error {
s, err := os.Stat(path)
if err != nil {
return err
}
if s.IsDir() {
return fmt.Errorf("'%s' is a directory, not a normal file", path)
}
return nil
}