diff options
author | Micah Anderson <micah@riseup.net> | 2013-11-26 14:21:50 -0500 |
---|---|---|
committer | Micah Anderson <micah@riseup.net> | 2013-11-26 14:21:50 -0500 |
commit | e49066146ffeb91ce7c8e2100cdc4eca0ceb3a9b (patch) | |
tree | 305dc13c6deb5dd98453d9d87a0e22e1ea93a7d3 /files | |
parent | 4c7bc8b661250bb7fe1000ae1515507c5bf4beb1 (diff) |
enable uploading a document to couch from a file (#4256)
Diffstat (limited to 'files')
-rw-r--r-- | files/couch-doc-update | 16 |
1 files 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 <host> --db <db> --id <doc_id> --data <json> +# couch-doc-update --host <host> --db <db> --id <doc_id> --data <json> [--file <file>] +# +# the content of <file> 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 <host> --db <db> --id <doc_id> --data <json>") + $stderr.puts("Usage: #{File.basename($0)} --host <host> --db <db> --id <doc_id> --data <json> [--file <file>]") exit(2) end |