-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-data.go
161 lines (148 loc) · 3.74 KB
/
parse-data.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"sync"
"gopkg.in/yaml.v2"
)
// port struct as per yaml config
type port struct {
Name string `yaml:"name"`
Baudrate int `yaml:"baudrate"`
Parity byte `yaml:"parity"`
Desc string `yaml:"desc"`
Status uint8 `yaml:"status"`
}
// Config type for YAML File marshall/unmarshall
type Config struct {
mu sync.Mutex
Ports []port `yaml:"ports"`
Logs struct {
Inlogs string `yaml:"inlogs"`
Maxsize int `yaml:"maxsize"`
Maxbackups int `yaml:"maxbackups"`
Maxage int `yaml:"maxage"`
} `yaml:"logs"`
ServerConfig []struct {
Name string `yaml:"name"`
Enable int `yaml:"enable"`
Port int `yaml:"port"`
SslCert string `yaml:"sslcert"`
SslKey string `yaml:"sslkey"`
} `yaml:"serverconfig"`
}
// writeYaml will write new change config struct to file
func (config *Config) writeYaml(filename string) error {
config.mu.Lock()
defer config.mu.Unlock()
out, err := yaml.Marshal(config)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, out, 0644)
if err != nil {
return err
}
return nil
}
// parseYaml will return err or parse yaml file
func (config *Config) parseYaml(fileName string) error {
yamlFile, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, config)
if err != nil {
log.Printf("Error : %s", err)
return err
}
return nil
}
// getJSON will convert struct to JSON format and return
// converted byte slice or error
func (config *Config) getJSON() ([]byte, error) {
config.mu.Lock()
defer config.mu.Unlock()
b, err := json.Marshal(config)
if err != nil {
return []byte(""), err
}
return b, err
}
// removeElement will remove provided port name from ports slice
// if name not found then return false
func (c *Config) removeElement(portname string) bool {
c.mu.Lock()
defer c.mu.Unlock()
for index, value := range c.Ports {
if value.Name == portname {
copy(c.Ports[index:], c.Ports[index+1:])
c.Ports[len(c.Ports)-1] = port{}
c.Ports = c.Ports[:len(c.Ports)-1]
return true
}
}
return false
}
// checkElement will check given port into config struct
// and return true if found or false.
func (c *Config) checkElement(portname string) bool {
c.mu.Lock()
defer c.mu.Unlock()
for _, value := range c.Ports {
if value.Name == portname {
return true
}
}
return false
}
// addElement will add new element provided port name, baudrate
// and description
func (c *Config) addElement(portname string, baudrate int, des string) error {
c.mu.Lock()
defer c.mu.Unlock()
for _, value := range c.Ports {
if value.Name == portname {
return errors.New("port name already exist")
}
}
c.Ports = append(c.Ports, port{Name: portname, Baudrate: baudrate, Desc: des, Status: 1})
return nil
}
// update element description with given new description
func (c *Config) updateElement(portname string, desc string) error {
c.mu.Lock()
defer c.mu.Unlock()
for index := range c.Ports {
if c.Ports[index].Name == portname {
c.Ports[index].Desc = desc
return nil
}
}
return errors.New("did not find any element with given port")
}
// getStatus will return port status for a given port
func (c *Config) getStatus(portname string) (uint8, error) {
c.mu.Lock()
defer c.mu.Unlock()
for index := range c.Ports {
if c.Ports[index].Name == portname {
return c.Ports[index].Status, nil
}
}
return 2, errors.New("port not found")
}
// portStatusUpdate will return port status for a given port
func (c *Config) portStatusUpdate(portname string, st uint8) error {
c.mu.Lock()
defer c.mu.Unlock()
for index := range c.Ports {
if c.Ports[index].Name == portname {
c.Ports[index].Status = st
return nil
}
}
return errors.New("port not found")
}