Merge branch '8784-dir-listings'
[arvados.git] / sdk / cli / bin / arv-tag
index ca12cb15abeaf8c3c5a172b61796d5e9792c8ffa..b1783bccf3f277cd705b5012b382182cb3e6b3ad 100755 (executable)
@@ -1,23 +1,32 @@
 #! /usr/bin/env ruby
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
 
 # arv tag usage:
 #   arv tag add tag1 [tag2 ...] --object obj_uuid1 [--object obj_uuid2 ...]
 #   arv tag remove tag1 [tag2 ...] --object obj_uuid1 [--object obj_uuid2 ...]
 #   arv tag remove tag1 [tag2 ...] --all
 
-def usage
-  abort "Usage:\n" +
-    "arv tag add tag1 [tag2 ...] --objects object_uuid1 [object_uuid2...]\n" +
-    "arv tag remove tag1 [tag2 ...] --objects object_uuid1 [object_uuid2...]\n" +
+def usage_string
+  return "\nUsage:\n" +
+    "arv tag add tag1 [tag2 ...] --object object_uuid1 [object_uuid2...]\n" +
+    "arv tag remove tag1 [tag2 ...] --object object_uuid1 [object_uuid2...]\n" +
     "arv tag remove --all\n"
 end
 
+def usage
+  abort usage_string
+end
+
 def api_call(method, parameters:{}, request_body:{})
-  request_body[:api_token] = ENV['ARVADOS_API_TOKEN']
   result = $client.execute(:api_method => method,
                            :parameters => parameters,
-                           :body => request_body,
-                           :authenticated => false)
+                           :body_object => request_body,
+                           :authenticated => false,
+                           :headers => {
+                             authorization: "OAuth2 #{ENV['ARVADOS_API_TOKEN']}",
+                           })
 
   begin
     results = JSON.parse result.body
@@ -81,11 +90,7 @@ def tag_remove(tag, obj_uuids=nil)
     $stderr.puts "no tags found to remove"
   end
 
-  return {
-    'kind' => 'arvados#linkList',
-    'items_available' => results.length,
-    'items' => results,
-  }
+  return results
 end
 
 if RUBY_VERSION < '1.9.3' then
@@ -99,7 +104,8 @@ $arvados_api_host = ENV['ARVADOS_API_HOST'] or
   abort "#{$0}: fatal: ARVADOS_API_HOST environment variable not set."
 $arvados_api_token = ENV['ARVADOS_API_TOKEN'] or
   abort "#{$0}: fatal: ARVADOS_API_TOKEN environment variable not set."
-$arvados_api_host_insecure = ENV['ARVADOS_API_HOST_INSECURE'] == 'yes'
+$arvados_api_host_insecure = %w(1 true yes).
+  include?((ENV['ARVADOS_API_HOST_INSECURE'] || "").downcase)
 
 begin
   require 'rubygems'
@@ -151,7 +157,8 @@ class Google::APIClient
 end
 
 global_opts = Trollop::options do
-  banner "arvados cli client"
+  banner usage_string
+  banner ""
   opt :dry_run, "Don't actually do anything", :short => "-n"
   opt :verbose, "Print some things on stderr", :short => "-v"
   opt :uuid, "Return the UUIDs of the objects in the response, one per line (default)", :short => nil
@@ -161,7 +168,7 @@ global_opts = Trollop::options do
   opt :yaml, "Return the response received from the API server, in YAML format", :short => "-y"
   stop_on ['add', 'remove']
 end
-  
+
 p = Trollop::Parser.new do
   opt(:all,
       "Remove this tag from all objects under your ownership. Only valid with `tag remove'.",
@@ -191,6 +198,11 @@ $arvados = $client.discovered_api('arvados', $arvados_api_version)
 
 results = []
 cmd = ARGV.shift
+
+if ARGV.empty?
+  usage
+end
+
 case cmd
 when 'add'
   ARGV.each do |tag|
@@ -201,9 +213,9 @@ when 'add'
 when 'remove'
   ARGV.each do |tag|
     if $options[:all] then
-      results.push(tag_remove(tag))
+      results.concat tag_remove(tag)
     else
-      results.push(tag_remove(tag, $options[:object]))
+      results.concat tag_remove(tag, $options[:object])
     end
   end
 else
@@ -218,10 +230,7 @@ elsif global_opts[:json] then
   puts Oj.dump(results)
 else
   results.each do |r|
-    next if r == nil
-    if r["items"] and r["kind"].match /list$/i
-      r['items'].each do |i| puts i['uuid'] end
-    elsif r['uuid'].nil?
+    if r['uuid'].nil?
       abort("Response did not include a uuid:\n" +
             Oj.dump(r, :indent => 1) +
             "\n")