Class: BuildCLICommands

Inherits:
Object
  • Object
show all
Defined in:
lib/commands.rb

Overview

NOTE: This file is generated

Class Method Summary collapse

Class Method Details

.indexObject



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
# File 'lib/commands.rb', line 3

def self.index
  return {
    "config": {
      cli_details: { :syntax => "build config -a <app>", :description => "Get the config variables for an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/config?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body.[]("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{res.body.[]("error")}"))
                         exit(1)
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                         exit(1)
                       end
                     end
                     JSON.parse(res.body).each { |key, value|
                       puts("#{key}=#{value}")
                     }
                   },
    },
    "config:get": {
      cli_details: { :syntax => "build config:get VARIABLE -a <app>", :description => "Get a config variable for an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     config_variable = args.[](0)
                     if config_variable.nil? || config_variable.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required argument VARIABLE}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     query_params.[]=(:key, config_variable)
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/config/get?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body.[]("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{res.body.[]("error")}"))
                         exit(1)
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                         exit(1)
                       end
                     end
                     puts(res.body)
                   },
    },
    "config:set": {
      cli_details: { :syntax => "build config:set VARIABLE=VALUE -a <app>", :description => "Set config variables for an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     config_variables = args.map { |arg,|
                       arg.split("=")
                     }.to_h
                     if config_variables.empty?
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:No config variables provided}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     query_params.[]=(:config, config_variables.to_json)
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/config/set?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body.[]("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{res.body.[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     puts(CLI::UI.fmt("{{green:›}}   Config variable(s) set successfully"))
                   },
    },
    "config:unset": {
      cli_details: { :syntax => "build config:unset VARIABLE -a <app>", :description => "Un-Set config variables for an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     config_variables = args.compact
                     if config_variables.empty?
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:No config variables provided}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     query_params.[]=(:config, config_variables)
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/config/unset?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body && res.body.[]("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{res.body.[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: An unknown error occurred"))
                       end
                       exit(1)
                     end
                     puts(CLI::UI.fmt("{{green:›}}   Config variable(s) unset successfully"))
                   },
    },
    "apps:create": {
      cli_details: { :syntax => "apps:create [options] -a <app>", :description => "Create a new app",
                     :options => [["-t", "--team=<value>", String, "team to use"], ["-r", "--region=<value>", String, "specify region for the app to run in"], ["-s", "--stack=<value>", String, "the stack to create the app on"], ["-a", "--app=<value>", String, "(required) the name of the app to create"]] },
      cli_perform: Proc.new { |args, options|
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     app_name = options.app
                     if app_name.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: no app name specified"))
                       exit(1)
                     end
                     query_params = {}
                     query_params.[]=(:app, app_name)
                     if options.team
                       query_params.[]=(:team, options.team)
                     end
                     if options.region
                       query_params.[]=(:region, options.region)
                     end
                     if options.stack
                       query_params.[]=(:stack, options.stack)
                     end
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/apps/create?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       puts(CLI::UI.fmt("{{red:›}}   Error: not authorized to create app (#{app_name})"))
                       exit(1)
                     end
                     app_name = JSON.parse(res.body).[]("name")
                     puts("https://#{app_name}.#{region}.antimony.io | #{app_name}")
                   },
    },
    "login": {
      cli_details: { :syntax => "build login", :description => "Login to your Build account", :options => [] },
      cli_perform: Proc.new { |args, options|
                     input = CLI::UI.any_key("Press any key to open up the browser to login or q to exit")
                     if input.downcase == "q"
                       exit
                     end
                     (user_token, user_email) = nil
                     CLI::UI::Spinner.spin("Waiting for login") { |spinner,|
                       client_secret = SecureRandom.uuid
                       oauth_url = "#{ANTIMONY_HOST}/cli_auth/authorize/#{client_secret}"
                       Launchy.open(oauth_url)
                       poll_interval = 1
                       timeout = (5 * 60)
                       start_time = Time.now
                       loop {
                         response = HTTParty.get("#{ANTIMONY_HOST}/api/cli_auth/resolve/#{client_secret}")
                         if response.code == 200 && response.[]("code") == "unresolved"
                           sleep(poll_interval)
                         else
                           if response.code == 200 && response.[]("code") == "resolved"
                             user_token = response.[]("token")
                             user_email = response.[]("email")
                             break
                           else
                             raise("Error: #{response.code}")
                           end
                         end
                         if Time.now - start_time > timeout
                           break
                         end
                       }
                       user_netrc = Netrc.read
                       user_netrc.[]=("build.io", ["#{user_email}", "#{user_token}"])
                       user_netrc.save
                     }
                     puts(CLI::UI.fmt("Logged in as {{green:#{user_email}}}"))
                   },
    },
    "logs": {
      cli_details: { :syntax => "build logs -t -a <app>", :description => "Display recent log output",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"], ["-t", "--tail", TrueClass, "continually stream logs"], ["-n", "--num=<value>", String, "number of lines to show"], ["-p", "--process=<value>", String, "show only logs for a specific Procfile process"], ["-s", "--source=<value>", String, "show only logs from a specific source (such as app or build)"]] },
      cli_perform: Proc.new { |args, options|
                     app_name = options.app
                     if app_name.nil? || app_name.strip == ""
                       puts(CLI::UI.fmt("{{red:›}}   Error: The following error occurred:"))
                       puts(CLI::UI.fmt("{{red:›}}     {{gray:Missing required flag app}}"))
                       puts(CLI::UI.fmt("{{red:›}}   See more help with --help"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     if options.num
                       query_params.[]=(:num, options.num)
                     end
                     if options.process
                       query_params.[]=(:process, options.process)
                     end
                     if options.source
                       query_params.[]=(:source, options.source)
                     end
                     if options.trace
                       query_params.[]=(:tail, options.trace)
                     end
                     if options.tail
                       query_params.[]=(:tail, options.tail)
                     end
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/apps/#{app_name}/logs/log_url?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       puts(CLI::UI.fmt("{{red:›}}   Error: Couldn't find that app."))
                       puts(CLI::UI.fmt("{{red:›}}"))
                       puts(CLI::UI.fmt("{{red:›}}   Error ID: not_found"))
                       exit(1)
                     end
                     log_url = res.[]("url")
                     res = HTTParty.get(log_url, { timeout: 1000000 }) { |fragment,|
                       unless fragment.empty?
                         puts(fragment)
                       end
                     }
                     if res.code != 200
                       puts(CLI::UI.fmt("{{red:›}}   Error: Connection to logs failed."))
                       puts(CLI::UI.fmt("{{red:›}}"))
                       puts(CLI::UI.fmt("{{red:›}}   Error ID: connection_failed"))
                       exit(1)
                     end
                   },
    },
    "whoami": {
      cli_details: { :syntax => "build whoami", :description => "Display the current logged in user",
                     :options => [] },
      cli_perform: Proc.new { |args, options|
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io")&.password
                     if user_token.nil?
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     auth = "Bearer #{user_token}"
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/whoami",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     puts(JSON.parse(res.body).[]("email"))
                   },
    },
    "run": {
      cli_details: { :syntax => "build run COMMAND -a <app>", :description => "Run a once-off process in an app",
                     :options => [["-a", "--app=<value>", String, "(required) app to run command against"]] },
      cli_perform: Proc.new { |args, options|
                     if args.empty?
                       puts(CLI::UI.fmt("{{red:›}}   Error: no command provided"))
                       exit(1)
                     end
                     user_netrc = Netrc.read
                     user_token = user_netrc.[]("build.io").[](1)
                     auth = "Bearer #{user_token}"
                     query_params = {}
                     if options.app
                       query_params.[]=(:app, options.app)
                     end
                     query_string = URI.encode_www_form(query_params)
                     res = HTTParty.get("#{ANTIMONY_HOST}/api/cli/command/run?#{query_string}",
                                        { headers: { "Authorization" => auth } })
                     if res.code != 200
                       if res.body.[]("error")
                         puts(CLI::UI.fmt("{{red:›}}   Error: #{res.body.[]("error")}"))
                       else
                         puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       end
                       exit(1)
                     end
                     ruby = JSON.parse(res.body).[]("ruby")
                     require("shellwords")
                     command = "/cnb/lifecycle/launcher #{Shellwords.join(args)}"
                     eval(ruby)
                   },
    }
  }
end