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
8 def tag_add(tag, obj_uuid)
10 request_body[:api_token] = ENV['ARVADOS_API_TOKEN']
12 return client.execute(:api_method => 'arvados.links.create',
16 :head_uuid => obj_uuid,
18 :body => request_body,
19 :authenticated => false)
22 def tag_remove(tag, obj_uuid=nil)
24 request_body[:api_token] = ENV['ARVADOS_API_TOKEN']
26 params = { :name => tag, :link_class => :tag }
28 params[:head_uuid] = obj_uuid
31 return client.execute(:api_method => 'arvados.links.destroy',
32 :parameters => params,
33 :body => request_body,
34 :authenticated => false)
37 if RUBY_VERSION < '1.9.3' then
39 #{$0.gsub(/^\.\//,'')} requires Ruby version 1.9.3 or higher.
43 $arvados_api_version = ENV['ARVADOS_API_VERSION'] || 'v1'
44 $arvados_api_host = ENV['ARVADOS_API_HOST'] or
45 abort "#{$0}: fatal: ARVADOS_API_HOST environment variable not set."
46 $arvados_api_token = ENV['ARVADOS_API_TOKEN'] or
47 abort "#{$0}: fatal: ARVADOS_API_TOKEN environment variable not set."
51 require 'google/api_client'
57 #{$0}: fatal: some runtime dependencies are missing.
58 Try: gem install pp google-api-client json trollop
62 def debuglog(message, verbosity=1)
63 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if $debuglevel >= verbosity
68 original_verbosity = $VERBOSE
71 $VERBOSE = original_verbosity
76 if $arvados_api_host.match /local/
77 # You probably don't care about SSL certificate checks if you're
78 # testing with a dev server.
79 suppress_warnings { OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE }
82 class Google::APIClient
83 def discovery_document(api, version)
85 return @discovery_documents["#{api}:#{version}"] ||=
87 response = self.execute!(
89 :uri => self.discovery_uri(api, version),
90 :authenticated => false
92 response.body.class == String ? JSON.parse(response.body) : response.body
97 p = Trollop::Parser.new do
99 "Remove this tag from all objects under your ownership. Only valid with `tag remove'.",
102 "The UUID of an object to which this tag operation should be applied.",
108 $options = Trollop::with_standard_exception_handling p do
112 if $options[:all] and ARGV[0] != 'remove'
113 abort "#{$0}: the --all option is only valid with the tag 'remove' command"
116 # Set up the API client.
118 $client ||= Google::APIClient.
119 new(:host => $arvados_api_host,
120 :application_name => File.split($0).last,
121 :application_version => $application_version.to_s)
122 $arvados = $client.discovered_api('arvados', $arvados_api_version)
129 $options[:object].each do |obj|
130 results.push(tag_add(tag, obj))
135 if $options[:all] then
136 results.push(tag_remove(tag))
138 $options[:object].each do |obj|
139 results.push(tag_remove(tag, obj))
144 abort "unknown tag command #{cmd}"
147 if global_opts[:human] or global_opts[:pretty] then
148 puts Oj.dump(results, :indent => 1)
149 elsif global_opts[:yaml] then
151 elsif global_opts[:json] then
152 puts Oj.dump(results)
153 elsif results["items"] and results["kind"].match /list$/i
154 results['items'].each do |i| puts i['uuid'] end
155 elsif results['uuid'].nil?
156 abort("Response did not include a uuid:\n" +
157 Oj.dump(results, :indent => 1) +
160 results.each do |result|