-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
cmd_lex.go
88 lines (74 loc) · 1.34 KB
/
cmd_lex.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
//
// Invoke our lexer on the specified input-file(s) and dump the output.
//
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"github.com/google/subcommands"
"github.com/skx/deployr/lexer"
"github.com/skx/deployr/util"
)
//
// lexCmd is the structure for this sub-command.
//
type lexCmd struct {
}
//
// Glue
//
func (*lexCmd) Name() string { return "lex" }
func (*lexCmd) Synopsis() string { return "Show our lexer output." }
func (*lexCmd) Usage() string {
return `lex :
Show the output of running our lexer on the given file(s).
`
}
//
// Flag setup
//
func (p *lexCmd) SetFlags(f *flag.FlagSet) {
}
//
// Lex the given recipe
//
func (p *lexCmd) Lex(file string) {
//
// Read the file contents.
//
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))
//
// Dump the tokens.
//
l.Dump()
}
//
// Entry-point.
//
func (p *lexCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
//
// For each file we've been passed.
//
for _, file := range f.Args() {
p.Lex(file)
}
//
// Fallback.
//
if len(f.Args()) < 1 {
if util.FileExists("deploy.recipe") {
p.Lex("deploy.recipe")
}
}
return subcommands.ExitSuccess
}