]> git.arvados.org - arvados.git/blob - sdk/cli/bin/arv-tag
Added 'add' and 'remove' functionality.
[arvados.git] / sdk / cli / bin / arv-tag
1 #! /usr/bin/env ruby
2
3 # arv tag usage:
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
7
8 def tag_add(tag, obj_uuid)
9   request_body = {}
10   request_body[:api_token] = ENV['ARVADOS_API_TOKEN']
11
12   return client.execute(:api_method => 'arvados.links.create',
13                         :parameters => {
14                           :name       => tag
15                           :link_class => :tag,
16                           :head_uuid  => obj_uuid,
17                         },
18                         :body => request_body,
19                         :authenticated => false)
20 end
21
22 def tag_remove(tag, obj_uuid=nil)
23   request_body = {}
24   request_body[:api_token] = ENV['ARVADOS_API_TOKEN']
25
26   params = { :name => tag, :link_class => :tag }
27   if obj_uuid then
28     params[:head_uuid] = obj_uuid
29   end
30
31   return client.execute(:api_method => 'arvados.links.destroy',
32                         :parameters => params,
33                         :body => request_body,
34                         :authenticated => false)
35 end
36
37 if RUBY_VERSION < '1.9.3' then
38   abort <<-EOS
39 #{$0.gsub(/^\.\//,'')} requires Ruby version 1.9.3 or higher.
40   EOS
41 end
42
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."
48
49 begin
50   require 'rubygems'
51   require 'google/api_client'
52   require 'json'
53   require 'pp'
54   require 'trollop'
55 rescue LoadError
56   abort <<-EOS
57 #{$0}: fatal: some runtime dependencies are missing.
58 Try: gem install pp google-api-client json trollop
59   EOS
60 end
61
62 def debuglog(message, verbosity=1)
63   $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if $debuglevel >= verbosity
64 end
65
66 module Kernel
67   def suppress_warnings
68     original_verbosity = $VERBOSE
69     $VERBOSE = nil
70     result = yield
71     $VERBOSE = original_verbosity
72     return result
73   end
74 end
75
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 }
80 end
81
82 class Google::APIClient
83   def discovery_document(api, version)
84     api = api.to_s
85     return @discovery_documents["#{api}:#{version}"] ||=
86       begin
87         response = self.execute!(
88                                  :http_method => :get,
89                                  :uri => self.discovery_uri(api, version),
90                                  :authenticated => false
91                                  )
92         response.body.class == String ? JSON.parse(response.body) : response.body
93       end
94   end
95 end
96
97 p = Trollop::Parser.new do
98   opt(:all,
99       "Remove this tag from all objects under your ownership. Only valid with `tag remove'.",
100       :short => :none)
101   opt(:object,
102       "The UUID of an object to which this tag operation should be applied.",
103       :type => :string,
104       :multi => true,
105       :short => :o)
106 end
107
108 $options = Trollop::with_standard_exception_handling p do
109   p.parse ARGV
110 end
111
112 if $options[:all] and ARGV[0] != 'remove'
113   abort "#{$0}: the --all option is only valid with the tag 'remove' command"
114 end
115
116 # Set up the API client.
117
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)
123
124 results = []
125 cmd = ARGV.shift
126 case cmd
127 when 'add'
128   ARGV.each do |tag|
129     $options[:object].each do |obj|
130       results.push(tag_add(tag, obj))
131     end
132   end
133 when 'remove'
134   ARGV.each do |tag|
135     if $options[:all] then
136       results.push(tag_remove(tag))
137     else
138       $options[:object].each do |obj|
139         results.push(tag_remove(tag, obj))
140       end
141     end
142   end
143 else
144   abort "unknown tag command #{cmd}"
145 end
146
147 if global_opts[:human] or global_opts[:pretty] then
148   puts Oj.dump(results, :indent => 1)
149 elsif global_opts[:yaml] then
150   puts results.to_yaml
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) +
158         "\n")
159 else
160   results.each do |result|
161     puts result['uuid']
162   end
163 end