5 # Ward Vandewege <ward@clinicalfuture.com>
7 if RUBY_VERSION < '1.9.3' then
9 #{$0.gsub(/^\.\//,'')} requires Ruby version 1.9.3 or higher.
13 ENV['ARVADOS_API_VERSION'] ||= 'v1'
15 if not ENV.include?('ARVADOS_API_HOST') or not ENV.include?('ARVADOS_API_TOKEN') then
17 ARVADOS_API_HOST and ARVADOS_API_TOKEN need to be defined as environment variables.
23 require 'google/api_client'
30 Please install all required gems:
41 original_verbosity = $VERBOSE
44 $VERBOSE = original_verbosity
49 # do this if you're testing with a dev server and you don't care about SSL certificate checks:
50 suppress_warnings { OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE }
52 class Google::APIClient
53 def discovery_document(api, version)
55 return @discovery_documents["#{api}:#{version}"] ||= (begin
56 response = self.execute!(
58 :uri => self.discovery_uri(api, version),
59 :authenticated => false
61 response.body.class == String ? JSON.parse(response.body) : response.body
66 client = Google::APIClient.new(:host => ENV['ARVADOS_API_HOST'], :application_name => 'wh-cli', :application_version => '1.0')
67 arvados = client.discovered_api('arvados', ENV['ARVADOS_API_VERSION'])
70 !!(s =~ /^(true|t|yes|y|1)$/i)
73 def parse_arguments(discovery_document)
74 sub_commands = Array.new()
75 sub_commands << '--help'
76 discovery_document["resources"].each do |k,v|
80 global_opts = Trollop::options do
81 banner "arvados cli client"
82 opt :dry_run, "Don't actually do anything", :short => "-n"
88 if sub_commands.include?(cmd) and cmd != '--help' then
90 # Now see if the method supplied exists
92 if discovery_document["resources"][cmd]["methods"].include?(method) then
93 # method exists. Collect arguments.
94 discovered_params = discovery_document["resources"][cmd]["methods"][method]["parameters"]
95 method_opts = Trollop::options do
96 discovered_params.each do |k,v|
98 opts[:type] = v["type"].to_sym if v.include?("type")
99 if [:datetime, :text, :object].index opts[:type]
100 opts[:type] = :string # else trollop bork
102 opts[:default] = v["default"] if v.include?("default")
103 opts[:default] = v["default"].to_i if opts[:type] == :integer
104 opts[:default] = to_boolean(v["default"]) if opts[:type] == :boolean
105 opts[:required] = true if v.include?("required") and v["required"]
107 description = ' ' + v["description"] if v.include?("description")
108 opt k.to_sym, description, opts
110 opt :json, "Return the raw json received from the API server", :short => "-j"
111 opt :jsonhuman, "Return the raw json received from the API server, formatted for human consumption", :short => "-h"
113 discovered_params.each do |k,v|
114 if v["type"] == "object" and method_opts.has_key? k
115 method_opts[k] = JSON.parse method_opts[k]
119 banner = "\nThis api command supports the following methods:\n\n"
120 discovery_document["resources"][cmd]["methods"].each do |k,v|
122 description = ' ' + v["description"] if v.include?("description")
123 banner += " #{sprintf("%20s",k)}#{description}\n"
129 if not method.nil? and method != '--help' then
130 Trollop::die "Unknown method #{method.to_s} for command #{cmd.to_s}"
138 banner = "\nThis Arvados cluster supports the following api commands:\n\n"
139 discovery_document["resources"].each do |k,v|
141 if discovery_document["schemas"].include?(k.singularize.capitalize) and
142 discovery_document["schemas"][k.singularize.capitalize].include?('description') then
143 description = ' ' + discovery_document["schemas"][k.singularize.capitalize]["description"]
145 banner += " #{sprintf("%30s",k.singularize)}#{description}\n"
151 if not cmd.nil? and cmd != '--help' then
152 Trollop::die "Unknown command #{cmd.inspect}"
158 return cmd, method, method_opts, ARGV
161 cmd, method, method_opts, remaining_opts = parse_arguments(arvados.discovery_document)
163 api_method = 'arvados.' + cmd + '.' + method
165 m_o = method_opts.reject {|key,value| key =~ /_given$|^json$|^jsonhuman$|^help$/ or value == nil }
167 result = client.execute :api_method => eval(api_method), :parameters => { :api_token => ENV['ARVADOS_API_TOKEN'] }.merge(m_o), :authenticated => false
168 results = JSON.parse result.body
170 if results["errors"] then
171 abort "Error: #{results["errors"][0]}"
174 if method_opts[:json] then
176 elsif method_opts[:jsonhuman] then
177 puts results.pretty_inspect()
178 elsif results["items"] and results["kind"].match /list$/i
179 results['items'].each do |i| puts i['uuid'] end