/
conf.go
80 lines (71 loc) · 1.45 KB
/
conf.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
package updater
import (
"bytes"
"fmt"
"os"
"regexp"
"time"
)
type confWriter struct {
file string
module string
mtime string
content []byte
}
func newConfWriter(file, module, mtime string) *confWriter {
return &confWriter{
file: file,
module: module,
mtime: mtime,
}
}
func (cw *confWriter) write(params []moduleParam) error {
var content bytes.Buffer
for _, p := range params {
content.WriteString(p.path)
if p.json {
content.WriteString(":JSON")
}
content.WriteString(" ")
content.WriteString(p.value)
content.WriteString("\n")
}
cw.content = content.Bytes()
return nil
}
var contentRe = regexp.MustCompile(`(?m)^(?:\s*|#.*)(?:\n|$)`)
func (cw *confWriter) isModified(oldContent []byte) (bool, error) {
return !bytes.Equal(contentRe.ReplaceAll(oldContent, []byte{}), cw.content), nil
}
func (cw *confWriter) close() error {
f, err := os.Create(cw.file)
if err != nil {
return err
}
_, err = fmt.Fprintf(f, "# This file is autogenerated by %s at %s\n", os.Args[0], time.Now().Format("2006-01-02 15:04:05"))
if err != nil {
f.Close()
return err
}
_, err = fmt.Fprintf(f, "#! Name %s\n", cw.module)
if err != nil {
f.Close()
return err
}
_, err = fmt.Fprintf(f, "#! Version %s\n\n", cw.mtime)
if err != nil {
f.Close()
return err
}
_, err = f.Write(cw.content)
if err != nil {
f.Close()
return err
}
_, err = f.WriteString("#EOF")
if err != nil {
f.Close()
return err
}
return f.Close()
}