-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
88 lines (73 loc) · 1.52 KB
/
file.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
package util
import (
"go/ast"
"go/parser"
"go/token"
"os"
"regexp"
)
const (
outputFilename = "./dnsroot.go"
)
func OutputFileExists() bool {
if _, err := os.Stat(outputFilename); err != nil {
return false
}
return true
}
func RemoveOutputFile() error {
if OutputFileExists() {
return os.Remove(outputFilename)
}
return nil
}
func WriteOutputFile(data []byte) error {
f, err := os.Create(outputFilename)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
return err
}
return nil
}
func GetCurrentOutputFileVersion() (string, bool) {
if !OutputFileExists() {
return "", false
}
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, outputFilename, nil, parser.ParseComments)
if err != nil {
return "", false
}
if !isCurrentProgramVersion(file.Comments) {
return "", false
}
return getInternicVersion(file.Comments)
}
func isCurrentProgramVersion(comentGroups []*ast.CommentGroup) bool {
ivr := regexp.MustCompile(`^// dnsroot version:\s+(\d\.\d\.\d)$`)
for _, cg := range comentGroups {
for _, c := range cg.List {
vs := ivr.FindStringSubmatch(c.Text)
if len(vs) > 1 {
return (vs[1] == ProgramVersion)
}
}
}
return false
}
func getInternicVersion(comentGroups []*ast.CommentGroup) (string, bool) {
ivr := regexp.MustCompile(`^// InterNIC version:\s+(\d+)$`)
for _, cg := range comentGroups {
for _, c := range cg.List {
vs := ivr.FindStringSubmatch(c.Text)
if len(vs) > 1 {
return vs[1], true
}
}
}
return "", false
}