-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
327 lines (286 loc) · 9.34 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
package main
import (
"bytes"
"fmt"
"io"
"os"
"runtime"
"strconv"
"strings"
"text/template"
"code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/cf/trace"
"code.cloudfoundry.org/cli/plugin"
"github.com/cloudfoundry-community/ocf-scheduler-cf-plugin/commands"
"github.com/cloudfoundry-community/ocf-scheduler-cf-plugin/core"
)
var Version string = "v0.0.0"
var SemVerMajor string
var SemVerMinor string
var SemVerPatch string
var SemVerPrerelease string
var SemVerBuild string
var BuildDate string
var BuildVcsUrl string
var BuildVcsId string
var BuildVcsIdDate string
var GoArch string
var GoOs string
type OCFScheduler struct{}
// Setup to use the cf cli ui prompts
// CloudFoundry/cli/cf/i18n
// define the T function which is suppose to translate and process go templates
// define the teePrinter
// define the UI interface
var teePrinter *terminal.TeePrinter
var ui terminal.UI
func (c *OCFScheduler) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "OCFScheduler",
Version: plugin.VersionType{
Major: getVersion("Major", SemVerMajor),
Minor: getVersion("Minor", SemVerMinor),
Build: getVersion("Patch",SemVerPatch),
},
Commands: []plugin.Command{
{
Name: "create-job",
HelpText: "Creates a job (task) related to an app.",
UsageDetails: plugin.Usage{
Usage: "create-job:\n\tcf create-job APP-NAME JOB-NAME COMMAND [OPTIONS]\n\nWHERE\n\tAPP-NAME is the name of the cf app environment to execute with\n\tJOB-NAME is the name for this job (task)\n\tCOMMAND is the name of the command to execute within the app environment.\n\nOPTIONS\n\t--disk LIMIT/-k LIMIT set job(task) disk limit (default 1024M)\n\t--memory LIMIT/-m LIMIT set the job(task) memory limit (default 1024M)\n\n\tNOTE: In both of the above options, LIMIT must be specified as an\n\tinteger with an M or G at the end. This suffix is required to\n\tdifferentiate between megabytes and gigabytes (and to avoid parser\n\terrors).\n",
},
},
{
Name: "run-job",
HelpText: "Runs the job (task) with the given name once.",
UsageDetails: plugin.Usage{
Usage: "run-job:\n\tcf run-job JOB-NAME",
},
},
{
Name: "schedule-job",
HelpText: "Schedules the named job (task) to run based on the given cron schedule.",
UsageDetails: plugin.Usage{
Usage: "schedule-job:\n\tcf schedule-job JOB-NAME CRON-EXPRESSION\n\nWHERE\n\tJOB-NAME is the name of the created job\n\tCRON-EXPRESSION is the cron schedule format \"MIN HOUR DAY-OF-MONTH MONTH DAY-OF-WEEK\"",
},
},
{
Name: "jobs",
HelpText: "Lists created jobs.",
UsageDetails: plugin.Usage{
Usage: "jobs:\ncf jobs",
},
},
{
Name: "job-schedules",
HelpText: "Lists created job schedules",
UsageDetails: plugin.Usage{
Usage: "job-schedules:\n\tcf job-schedules",
},
},
{
Name: "job-history",
HelpText: "Lists execution history for the given job name",
UsageDetails: plugin.Usage{
Usage: "job-history:\n\tcf job-history JOB-NAME",
},
},
{
Name: "delete-job",
HelpText: "Deletes named job.",
UsageDetails: plugin.Usage{
Usage: "delete-job:\n\tcf delete-job JOB-NAME [OPTIONS]\n\nWHERE\n\tJOB-NAME is the job (task) name to delete\n\nOPTIONS\n\t--force /-f Force deletion without confirmation",
},
},
{
Name: "delete-job-schedule",
HelpText: "Deletes the job scheduled with the named GUID.",
UsageDetails: plugin.Usage{
Usage: "delete-job-schedule:\n\tcf delete-job-schedule JOB-NAME SCHEDULE-GUID [OPTIONS]\n\nOPTIONS\n\t--force /-f Force deletion without confirmation",
},
},
{
Name: "create-call",
HelpText: "Creates a web request call",
UsageDetails: plugin.Usage{
Usage: "create-call:\n\tcf create-call APP-NAME CALL-NAME URL\nWHERE\n\tAPP-NAME is the name of the cf app to create a call for\n\tCALL-NAME is a name to refer to the call as\n\tURL is the URL to call.",
},
},
{
Name: "run-call",
HelpText: "Execute a named call request once.",
UsageDetails: plugin.Usage{
Usage: "run-call:\n\tcf run-call CALL-NAME",
},
},
{
Name: "schedule-call",
HelpText: "Schedules a call to be run based on the supplied cron schedule",
UsageDetails: plugin.Usage{
Usage: "schedule-call:\n\tcf schedule-call CALL-NAME SCHEDULE\n\tCALL-NAME is a name for the scheduled call\n\tSCHEUDLE is a schedule using cron schedule format \"MIN HOUR DAY-OF-MONTH DAY-OF-WEEK\"\n\nEXAMPLE\n\tcf schedule-call hourlyrun \"0 * * * *\"",
},
},
{
Name: "calls",
HelpText: "List created calls",
UsageDetails: plugin.Usage{
Usage: "calls:\n\tcf calls",
},
},
{
Name: "call-schedules",
HelpText: "List calls scheduled to be run with app and schedule.",
UsageDetails: plugin.Usage{
Usage: "call-schedules:\n\tcf call-schedules",
},
},
{
Name: "call-history",
HelpText: "Shows the execution history for the named call.",
UsageDetails: plugin.Usage{
Usage: "call-history:\n\tcf call-history CALL-NAME",
},
},
{
Name: "delete-call",
HelpText: "Deletes the named call.",
UsageDetails: plugin.Usage{
Usage: "delete-call:\n\tcf delete-call CALL-NAME [OPTIONS]\n\nOPTIONS\n\t--force /-f Force deletion without confirmation",
},
},
{
Name: "delete-call-schedule",
HelpText: "Delete a call scheduled with a given GUID",
UsageDetails: plugin.Usage{
Usage: "delete-call-schedule:\n\tcf delete-call-schedule CALL-NAME SCHEDULE-GUID [OPTIONS]\n\nOPTIONS\n\t--force /-f Force deletion without confirmation",
},
},
},
}
}
func (c *OCFScheduler) Run(cliConnection plugin.CliConnection, args []string) {
i18n.T = func(translationID string, args ...interface{}) string {
var buffer bytes.Buffer
var keys interface{}
if len(args) > 0 {
keys = args[0]
}
formattedTemplate := template.Must(template.New("Display Text").Parse(translationID))
err := formattedTemplate.Execute(&buffer, keys)
if err != nil {
return translationID + "\ntemplate processing failed " + err.Error() + "\n"
}
return buffer.String()
}
if runtime.GOOS == "windows" {
terminal.UserAskedForColors = "false"
}
terminal.InitColorSupport()
teePrinter = terminal.NewTeePrinter(os.Stdout)
// Should we really be using NewPluginUI instead TODO
ui = terminal.NewUI(os.Stdin, os.Stdout, teePrinter, trace.NewWriterPrinter(io.Discard, false))
scheduler, err := core.GetScheduler(cliConnection)
if err != nil {
fmt.Println(err)
return
}
token, err := core.GetBearer(cliConnection)
if err != nil {
fmt.Println(err)
return
}
client, err := core.NewDriver(scheduler, token)
if err != nil {
fmt.Println("Could not create a Scheduler API client.")
return
}
services := &core.Services{CLI: cliConnection, Client: client, UI: ui}
switch args[0] {
case "create-job":
commands.CreateJob(services, args)
case "run-job":
commands.RunJob(services, args)
case "schedule-job":
commands.ScheduleJob(services, args)
case "jobs":
commands.Jobs(services, args)
case "job-schedules":
commands.JobSchedules(services, args)
case "job-history":
commands.JobHistory(services, args)
case "delete-job":
commands.DeleteJob(services, args)
case "delete-job-schedule":
commands.DeleteJobSchedule(services, args)
case "create-call":
commands.CreateCall(services, args)
case "run-call":
commands.RunCall(services, args)
case "schedule-call":
commands.ScheduleCall(services, args)
case "calls":
commands.Calls(services, args)
case "call-schedules":
commands.CallSchedules(services, args)
case "call-history":
commands.CallHistory(services, args)
case "delete-call":
commands.DeleteCall(services, args)
case "delete-call-schedule":
commands.DeleteCallSchedule(services, args)
}
}
func createBuildMeta(buildOs, buildArch, build string) string {
p1 := strings.TrimSpace(buildOs)
p2 := strings.TrimSpace(buildArch)
p3 := strings.TrimSpace(build)
if p1 == "" || p2 == "" {
panic(fmt.Sprintf("Go meta data is missing one of its parts: %s, %s ", p1, p2))
}
b := strings.Join([]string{p1, p2}, ".")
if p3 != "" {
b += "." + p3
}
return b
}
func createSemVer(major, minor, patch, prerelease, build string) string {
p1 := strings.TrimSpace(major)
p2 := strings.TrimSpace(minor)
p3 := strings.TrimSpace(patch)
p4 := strings.TrimSpace(prerelease)
p5 := strings.TrimSpace(build)
if p1 == "" || p2 == "" || p3 == "" {
panic(fmt.Sprintf("Semanic version is missing one of its parts: %s.%s.%s", p1, p2, p3))
}
sv := strings.Join([]string{p1, p2, p3}, ".")
if p4 != "" {
sv += "-" + p4
}
if p5 != "" {
sv += "+" + p5
}
return sv
}
func getVersion(version, toInt string) int {
theInt, err := strconv.Atoi(toInt)
if err != nil {
theInt = 0
fmt.Printf("Warning: %v for %v version value. Defaulting to a zero value\n", err.Error(), version)
}
return theInt
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
bm := createBuildMeta(GoOs, GoArch, SemVerBuild)
sv := createSemVer(SemVerMajor, SemVerMinor, SemVerPatch, SemVerPrerelease, bm)
f := "%13v %v\n"
fmt.Printf(f, "Version:", sv)
fmt.Printf(f, "Build Date:", BuildDate)
fmt.Printf(f, "VCS Url:", BuildVcsUrl)
fmt.Printf(f, "VCS Id:", BuildVcsId)
fmt.Printf(f, "VCS Id Date:", BuildVcsIdDate)
}
plugin.Start(new(OCFScheduler))
}