4 # arv tag add tag1 [tag2 ...] --object obj_uuid1 [--object obj_uuid2 ...]
5 # arv tag remove tag1 [tag2 ...] --object obj_uuid1 [--object obj_uuid2 ...]
6 # arv tag remove tag1 [tag2 ...] --all
10 "arv tag add tag1 [tag2 ...] --objects object_uuid1 [object_uuid2...]\n" +
11 "arv tag remove tag1 [tag2 ...] --objects object_uuid1 [object_uuid2...]\n" +
12 "arv tag remove --all\n"
15 def api_call(method, parameters:{}, request_body:{})
16 request_body[:api_token] = ENV['ARVADOS_API_TOKEN']
17 result = $client.execute(:api_method => method,
18 :parameters => parameters,
19 :body => request_body,
20 :authenticated => false)
23 results = JSON.parse result.body
24 rescue JSON::ParserError => e
25 abort "Failed to parse server response:\n" + e.to_s
29 abort "Error: #{results["errors"][0]}"
35 def tag_add(tag, obj_uuid)
36 return api_call($arvados.links.create,
41 :head_uuid => obj_uuid,
46 def tag_remove(tag, obj_uuids=nil)
47 # If we got a list of objects to untag, look up the uuids for the
48 # links that need to be deleted.
51 obj_uuids.each do |uuid|
52 link = api_call($arvados.links.list,
60 if link['items_available'] > 0
61 link_uuids.push link['items'][0]['uuid']
65 all_tag_links = api_call($arvados.links.list,
72 link_uuids = all_tag_links['items'].map { |obj| obj['uuid'] }
77 link_uuids.each do |uuid|
78 results.push api_call($arvados.links.delete, parameters:{ :uuid => uuid })
81 $stderr.puts "no tags found to remove"
85 'kind' => 'arvados#linkList',
86 'items_available' => results.length,
91 if RUBY_VERSION < '1.9.3' then
93 #{$0.gsub(/^\.\//,'')} requires Ruby version 1.9.3 or higher.
97 $arvados_api_version = ENV['ARVADOS_API_VERSION'] || 'v1'
98 $arvados_api_host = ENV['ARVADOS_API_HOST'] or
99 abort "#{$0}: fatal: ARVADOS_API_HOST environment variable not set."
100 $arvados_api_token = ENV['ARVADOS_API_TOKEN'] or
101 abort "#{$0}: fatal: ARVADOS_API_TOKEN environment variable not set."
102 $arvados_api_host_insecure = ENV['ARVADOS_API_HOST_INSECURE'] == 'yes'
106 require 'google/api_client'
113 #{$0}: fatal: some runtime dependencies are missing.
114 Try: gem install pp google-api-client json trollop
118 def debuglog(message, verbosity=1)
119 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if $debuglevel >= verbosity
123 def suppress_warnings
124 original_verbosity = $VERBOSE
127 $VERBOSE = original_verbosity
132 if $arvados_api_host_insecure or $arvados_api_host.match /local/
133 # You probably don't care about SSL certificate checks if you're
134 # testing with a dev server.
135 suppress_warnings { OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE }
138 class Google::APIClient
139 def discovery_document(api, version)
141 return @discovery_documents["#{api}:#{version}"] ||=
143 response = self.execute!(
144 :http_method => :get,
145 :uri => self.discovery_uri(api, version),
146 :authenticated => false
148 response.body.class == String ? JSON.parse(response.body) : response.body
153 global_opts = Trollop::options do
154 banner "arvados cli client"
155 opt :dry_run, "Don't actually do anything", :short => "-n"
156 opt :verbose, "Print some things on stderr", :short => "-v"
157 opt :uuid, "Return the UUIDs of the objects in the response, one per line (default)", :short => nil
158 opt :json, "Return the entire response received from the API server, as a JSON object", :short => "-j"
159 opt :human, "Return the response received from the API server, as a JSON object with whitespace added for human consumption", :short => "-h"
160 opt :pretty, "Synonym of --human", :short => nil
161 opt :yaml, "Return the response received from the API server, in YAML format", :short => "-y"
162 stop_on ['add', 'remove']
165 p = Trollop::Parser.new do
167 "Remove this tag from all objects under your ownership. Only valid with `tag remove'.",
170 "The UUID of an object to which this tag operation should be applied.",
176 $options = Trollop::with_standard_exception_handling p do
180 if $options[:all] and ARGV[0] != 'remove'
184 # Set up the API client.
186 $client ||= Google::APIClient.
187 new(:host => $arvados_api_host,
188 :application_name => File.split($0).last,
189 :application_version => $application_version.to_s)
190 $arvados = $client.discovered_api('arvados', $arvados_api_version)
197 $options[:object].each do |obj|
198 results.push(tag_add(tag, obj))
203 if $options[:all] then
204 results.push(tag_remove(tag))
206 results.push(tag_remove(tag, $options[:object]))
213 if global_opts[:human] or global_opts[:pretty] then
214 puts Oj.dump(results, :indent => 1)
215 elsif global_opts[:yaml] then
217 elsif global_opts[:json] then
218 puts Oj.dump(results)
222 if r["items"] and r["kind"].match /list$/i
223 r['items'].each do |i| puts i['uuid'] end
225 abort("Response did not include a uuid:\n" +
226 Oj.dump(r, :indent => 1) +