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

def self.index
  return {
    "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|
                     begin
                       user_netrc = Netrc.read
                       user_token = user_netrc.[]("build.io").[](1)
                       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}")
                     rescue
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                   },
    },
    "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

                     begin
                       user_netrc = Netrc.read
                       user_token = user_netrc.[]("build.io").[](1)
                       auth = "Bearer #{user_token}"
                     rescue
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                     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|
                     begin
                       user_netrc = Netrc.read
                       user_token = user_netrc.[]("build.io").[](1)
                       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"))
                     rescue
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                   },
    },
    "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|
                     begin
                       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
                         puts(CLI::UI.fmt("{{red:›}}   Error: app not found"))
                         exit(1)
                       end
                       ruby = JSON.parse(res.body).[]("ruby")
                       require("shellwords")
                       command = "/cnb/lifecycle/launcher #{Shellwords.join(args)}"
                       eval(ruby)
                     rescue
                       puts(CLI::UI.fmt("{{red:›}}   Error: not logged in"))
                       exit(1)
                     end
                   },
    }
  }
end