-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcmd_run.go
193 lines (162 loc) · 3.72 KB
/
cmd_run.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// Execute the recipe from the given file(s).
//
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"regexp"
"strings"
"github.com/google/subcommands"
"github.com/skx/deployr/evaluator"
"github.com/skx/deployr/lexer"
"github.com/skx/deployr/parser"
"github.com/skx/deployr/util"
)
// arrayFlags is the type of a flag that can be duplicated
type arrayFlags []string
// String returns a human-readable version of the flags-set
func (i *arrayFlags) String() string {
return strings.Join(*i, ",")
}
// Set updates the value of this flag, by appending to the list.
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
//
// runCmd holds the state for this sub-command.
//
type runCmd struct {
// nop is true if we should pretend to run commands, not actually
// run them for real.
nop bool
// identity holds the SSH identity file to use.
identity string
// target allows the target against which the recipe runs to be
// set on the command-line.
target string
// vars stores any variables which are specified on the command-line.
vars arrayFlags
// verbose is true if we should be extra-verbose when running.
verbose bool
}
//
// Glue
//
func (*runCmd) Name() string { return "run" }
func (*runCmd) Synopsis() string { return "Run the specified recipe(s)." }
func (*runCmd) Usage() string {
return `run :
Load and execute the recipe in the specified file(s).
`
}
//
// Flag setup
//
func (r *runCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&r.nop, "nop", false, "No operation - just pretend to run.")
f.BoolVar(&r.verbose, "verbose", false, "Run verbosely.")
f.StringVar(&r.identity, "identity", "", "The identity file to use for key-based authentication.")
f.StringVar(&r.target, "target", "", "The target host to execute the recipe against.")
f.Var(&r.vars, "set", "Set the value of a particular variable. (May be repeated.)")
}
//
// Run the given recipe
//
func (r *runCmd) Run(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.
//
p := parser.New(l)
//
// Parse the program, looking for errors.
//
statements, err := p.Parse()
if err != nil {
fmt.Printf("Error parsing program: %s\n", err.Error())
return
}
//
// No errors? Great.
//
// Create the evaluator - which will run the statements.
//
e := evaluator.New(statements)
//
// Set the target, if we've been given one.
//
if r.target != "" {
err = e.ConnectTo(r.target)
if err != nil {
fmt.Printf("Failed to connect to target: %s\n", err.Error())
return
}
}
//
// Set our flags verbosity-level
//
e.SetVerbose(r.verbose)
if r.nop {
e.SetVerbose(true)
e.SetNOP(true)
}
//
// Save the identity-flag - the default is ~/.ssh/id_rsa
//
e.SetIdentity(r.identity)
//
// Are there any variables set on the command-line?
//
re := regexp.MustCompile("^([^=]+)=(.*)$")
for _, set := range r.vars {
matches := re.FindStringSubmatch(set)
if len(matches) == 3 {
e.SetVariable(matches[1], matches[2])
}
}
//
// Now run the program. Hurrah!
//
err = e.Run()
//
// Errors? Boo!
//
if err != nil {
fmt.Printf("Error running program\n%s\n", err.Error())
}
}
//
// Entry-point.
//
func (r *runCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
//
// For each file we were given.
//
for _, file := range f.Args() {
r.Run(file)
}
//
// Fallback.
//
if len(f.Args()) < 1 {
if util.FileExists("deploy.recipe") {
r.Run("deploy.recipe")
}
}
return subcommands.ExitSuccess
}