-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.go
More file actions
150 lines (119 loc) · 2.95 KB
/
unit.go
File metadata and controls
150 lines (119 loc) · 2.95 KB
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
package unit
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
/*
reads and builds a map from a standard ini style file
./sample
[sample]
key1=value1
key2=value2
var u unit.Unit
u.Parse("./sample", "sample")
t.Log(u)
*/
type Unit map[string]string
// Parse a unit file; path,section,...
func (m *Unit) Parse(path string, section ...string) bool {
(*m) = Reader(path, section...)
return len(*m) > 0
}
// Writer creates a unit file with a defined section using
// the map[string\string] to configure the key=value items
func Writer(path, section string, kv map[string]string) {
f, _ := os.Create(path)
defer f.Close()
//fmt.Fprintf(f, "# %s created %s\n", filepath.Base(path), time.Now().UTC().Format(time.RFC3339)[:19])
fmt.Fprintf(f, "[%s]\n", section)
for k := range kv {
fmt.Fprintf(f, "%s = %s\n", k, kv[k])
}
}
// Reader will parse the unit file and build the key=value
// map[string]string from the stated section; multiple keys
// have their values joined as comma delimited
func Reader(path string, section ...string) map[string]string {
f, err := os.Open(path)
if err != nil {
return nil
}
defer f.Close()
var row string
var extract = len(section) == 0
var scanner = bufio.NewScanner(f)
var m = make(map[string]string)
for scanner.Scan() {
// strip comments
row = strings.TrimSpace(scanner.Text())
if idx := strings.Index(row, "#"); idx > -1 {
row = row[:idx]
}
// check length
if len(row) == 0 {
continue
}
// section
if strings.HasPrefix(row, "[") && strings.HasSuffix(row, "]") {
if extract = len(section) == 0; !extract {
for i := range section {
if extract = section[i] == row[1:len(row)-1]; extract {
continue
}
}
}
}
// extract key:value
if extract {
var seg []string
switch {
case strings.Contains(row, "="):
seg = strings.SplitN(row, "=", 2)
case strings.Contains(row, ":"):
seg = strings.SplitN(row, ":", 2)
}
if len(seg) > 0 {
seg[0] = strings.TrimSpace(seg[0])
seg[1] = strings.TrimSpace(seg[1])
if _, exist := m[seg[0]]; exist {
m[seg[0]] += "," + seg[1]
} else {
m[seg[0]] = seg[1]
}
//m[strings.TrimSpace(seg[0])] = strings.TrimSpace(seg[1])
}
}
}
return m
}
// Append the key=value item under unit file section at the head
// along with a timestamp reference comment; does not validate
// or provide any assurances that the key is unique
func Append(path, section, key, value string) {
src := path
dst := path + ".tmp"
section = "[" + section + "]"
r, err := os.Open(src)
if err == nil {
w, err := os.Create(dst)
if err == nil {
var row string
var scanner = bufio.NewScanner(r)
for scanner.Scan() {
row = strings.TrimSpace(scanner.Text())
fmt.Fprintln(w, row)
if row == section {
// section, append key:value
fmt.Fprintf(w, "%s=%s # %s\n",
key, value, time.Now().UTC().Format("20060102"))
}
}
w.Close()
defer os.Rename(dst, src)
}
r.Close()
}
}