/
main.go
362 lines (319 loc) · 10.5 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/cloudfoundry/cli/plugin"
)
/*
* This is the struct implementing the interface defined by the core CLI. It can
* be found at "github.com/cloudfoundry/cli/plugin/plugin.go"
*
*/
type TreelineCli struct{}
/*
* This function must be implemented by any plugin because it is part of the
* plugin interface defined by the core CLI.
*
* Run(....) is the entry point when the core CLI is invoking a command defined
* by a plugin. The first parameter, plugin.CliConnection, is a struct that can
* be used to invoke cli commands. The second paramter, args, is a slice of
* strings. args[0] will be the name of the command, and will be followed by
* any additional arguments a cli user typed in.
*
* Any error handling should be handled with the plugin itself (this means printing
* user facing errors). The CLI will exit 0 if the plugin exits 0 and will exit
* 1 should the plugin exits nonzero.
*/
func (c *TreelineCli) Run(cliConnection plugin.CliConnection, args []string) {
// Ensure that we called the command treeline
if args[0] == "treeline" {
_, err := exec.LookPath("treeline")
if err != nil {
fmt.Println("Please install treeline using 'npm install -g treeline'")
os.Exit(1)
}
if args[1] == "config-pws" {
writeDevelopmentConfig()
if _, err := os.Stat(".cfignore"); os.IsNotExist(err) {
err := os.Symlink(".gitignore", ".cfignore")
if err != nil {
fmt.Println("Could not link .cfignore to .gitignore", err)
os.Exit(1)
}
}
npmInstalls()
os.Exit(0)
}
if args[1] == "deploy" {
_, err = cliConnection.CliCommand("push", "hackday-nc", "--no-start")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
_, err = cliConnection.CliCommand("set-env", "hackday-nc", "NODE_ENV", "development")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
createServices(cliConnection)
_, err = cliConnection.CliCommand("start", "hackday-nc")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}
cmd := exec.Command("treeline", args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err = cmd.Start()
if err != nil {
fmt.Println("Error starting command", err)
os.Exit(1)
}
err = cmd.Wait()
if err != nil {
fmt.Println("Error running command", err)
os.Exit(1)
}
}
}
/*
* This function must be implemented as part of the plugin interface
* defined by the core CLI.
*
* GetMetadata() returns a PluginMetadata struct. The first field, Name,
* determines the name of the plugin which should generally be without spaces.
* If there are spaces in the name a user will need to properly quote the name
* during uninstall otherwise the name will be treated as seperate arguments.
* The second value is a slice of Command structs. Our slice only contains one
* Command Struct, but could contain any number of them. The first field Name
* defines the command `cf treeline` once installed into the CLI. The
* second field, HelpText, is used by the core CLI to display help information
* to the user in the core commands `cf help`, `cf`, or `cf -h`.
*/
func (c *TreelineCli) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "TreelineCli",
Version: plugin.VersionType{
Major: 1,
Minor: 0,
Build: 0,
},
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 7,
Build: 0,
},
Commands: []plugin.Command{
plugin.Command{
Name: "treeline",
HelpText: "Basic plugin command's help text",
// UsageDetails is optional
// It is used to show help of usage of each command
UsageDetails: plugin.Usage{
Usage: "treeline\n cf treeline",
},
},
},
}
}
/*
* Unlike most Go programs, the `Main()` function will not be used to run all of the
* commands provided in your plugin. Main will be used to initialize the plugin
* process, as well as any dependencies you might require for your
* plugin.
*/
func main() {
// Any initialization for your plugin can be handled here
//
// Note: to run the plugin.Start method, we pass in a pointer to the struct
// implementing the interface defined at "github.com/cloudfoundry/cli/plugin/plugin.go"
//
// Note: The plugin's main() method is invoked at install time to collect
// metadata. The plugin will exit 0 and the Run([]string) method will not be
// invoked.
plugin.Start(new(TreelineCli))
// Plugin code should be written in the Run([]string) method,
// ensuring the plugin environment is bootstrapped.
}
func npmInstalls() {
packages := []string{"connect-redis@1.4.5", "sails-mysql", "socket.io-redis"}
for _, value := range packages {
npmSetup := exec.Command("npm", "install", value, "--save", "--save-exact")
npmSetup.Stdout = os.Stdout
err := npmSetup.Run()
if err != nil {
fmt.Println("Error installing npm packages", err)
}
}
}
func createServices(cliConnection plugin.CliConnection) {
services, err := cliConnection.GetServices()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
redisFound, redisBound, sqlFound, sqlBound := false, false, false, false
for _, service := range services {
if service.Name == "hackday-rediscloud" {
redisFound = true
for _, app := range service.ApplicationNames {
if app == "hackday-nc" {
redisBound = true
}
}
}
if service.Name == "hackday-cleardb" {
sqlFound = true
for _, app := range service.ApplicationNames {
if app == "hackday-nc" {
sqlBound = true
}
}
}
}
if !redisFound {
_, err = cliConnection.CliCommand("cs", "rediscloud", "30mb", "hackday-rediscloud")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if !redisBound {
_, err = cliConnection.CliCommand("bs", "hackday-nc", "hackday-rediscloud")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if !sqlFound {
_, err = cliConnection.CliCommand("cs", "cleardb", "turtle", "hackday-cleardb")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if !sqlBound {
_, err = cliConnection.CliCommand("bs", "hackday-nc", "hackday-cleardb")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
func writeDevelopmentConfig() {
developmentConfig := []byte(`
/**
* Development environment settings
*/
if (process.env.VCAP_SERVICES) {
vcapServices = JSON.parse(process.env.VCAP_SERVICES);
module.exports = {
/***************************************************************************
* Set the default database connection for models in the development *
* environment (see config/connections.js and config/models.js ) *
***************************************************************************/
models: {
connection: 'sailsMySql',
migrate: 'alter'
},
connections: {
sailsMySql: {
adapter: 'sails-mysql',
host : vcapServices.cleardb[0].credentials.hostname,
port : 3306,
user : vcapServices.cleardb[0].credentials.username,
password : vcapServices.cleardb[0].credentials.password,
database : vcapServices.cleardb[0].credentials.name
}
},
/***************************************************************************
* Session configuration *
***************************************************************************/
session: {
adapter: 'redis',
host: vcapServices.rediscloud[0].credentials.hostname,
port: vcapServices.rediscloud[0].credentials.port,
pass: vcapServices.rediscloud[0].credentials.password,
prefix: 'sess:',
// ttl: <redis session TTL in seconds>,
// db: 0,
},
/***************************************************************************
* WebSocket Configuration *
***************************************************************************/
sockets: {
adapter: 'socket.io-redis',
host: vcapServices.rediscloud[0].credentials.hostname,
port: vcapServices.rediscloud[0].credentials.port,
pass: vcapServices.rediscloud[0].credentials.password,
// db: 'sails',
},
/***************************************************************************
* Set the port in the development environment to 80 *
***************************************************************************/
port: process.env.PORT,
/***************************************************************************
* Set the log level in development environment to "silent" *
***************************************************************************/
log: {
level: "verbose"
}
};
}
`)
err := ioutil.WriteFile("config/env/development.js", developmentConfig, 0644)
if err != nil {
fmt.Println("Error writing configuration", err)
os.Exit(1)
}
fmt.Println("Updated config/env/development.js")
localConfig := []byte(`
/**
* Local environment settings
*/
module.exports = {
/***************************************************************************
* Set the default database connection for models in the local *
* environment (see config/connections.js and config/models.js ) *
***************************************************************************/
models: {
connection: 'localDiskDb',
},
connections: {
localDiskDb: {
adapter: 'sails-disk',
}
},
/***************************************************************************
* Session configuration *
***************************************************************************/
session: {
},
/***************************************************************************
* WebSocket Configuration *
***************************************************************************/
sockets: {
},
/***************************************************************************
* Set the port in the development environment to 80 *
***************************************************************************/
port: process.env.PORT || 1337,
/***************************************************************************
* Set the log level in development environment to "silent" *
***************************************************************************/
log: {
level: "verbose"
}
};
`)
err = ioutil.WriteFile("config/local.js", localConfig, 0644)
if err != nil {
fmt.Println("Error writing configuration", err)
os.Exit(1)
}
fmt.Println("Updated config/local.js")
}