Merge branch '8087-arv-cli-request-body-from-file' of https://github.com/wtsi-hgi...
[arvados.git] / sdk / cli / bin / arv
index 6811a4473fe0cc82571f60bb5b0cd52ce61c562a..da65b07bf0525b29047f2207543bc63e5fd2d851 100755 (executable)
@@ -638,13 +638,26 @@ if resource_body
   rescue JSON::ParserError => e
     _is_json = false
   end
-  if !_is_json
-    # if resource_body is not valid JSON, it should be a filename (or '-' for stdin)
-    if resource_body == '-'
-      _resource_body_file = $stdin
-    else
+  _is_readable_file = false
+  # if resource_body is not valid JSON, it should be a filename (or '-' for stdin)
+  if resource_body == '-'
+    _is_readable_file = true
+    _resource_body_file = $stdin
+  elsif File.readable? resource_body
+      _is_readable_file = true
       _resource_body_file = File.open(resource_body, 'r')
+  end
+  if _is_json and _is_readable_file
+    abort "Argument '#{resource_body}' specified for option '--#{resource_schema.to_sym}' is both valid JSON and a readable file, cannot continue (suggest renaming the file '#{resource_body}')."
+  elsif !_is_json and !_is_readable_file
+    if File.exists? resource_body
+      # specified file exists but is not readable
+      abort "File '#{resource_body}' specified for option '--#{resource_schema.to_sym}' exists but is not readable."
+    else
+      # specified file does not exist
+      abort "File '#{resource_body}' specified for option '--#{resource_schema.to_sym}' does not exist."
     end
+  elsif _is_readable_file
     resource_body = _resource_body_file.read()
     _resource_body_file.close()
   end