From e49066146ffeb91ce7c8e2100cdc4eca0ceb3a9b Mon Sep 17 00:00:00 2001 From: Micah Anderson Date: Tue, 26 Nov 2013 14:21:50 -0500 Subject: enable uploading a document to couch from a file (#4256) --- files/couch-doc-update | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/files/couch-doc-update b/files/couch-doc-update index 9101802..08f8cfe 100644 --- a/files/couch-doc-update +++ b/files/couch-doc-update @@ -15,7 +15,10 @@ require 'syslog' # # USAGE # -# couch-doc-update --host --db --id --data +# couch-doc-update --host --db --id --data [--file ] +# +# the content of will be merged with the data provided. +# If you only want the file content use --data '{}' # # EXAMPLE # @@ -59,22 +62,31 @@ def process_options db_name = nil doc_id = nil new_data = nil + filename = nil loop do case ARGV[0] when '--host' then ARGV.shift; host = ARGV.shift when '--db' then ARGV.shift; db_name = ARGV.shift when '--id' then ARGV.shift; doc_id = ARGV.shift when '--data' then ARGV.shift; new_data = ARGV.shift + when '--file' then ARGV.shift; filename = ARGV.shift when /^-/ then usage("Unknown option: #{ARGV[0].inspect}") else break end end usage("Missing required option") unless db_name && doc_id && new_data new_data = JSON.parse(new_data) + new_data.merge!(read_file(filename)) if filename db = CouchRest.database(connection_string(db_name, host)) return db, doc_id, new_data end +def read_file(filename) + data = JSON.parse( IO.read(filename) ) + # strip off _id and _rev to avoid conflicts + data.reject! {|k,v| k.start_with? '_'} +end + # # update document # @@ -131,7 +143,7 @@ end def usage(s) $stderr.puts(s) - $stderr.puts("Usage: #{File.basename($0)} --host --db --id --data ") + $stderr.puts("Usage: #{File.basename($0)} --host --db --id --data [--file ]") exit(2) end -- cgit v1.2.3