zillabyte-cli 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,180 +0,0 @@
1
- require "zillabyte/cli/base"
2
- require "zillabyte/cli/config"
3
- require "zillabyte/common"
4
- require "pty"
5
- require 'indentation'
6
- require 'open3'
7
- require 'securerandom'
8
- require 'colorize'
9
- require 'time_difference'
10
-
11
-
12
- # executes arbitrary input against apps
13
- #
14
- # HIDDEN
15
- class Zillabyte::Command::Executes < Zillabyte::Command::Base
16
-
17
- POLL_DELAY = 3
18
-
19
-
20
- # executes:exec ID INPUT_PARAMS [INPUT_PARAMS]
21
- #
22
- # executes an app against with the given parameters. INPUT_PARAMS
23
- # is expected to be in JSON format.
24
- #
25
- # --json # return the results as JSON
26
- # --async # returns immediately. Use executes:results to fetch results
27
- # --select FIELDS # returns only the fields in this list. useful for scripting
28
- #
29
- # Examples:
30
- #
31
- # $ zillabyte exec "web_screenshot", "{url: 'google.com'}"
32
- # $ zillabyte exec "web_screenshot", "{url: 'google.com'}", "{url: 'facebook.com'}"
33
- def exec
34
-
35
- # INIT
36
- app_id = options[:id] || shift_argument
37
- async = options[:async] || false
38
- select_field = options[:select]
39
-
40
- # Get the inputs
41
- input_params = []
42
- while ((ip = shift_argument()) != nil)
43
- input_params << JSON.parse(ip)
44
- end
45
-
46
- # Sanity
47
- error("At least one input parameter is required") if input_params.size == 0
48
- error("Unable to execute multiple requests synchronously. Add the --async flag or call with only one input_param") if input_params.size > 1 and !async
49
- error("The --select option and --async cannot be used at the same time") if async and select_field
50
-
51
- # Work with concrete ids or names
52
- if !(app_id =~ /^\d*$/)
53
- options[:is_name] = true
54
- end
55
-
56
- # Build the hash
57
- options[:input] = input_params
58
-
59
- # Make the request
60
- res = api.request(
61
- :expects => 200,
62
- :method => :post,
63
- :body => options.to_json,
64
- :path => "/flows/#{CGI.escape(app_id)}/executes"
65
- ).body
66
-
67
-
68
- if async
69
-
70
- # Async. Return right away...
71
- if res['status'] == 'success'
72
- if options[:display_type] == :json
73
- display res.to_s
74
- else
75
- # TODO:
76
- display res.to_s
77
- end
78
- else
79
- error res['error_message']
80
- end
81
-
82
- else
83
-
84
- # Synchronous... poll until done..
85
- execute_ids = res['execute_ids']
86
- while (true)
87
-
88
- res = get_results(app_id, execute_ids)
89
- if res['status'] == 'success'
90
-
91
- execute_ids.each do |execute_id|
92
- h = res['results'][execute_id]
93
- if h['status'] == 'complete'
94
- if select_field
95
- display(h['data'][select_field])
96
- else
97
- display(h.to_json)
98
- end
99
- execute_ids.delete(execute_id)
100
- end
101
- end
102
-
103
- else
104
- error(res.to_json)
105
- end
106
-
107
- if execute_ids.size == 0
108
- # success. done
109
- return
110
- else
111
- # still more. keep polling
112
- sleep(POLL_DELAY)
113
- end
114
-
115
- end
116
-
117
- end
118
-
119
- end
120
- alias_command "exec", "executes:exec"
121
- alias_command "execute", "executes:exec"
122
-
123
-
124
-
125
-
126
-
127
- # executes:results ID EXECUTE_ID [EXECUTE_ID]
128
- #
129
- # fetches the results associated with the given execute_id.
130
- #
131
- # --json # return the results as JSON
132
- #
133
- def results
134
-
135
- # INIT
136
- app_id = options[:id] || shift_argument
137
- execute_ids = []
138
- while ((eid = shift_argument()) != nil)
139
- execute_ids << eid
140
- end
141
-
142
- # Sanity
143
- error("at least one execute id parameter is required") if execute_ids.size == 0
144
-
145
- # Work with concrete ids or names
146
- if !(app_id =~ /^\d*$/)
147
- options[:is_name] = true
148
- end
149
-
150
- # Fetch the results...
151
- display( get_results(app_id, execute_ids).to_json )
152
-
153
-
154
-
155
- end
156
- alias_command "results", "executes:results"
157
-
158
-
159
-
160
- private
161
-
162
- def get_results(app_id, execute_ids)
163
-
164
- hash = {
165
- :execute_ids => execute_ids
166
- }
167
-
168
- res = api.request(
169
- :expects => 200,
170
- :method => :get,
171
- :body => hash.to_json,
172
- :path => "/flows/#{CGI.escape(app_id)}/executes"
173
- ).body
174
-
175
- return res
176
-
177
- end
178
-
179
-
180
- end