-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (75 loc) · 1.67 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/varnish/vclr/lexer"
"github.com/varnish/vclr/parser"
)
var (
commandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
versionFlag = commandLine.Bool("V", false, "Show version and exit")
vclFile = commandLine.String("f", "", "Parse the specified vcl.")
vclFolder = commandLine.String("p", "", "Parse vcl files in specified folder. ")
version = "N/A"
revision = "N/A"
)
func parseVcl(vclFile string) error {
fileContent, err := ioutil.ReadFile(vclFile)
if err != nil {
return err
}
vclFileContent := string(fileContent)
l := lexer.New(vclFileContent)
p := parser.New(l)
_, err = p.ParseProgram()
return err
}
func parseFolder(vclFolder string) error {
err := filepath.Walk(vclFolder,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.ToLower(filepath.Ext(path)) != ".vcl" {
return nil
}
return parseVcl(path)
})
return err
}
func main() {
commandLine.Usage = func() {
fmt.Fprint(os.Stdout, "Usage of the VCL parser:\n")
commandLine.PrintDefaults()
os.Exit(0)
}
if err := commandLine.Parse(os.Args[1:]); err != nil {
log.Fatalln(err)
}
if *versionFlag {
fmt.Println("Version: " + version)
fmt.Println("Revision: " + revision)
os.Exit(0)
}
if *vclFile == "" && *vclFolder == "" {
fmt.Println("The parser needs either -f or -p to run. See help (-h)")
os.Exit(1)
}
if *vclFile != "" {
err := parseVcl(*vclFile)
if err != nil {
fmt.Println(err)
}
}
if *vclFolder != "" {
err := parseFolder(*vclFolder)
if err != nil {
fmt.Println(err)
}
}
}