Skip to content

Commit 5e8ccda

Browse files
committed
Use a default-file if none are specified.
If no input file(s) are specified then use a default of `deployr.recipe` which is what I used when I wrote my rules for my applications. This closes #4.
1 parent 2bd31ee commit 5e8ccda

File tree

5 files changed

+198
-115
lines changed

5 files changed

+198
-115
lines changed

cmd_lex.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"flag"
1010
"fmt"
1111
"io/ioutil"
12-
"os"
1312

1413
"github.com/google/subcommands"
1514
"github.com/skx/deployr/lexer"
15+
"github.com/skx/deployr/util"
1616
)
1717

1818
//
@@ -38,6 +38,31 @@ func (*lexCmd) Usage() string {
3838
func (p *lexCmd) SetFlags(f *flag.FlagSet) {
3939
}
4040

41+
//
42+
// Lex the given recipe
43+
//
44+
func (p *lexCmd) Lex(file string) {
45+
46+
//
47+
// Read the file contents.
48+
//
49+
dat, err := ioutil.ReadFile(file)
50+
if err != nil {
51+
fmt.Printf("Error reading file %s - %s\n", file, err.Error())
52+
return
53+
}
54+
55+
//
56+
// Create a lexer object with those contents.
57+
//
58+
l := lexer.New(string(dat))
59+
60+
//
61+
// Dump the tokens.
62+
//
63+
l.Dump()
64+
}
65+
4166
//
4267
// Entry-point.
4368
//
@@ -47,25 +72,16 @@ func (p *lexCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
4772
// For each file we've been passed.
4873
//
4974
for _, file := range f.Args() {
75+
p.Lex(file)
76+
}
5077

51-
//
52-
// Read the file contents.
53-
//
54-
dat, err := ioutil.ReadFile(file)
55-
if err != nil {
56-
fmt.Printf("Error reading file %s - %s\n", file, err.Error())
57-
os.Exit(1)
78+
//
79+
// Fallback.
80+
//
81+
if len(f.Args()) < 1 {
82+
if util.FileExists("deploy.recipe") {
83+
p.Lex("deploy.recipe")
5884
}
59-
60-
//
61-
// Create a lexer object with those contents.
62-
//
63-
l := lexer.New(string(dat))
64-
65-
//
66-
// Dump the tokens.
67-
//
68-
l.Dump()
6985
}
7086

7187
return subcommands.ExitSuccess

cmd_parse.go

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"flag"
1010
"fmt"
1111
"io/ioutil"
12-
"os"
1312

1413
"github.com/google/subcommands"
1514
"github.com/skx/deployr/lexer"
1615
"github.com/skx/deployr/parser"
16+
"github.com/skx/deployr/util"
1717
)
1818

1919
//
@@ -40,50 +40,65 @@ func (p *parseCmd) SetFlags(f *flag.FlagSet) {
4040
}
4141

4242
//
43-
// Entry-point.
43+
// Parse the given file.
4444
//
45-
func (p *parseCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
45+
func (p *parseCmd) Parse(file string) {
46+
//
47+
// Read the contents of the file.
48+
//
49+
dat, err := ioutil.ReadFile(file)
50+
if err != nil {
51+
fmt.Printf("Error reading file %s - %s\n", file, err.Error())
52+
return
53+
}
4654

4755
//
48-
// For each file we were given.
56+
// Create a lexer object with those contents.
4957
//
50-
for _, file := range f.Args() {
58+
l := lexer.New(string(dat))
5159

52-
//
53-
// Read the contents of the file.
54-
//
55-
dat, err := ioutil.ReadFile(file)
56-
if err != nil {
57-
fmt.Printf("Error reading file %s - %s\n", file, err.Error())
58-
os.Exit(1)
59-
}
60+
//
61+
// Create a parser, using the lexer.
62+
//
63+
pa := parser.New(l)
6064

61-
//
62-
// Create a lexer object with those contents.
63-
//
64-
l := lexer.New(string(dat))
65+
//
66+
// Parse the program, looking for errors.
67+
//
68+
statements, err := pa.Parse()
69+
if err != nil {
70+
fmt.Printf("Error parsing program: %s\n", err.Error())
71+
return
72+
}
6573

66-
//
67-
// Create a parser, using the lexer.
68-
//
69-
p := parser.New(l)
74+
//
75+
// No errors? Great.
76+
//
77+
// We can dump the parsed statements.
78+
//
79+
for _, statement := range statements {
80+
fmt.Printf("%v\n", statement)
81+
}
82+
}
7083

71-
//
72-
// Parse the program, looking for errors.
73-
//
74-
statements, err := p.Parse()
75-
if err != nil {
76-
fmt.Printf("Error parsing program: %s\n", err.Error())
77-
return subcommands.ExitFailure
78-
}
84+
//
85+
// Entry-point.
86+
//
87+
func (p *parseCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
7988

80-
//
81-
// No errors? Great.
82-
//
83-
// We can dump the parsed statements.
84-
//
85-
for _, statement := range statements {
86-
fmt.Printf("%v\n", statement)
89+
//
90+
// For each file we were given.
91+
//
92+
for _, file := range f.Args() {
93+
p.Parse(file)
94+
}
95+
96+
//
97+
// Fallback.
98+
//
99+
if len(f.Args()) < 1 {
100+
if util.FileExists("deploy.recipe") {
101+
p.Parse("deploy.recipe")
87102
}
88103
}
89104

cmd_run.go

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"flag"
1010
"fmt"
1111
"io/ioutil"
12-
"os"
1312

1413
"github.com/google/subcommands"
1514
"github.com/skx/deployr/evaluator"
1615
"github.com/skx/deployr/lexer"
1716
"github.com/skx/deployr/parser"
17+
"github.com/skx/deployr/util"
1818
)
1919

2020
//
@@ -45,77 +45,93 @@ func (r *runCmd) SetFlags(f *flag.FlagSet) {
4545
}
4646

4747
//
48-
// Entry-point.
48+
// Run the given recipe
4949
//
50-
func (r *runCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
50+
func (r *runCmd) Run(file string) {
5151

5252
//
53-
// For each file we were given.
53+
// Read the contents of the file.
5454
//
55-
for _, file := range f.Args() {
55+
dat, err := ioutil.ReadFile(file)
56+
if err != nil {
57+
fmt.Printf("Error reading file %s - %s\n", file, err.Error())
58+
return
59+
}
5660

57-
//
58-
// Read the contents of the file.
59-
//
60-
dat, err := ioutil.ReadFile(file)
61-
if err != nil {
62-
fmt.Printf("Error reading file %s - %s\n", file, err.Error())
63-
os.Exit(1)
64-
}
61+
//
62+
// Create a lexer object with those contents.
63+
//
64+
l := lexer.New(string(dat))
6565

66-
//
67-
// Create a lexer object with those contents.
68-
//
69-
l := lexer.New(string(dat))
66+
//
67+
// Create a parser, using the lexer.
68+
//
69+
p := parser.New(l)
7070

71-
//
72-
// Create a parser, using the lexer.
73-
//
74-
p := parser.New(l)
71+
//
72+
// Parse the program, looking for errors.
73+
//
74+
statements, err := p.Parse()
75+
if err != nil {
76+
fmt.Printf("Error parsing program: %s\n", err.Error())
77+
return
78+
}
79+
80+
//
81+
// No errors? Great.
82+
//
83+
// Create the evaluator - which will run the statements.
84+
//
85+
e := evaluator.New(statements)
7586

76-
//
77-
// Parse the program, looking for errors.
78-
//
79-
statements, err := p.Parse()
87+
//
88+
// Set the target, if we've been given one.
89+
//
90+
if r.target != "" {
91+
err := e.ConnectTo(r.target)
8092
if err != nil {
81-
fmt.Printf("Error parsing program: %s\n", err.Error())
82-
return subcommands.ExitFailure
83-
}
93+
fmt.Printf("Failed to connect to target: %s\n", err.Error())
94+
return
8495

85-
//
86-
// No errors? Great.
87-
//
88-
// Create the evaluator - which will run the statements.
89-
//
90-
e := evaluator.New(statements)
91-
92-
//
93-
// Set the target, if we've been given one.
94-
//
95-
if r.target != "" {
96-
err := e.ConnectTo(r.target)
97-
if err != nil {
98-
fmt.Printf("Failed to connect to target: %s\n", err.Error())
99-
return subcommands.ExitFailure
100-
101-
}
10296
}
97+
}
10398

104-
//
105-
// Set the verbosity-level
106-
//
107-
e.SetVerbose(r.verbose)
99+
//
100+
// Set the verbosity-level
101+
//
102+
e.SetVerbose(r.verbose)
108103

109-
//
110-
// Now run the program. Hurrah!
111-
//
112-
err = e.Run()
104+
//
105+
// Now run the program. Hurrah!
106+
//
107+
err = e.Run()
113108

114-
//
115-
// Errors? Boo!
116-
//
117-
if err != nil {
118-
fmt.Printf("Error running program\n%s\n", err.Error())
109+
//
110+
// Errors? Boo!
111+
//
112+
if err != nil {
113+
fmt.Printf("Error running program\n%s\n", err.Error())
114+
}
115+
}
116+
117+
//
118+
// Entry-point.
119+
//
120+
func (r *runCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
121+
122+
//
123+
// For each file we were given.
124+
//
125+
for _, file := range f.Args() {
126+
r.Run(file)
127+
}
128+
129+
//
130+
// Fallback.
131+
//
132+
if len(f.Args()) < 1 {
133+
if util.FileExists("deploy.recipe") {
134+
r.Run("deploy.recipe")
119135
}
120136
}
121137

util/util.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ import (
77
"os"
88
)
99

10-
//
11-
// hashFile returns the SHA1-hash of the contents of the specified file.
12-
//
10+
// FileExists reports whether the named file or directory exists.
11+
func FileExists(name string) bool {
12+
if _, err := os.Stat(name); err != nil {
13+
if os.IsNotExist(err) {
14+
return false
15+
}
16+
}
17+
return true
18+
}
19+
20+
// HashFile returns the SHA1-hash of the contents of the specified file.
1321
func HashFile(filePath string) (string, error) {
1422
var returnSHA1String string
1523

0 commit comments

Comments
 (0)