-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcmd_parse.go
106 lines (91 loc) · 1.74 KB
/
cmd_parse.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
//
// Show the result of invoking our parser on the given input-file(s).
//
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"github.com/google/subcommands"
"github.com/skx/deployr/lexer"
"github.com/skx/deployr/parser"
"github.com/skx/deployr/util"
)
//
// parseCmd is the structure for this sub-command.
//
type parseCmd struct {
}
//
// Glue
//
func (*parseCmd) Name() string { return "parse" }
func (*parseCmd) Synopsis() string { return "Show our parser output." }
func (*parseCmd) Usage() string {
return `parser :
Show the output of running our parser on the given file(s).
`
}
//
// Flag setup
//
func (p *parseCmd) SetFlags(f *flag.FlagSet) {
}
//
// Parse the given file.
//
func (p *parseCmd) Parse(file string) {
//
// Read the contents of the file.
//
dat, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("Error reading file %s - %s\n", file, err.Error())
return
}
//
// Create a lexer object with those contents.
//
l := lexer.New(string(dat))
//
// Create a parser, using the lexer.
//
pa := parser.New(l)
//
// Parse the program, looking for errors.
//
statements, err := pa.Parse()
if err != nil {
fmt.Printf("Error parsing program: %s\n", err.Error())
return
}
//
// No errors? Great.
//
// We can dump the parsed statements.
//
for _, statement := range statements {
fmt.Printf("%v\n", statement)
}
}
//
// Entry-point.
//
func (p *parseCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
//
// For each file we were given.
//
for _, file := range f.Args() {
p.Parse(file)
}
//
// Fallback.
//
if len(f.Args()) < 1 {
if util.FileExists("deploy.recipe") {
p.Parse("deploy.recipe")
}
}
return subcommands.ExitSuccess
}