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.
16 if @sub == 'ls' or @sub == 'get' or @sub == 'put' or @sub == 'less' then
17 exec(`which wh#{@sub}`.strip + ' ' + ARGV.join(' '))
28 ENV['ARVADOS_API_VERSION'] ||= 'v1'
30 if not ENV.include?('ARVADOS_API_HOST') or not ENV.include?('ARVADOS_API_TOKEN') then
32 ARVADOS_API_HOST and ARVADOS_API_TOKEN need to be defined as environment variables.
38 require 'google/api_client'
44 require 'active_support/inflector'
48 Please install all required gems:
50 gem install google-api-client json trollop andand oj activesupport
55 ActiveSupport::Inflector.inflections do |inflect|
56 inflect.irregular 'specimen', 'specimens'
57 inflect.irregular 'human', 'humans'
62 original_verbosity = $VERBOSE
65 $VERBOSE = original_verbosity
70 # do this if you're testing with a dev server and you don't care about SSL certificate checks:
71 if ENV['ARVADOS_API_HOST_INSECURE']
72 suppress_warnings { OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE }
75 class Google::APIClient
76 def discovery_document(api, version)
78 return @discovery_documents["#{api}:#{version}"] ||= (begin
79 response = self.execute!(
81 :uri => self.discovery_uri(api, version),
82 :authenticated => false
84 response.body.class == String ? JSON.parse(response.body) : response.body
89 client = Google::APIClient.new(:host => ENV['ARVADOS_API_HOST'], :application_name => 'arvados-cli', :application_version => '1.0')
90 arvados = client.discovered_api('arvados', ENV['ARVADOS_API_VERSION'])
93 !!(s =~ /^(true|t|yes|y|1)$/i)
96 def parse_arguments(discovery_document)
97 resource_types = Array.new()
98 resource_types << '--help'
99 discovery_document["resources"].each do |k,v|
100 resource_types << k.singularize
103 global_opts = Trollop::options do
104 banner "arvados cli client"
105 opt :dry_run, "Don't actually do anything", :short => "-n"
106 opt :verbose, "Print some things on stderr", :short => "-v"
107 opt :uuid, "Return the UUIDs of the objects in the response, one per line (default)", :short => nil
108 opt :json, "Return the entire response received from the API server, as a JSON object", :short => "-j"
109 opt :human, "Return the response received from the API server, as a JSON object with whitespace added for human consumption", :short => "-h"
110 opt :pretty, "Synonym of --human", :short => nil
111 stop_on resource_types
115 resource_arg = ARGV.shift
116 if resource_types.include?(resource_arg) and resource_arg != '--help' then
118 # Now see if the method supplied exists
120 if discovery_document["resources"][resource_arg.pluralize]["methods"].include?(method) then
121 # method exists. Collect arguments.
122 discovered_params = discovery_document["resources"][resource_arg.pluralize]["methods"][method]["parameters"]
123 method_opts = Trollop::options do
124 discovered_params.each do |k,v|
126 opts[:type] = v["type"].to_sym if v.include?("type")
127 if [:datetime, :text, :object].index opts[:type]
128 opts[:type] = :string # else trollop bork
130 opts[:default] = v["default"] if v.include?("default")
131 opts[:default] = v["default"].to_i if opts[:type] == :integer
132 opts[:default] = to_boolean(v["default"]) if opts[:type] == :boolean
133 opts[:required] = true if v.include?("required") and v["required"]
135 description = ' ' + v["description"] if v.include?("description")
136 opt k.to_sym, description, opts
139 discovered_params.each do |k,v|
140 if v["type"] == "object" and method_opts.has_key? k
141 method_opts[k] = JSON.parse method_opts[k]
145 banner = "\nThis resource type supports the following methods:\n\n"
146 discovery_document["resources"][resource_arg.pluralize]["methods"].each do |k,v|
148 description = ' ' + v["description"] if v.include?("description")
149 banner += " #{sprintf("%20s",k)}#{description}\n"
155 if not method.nil? and method != '--help' then
156 Trollop::die "Unknown method #{method.to_s} for command #{resource_arg.to_s}"
164 banner = "\nThis Arvados instance supports the following resource types:\n\n"
165 discovery_document["resources"].each do |k,v|
167 if discovery_document["schemas"].include?(k.singularize.capitalize) and
168 discovery_document["schemas"][k.singularize.capitalize].include?('description') then
169 description = ' ' + discovery_document["schemas"][k.singularize.capitalize]["description"]
171 banner += " #{sprintf("%30s",k.singularize)}#{description}\n"
177 if not resource_arg.nil? and resource_arg != '--help' then
178 Trollop::die "Unknown resource type #{resource_arg.inspect}"
184 return resource_arg.pluralize, method, method_opts, global_opts, ARGV
187 controller, method, method_opts, global_opts, remaining_opts = parse_arguments(arvados.discovery_document)
189 api_method = 'arvados.' + controller + '.' + method
191 if global_opts[:dry_run]
192 if global_opts[:verbose]
193 $stderr.puts "#{api_method} #{method_opts.inspect}"
198 result = client.execute :api_method => eval(api_method), :parameters => { :api_token => ENV['ARVADOS_API_TOKEN'] }.merge(method_opts), :authenticated => false
199 results = JSON.parse result.body
201 if results["errors"] then
202 abort "Error: #{results["errors"][0]}"
205 if global_opts[:human] or global_opts[:pretty] then
206 puts Oj.dump(results, :indent => 1)
207 elsif global_opts[:json] then
208 puts Oj.dump(results)
209 elsif results["items"] and results["kind"].match /list$/i
210 results['items'].each do |i| puts i['uuid'] end