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
# 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
                       query_params[:team] = options.team if options.team
                       query_params[:region] = options.region if options.region
                       query_params[:stack] = options.stack if options.stack

                       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')
                     exit if input.downcase == "q"

                     user_token, user_email = nil
                     CLI::UI::Spinner.spin("Waiting for login") do |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 do
                         response = HTTParty.get("#{ANTIMONY_HOST}/api/cli_auth/resolve/#{client_secret}")
                         if response.code == 200 && response['code'] == 'unresolved'
                           sleep(poll_interval)
                         elsif response.code == 200 && response['code'] == 'resolved'
                           user_token = response["token"]
                           user_email = response["email"]
                           break
                         else
                           raise "Error: #{response.code}"
                         end
                         break if Time.now - start_time > timeout
                       end

                       user_netrc = Netrc.read
                       user_netrc["build.io"] = "#{user_email}", "#{user_token}"
                       user_netrc.save
                     end

                     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 = {}
                     query_params[:num] = options.num if options.num
                     query_params[:process] = options.process if options.process
                     query_params[:source] = options.source if options.source
                     query_params[:tail] = options.trace if options.trace
                     query_params[:tail] = options.tail if options.tail

                     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: 1_000_000) do |fragment|
                       puts fragment unless fragment.empty?
                     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
                   },
    }
  }
end