-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhd
executable file
·259 lines (213 loc) · 6.4 KB
/
hd
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
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
### hd -- HackDepot console client.
# Copyright (C) 2016 Artyom V. Poptsov <poptsov.artyom@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
### Commentary:
# Usage examples:
#
# Get list of all item IDs:
# ./hd -u $EMAIL -p $PASSWORD item
#
# Get information about an item by its ID:
# ./hd -u $EMAIL -p $PASSWORD item 562dee846d986e7c88000000
### Code:
require 'rest-client'
require 'json'
require 'optparse'
require 'colorize'
require 'yaml'
YAML_CONFIG_FILE = 'hd.yaml'
$options = {}
opt_parser = OptionParser.new do |opt|
opt.on("-u", "--user=USER", "Set user name.") do |user|
$options[:user] = user
end
opt.on("-p", "--password=PASSWORD", "Set password.") do |password|
$options[:password] = password
end
$options[:nocolor] = false
opt.on("-n", "--nocolor", "Disable ASCI color codes.") do
$options[:nocolor] = true
end
$options[:insecure] = false
opt.on("--insecure", "Disable SSL verification.") do
$options[:insecure] = true
end
end
opt_parser.parse!
# HackDepot instance domain name.
HACKDEPOT_DEFAULT_INSTANCE = 'hd.cadr.nntc.nnov.ru'
API_VERSION = "v1"
$hackdepot_instance = ""
$api_base_url = ""
### Helper methods
# Authenticate with a EMAIL and PASSWORD, return authentication
# response.
def authenticate(email, password)
request_body_map = {
:email => email,
:password => password
}
client = RestClient::Resource.new(
"#{$api_base_url}/auth/sign_in",
:verify_ssl => $options[:insecure] ?
OpenSSL::SSL::VERIFY_NONE
: OpenSSL::SSL::VERIFY_PEER)
client.post request_body_map.to_json,
{:content_type => 'application/json',
:accept => 'application/json'}
end
# Get PAGE of items registered in HackDepot. Use ACCESS_TOKEN, EXPIRY
# and CLIENT for authentication.
def get_all_items(user, access_token, expiry, client, page)
resource = RestClient::Resource.new(
"#{$api_base_url}/items?page=#{page}",
:verify_ssl => $options[:insecure] ?
OpenSSL::SSL::VERIFY_NONE
: OpenSSL::SSL::VERIFY_PEER)
resource.get({
:content_type => 'application/json',
:accept => 'application/json',
:access_token => access_token,
:token_type => "Bearer",
:expiry => expiry,
:client => client,
:uid => user
})
end
# Get information about item with ID. Use ACCESS_TOKEN, EXPIRY and
# CLIENT for authentication.
def get_item(user, id, access_token, expiry, client)
resource = RestClient::Resource.new(
"#{$api_base_url}/items/#{id}",
:verify_ssl => $options[:insecure] ?
OpenSSL::SSL::VERIFY_NONE
: OpenSSL::SSL::VERIFY_PEER)
resource.get({
:content_type => 'application/json',
:accept => 'application/json',
:access_token => access_token,
:token_type => "Bearer",
:expiry => expiry,
:client => client,
:uid => user
})
end
# Print IDs of items from an ARRAY to stdout.
def print_item_list(array)
max_title_len = array.inject(20) do |p, e|
e['title'].length > p ? e['title'].length : p
end
array.each { |elem|
sn = elem['serial']
printf "%s: %-#{max_title_len}s %s\n",
elem['id']['$oid'],
elem['title'],
if not sn == nil and not sn.empty?
" (S/N: #{sn})"
else
""
end
}
end
# Print an ITEM to stdout in a human-friendly manner.
def print_item(item)
if $options[:nocolor] == true
p = proc { |key, value|
printf "%s: %s", key, value
}
else
p = proc { |key, value|
print "#{key}: ".green
puts value
}
end
item.each_pair(&p)
end
###
# Handle "item" command.
def handle_item(config, args)
if config
user = config['HackDepot']['user']
password = config['HackDepot']['password']
end
user = $options[:user] ? $options[:user] : user
password = $options[:password] ? $options[:password] : password
user or raise "'user' should be specified."
password or raise "'password' should be specified"
auth_rsp = authenticate user, password
if not args[1] == nil
rsp = get_item user,
args[1],
auth_rsp.headers[:access_token],
auth_rsp.headers[:expiry],
auth_rsp.headers[:client]
print_item JSON.parse(rsp)
else
page = 0
while true
rsp = get_all_items user,
auth_rsp.headers[:access_token],
auth_rsp.headers[:expiry],
auth_rsp.headers[:client],
page
json = JSON.parse(rsp.body)
if json.length > 0
print_item_list json
page += 1
else
break
end
end
end
end
def print_help_and_exit
puts "\
Usage: hd command [args]
Options:
-u, --user=USER User name for authenticaton.
-p, --password=PASSWORD
Password for authentication.
--insecure Disable SSL verification.
Commands:
item Query HackDepot instance for items.
help Print this message and exit.
"
exit
end
def set_global_variables (config)
$hackdepot_instance = config['HackDepot']['instance'] ?
config['HackDepot']['instance']
: HACKDEPOT_DEFAULT_INSTANCE
$api_base_url = "https://#{$hackdepot_instance}/api/#{API_VERSION}"
end
### Entry point
def main
begin
config = YAML.load_file(YAML_CONFIG_FILE)
rescue
config = nil
end
set_global_variables config
case ARGV[0]
when "item"
handle_item config, ARGV
else
print_help_and_exit
end
end
main
### hd ends here.