-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcouchtools
More file actions
executable file
·114 lines (86 loc) · 2.15 KB
/
couchtools
File metadata and controls
executable file
·114 lines (86 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/local/bin/ruby
$:.unshift File.dirname(__FILE__)
require 'couch_tools'
ACTIONS = {
:"save-design-doc" => :save_design_doc,
:"get-resource" => :get_resource,
:"get-dbs" => :get_dbs,
:"delete-resource" => :delete_resource,
:"put-resource" => :put_resource
}
def usage_message
name = File.basename(__FILE__)
<<EOS
# #{name}
#
# Useful for assembing couchdb design documents
# @see http://books.couchdb.org/relax/design-documents/design-documents
Options:
\t#{name} save-design-doc \\
\t\t--nosave \\
\t\tpath=/path/on/fs \\
\t\turl=http://url.of.the/db/_design/doc
\t#{name} get-resource \\
\t\turl=http://url.of.the/resource
\t#{name} get-dbs \\
\t\thost=http://url.of.host
\t#{name} delete-resource \\
\t\t--force \\
\t\turl=http://url.of.the/resource
\t#{name} put-resource \\
\t\turl=http://url.of.the/resource
\t\tdata={some: json-data}
EOS
end
def parse_args(argv)
ret = {}
ret[:action] = argv[0] && argv[0].to_sym
argv.each do |arg|
k,v = arg.split(/[=:\s]/,2)
v = (v == "true") if ["true","false"].include?(v)
ret[k.to_sym] = v.nil? ? k : v
end
ret
end
def main
args = parse_args(ARGV)
if args[:"-h"]
puts usage_message
exit
end
action = ACTIONS[args[:action]]
unless action
puts "Unknown action: %s"%args[:action] #"]
puts usage_message
exit
end
self.__send__(action, args)
end
def save_design_doc(args)
d = CouchTools::DesignDocument.new(args[:path], args[:url])
d.save unless args[:"--nosave"]
d.to_json
end
def get_resource(args)
d = CouchTools::Document.new(args[:url])
d.to_json
end
def delete_resource(args)
url = CouchTools::Url.parse(args[:url])
raise ArgumentError, "bad URL" unless url
res = url.doc ? CouchTools::Document.new(url) : CouchTools::Database.new(url)
if !args[:"--force"]
puts res.to_json
print "\nDelete %s [y/n]? "%args[:url]
str = STDIN.gets
return unless str && str.match(/^y/i)
end
res.delete
end
def put_resource(args)
url = CouchTools::Url.parse(args[:url])
raise ArgumentError, "bad URL" unless url
res = url.doc ? CouchTools::Document.new(url) : CouchTools::Database.new(url)
res.put(args[:data])
end
puts main