gomon 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +6 -1
- data/README.md +11 -8
- data/lib/gomon/base.rb +10 -10
- data/lib/gomon/dump.rb +1 -1
- data/lib/gomon/options.rb +31 -4
- data/lib/gomon/restore.rb +3 -3
- data/lib/gomon/version.rb +1 -1
- data/spec/base_spec.rb +3 -2
- data/spec/dump_spec.rb +3 -2
- data/spec/options_spec.rb +32 -4
- data/spec/restore_spec.rb +4 -3
- metadata +1 -1
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Ruby wrappers around mongodb cli tools.
|
|
6
6
|
|
7
7
|
[![Build Status](https://travis-ci.org/novelys/gomon.png?branch=master)](https://travis-ci.org/novelys/gomon)
|
8
8
|
|
9
|
-
This gem depends on no other gems.
|
9
|
+
This gem depends on no other gems. It is tested agains many rubies: 1.8.7, 1.9.2, 1.9.3, jruby-18, jruby-19, rubinius-18, rubinius-19.
|
10
10
|
|
11
11
|
It assumes the presence of `mongorestore` and `mongodump` commands.
|
12
12
|
It was built around the latest mongodb tools at the moment of this writing, meaning version 2.2.2.
|
@@ -27,7 +27,7 @@ These two classes are used in the same way: first, instantiate the object, then,
|
|
27
27
|
|
28
28
|
### Example
|
29
29
|
|
30
|
-
> gd = Gomon::Dump.new uri: "mongodb://user:pass@host:12345/database", other: '
|
30
|
+
> gd = Gomon::Dump.new uri: "mongodb://user:pass@host:12345/database", other: 'journal'
|
31
31
|
> gd.cmd_string
|
32
32
|
=> "mongodump --host 'host' --port '12345' --username 'user' --password 'pass' --db 'database' --journal"
|
33
33
|
> gd.call
|
@@ -39,19 +39,21 @@ These two classes are used in the same way: first, instantiate the object, then,
|
|
39
39
|
|
40
40
|
### Options
|
41
41
|
|
42
|
-
The two classes accepts a hash of options when initializing, as in `gd = Gomon::Dumper.new(db: 'gem_test')`.
|
42
|
+
The two classes accepts a hash of options when initializing, as in `gd = Gomon::Dumper.new(db: 'gem_test')`. You can also supply this hash later by calling `gd.handle_options(opts)`, both have the same effect. When you add options this way, some specific rules apply :
|
43
43
|
|
44
|
-
* Any option accepted by `mongodump` or `mongorestore`. You should refer to the [MongoDB Manual](http://docs.mongodb.org/manual/reference/mongorestore/) for more on them;
|
45
|
-
* Singular options that have no value, as '--drop', can be supplied with the key `:other`;
|
46
44
|
* If the key `:uri` is present, its value will be parsed and split to retrieve the username, password, host, port, and database;
|
47
45
|
* Specific to `Gomon::Restore`: the option value for the key `:path` will be used as its last argument, as is.
|
48
46
|
|
49
|
-
|
47
|
+
Adding options using the following methods will **not** trigger the specific behaviours listed above.
|
50
48
|
|
49
|
+
* `add_option(key, value)`: adds the option. It will override previous ones.
|
50
|
+
* `add_options(hash)`: adds the options. It will override previous ones.
|
51
|
+
* `add_singular_option(string): adds the singular option. Duplicates will be removed.
|
52
|
+
* `add_singular_options(string_or_array): adds the singular options. Duplicates will be removed.
|
51
53
|
|
52
|
-
|
54
|
+
Options keys will always be casted as symbols, while values will always be casted as string.
|
53
55
|
|
54
|
-
|
56
|
+
No validation of options is done, so you should be careful about supplying only options accepted by `mongodump` or `mongorestore`. Refer to the [MongoDB Manual](http://docs.mongodb.org/manual/reference/mongorestore/) for more on the topic.
|
55
57
|
|
56
58
|
## Contributions
|
57
59
|
|
@@ -62,6 +64,7 @@ If you want to set options once the object is already created, you can use `add_
|
|
62
64
|
|
63
65
|
## Changelog
|
64
66
|
|
67
|
+
* `0.4`: Better options handling. Many ruby versions are now supported.
|
65
68
|
* `0.3`: Ruby 1.8 support
|
66
69
|
* `0.2`: Test coverage.
|
67
70
|
* `0.1`: First version. Provices basic functionnality for `Gomon::Dump` & `Gomon::Restore`
|
data/lib/gomon/base.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'active_support/ordered_hash'
|
2
1
|
require 'gomon/options'
|
3
2
|
require 'gomon/uri'
|
4
3
|
|
@@ -7,20 +6,21 @@ module Gomon
|
|
7
6
|
include Options
|
8
7
|
include Uri
|
9
8
|
|
9
|
+
attr_reader :options, :singular_options
|
10
|
+
|
10
11
|
def initialize(opts = {})
|
11
|
-
|
12
|
+
init_options :host => 'localhost', :port => '27017'
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
@options[:host] = 'localhost'
|
16
|
-
@options[:port] = '27017'
|
14
|
+
handle_options opts
|
15
|
+
end
|
17
16
|
|
18
|
-
|
17
|
+
# Handle options addition, with special cases
|
18
|
+
def handle_options(opts = {})
|
19
|
+
# Extract singular options
|
20
|
+
add_singular_options opts.delete(:other)
|
19
21
|
|
20
22
|
# Extract uri into options
|
21
|
-
|
22
|
-
add_options parse_uri(uri)
|
23
|
-
end
|
23
|
+
add_options parse_uri(opts.delete(:uri)) if opts[:uri]
|
24
24
|
|
25
25
|
# Keep track of other options
|
26
26
|
add_options opts
|
data/lib/gomon/dump.rb
CHANGED
data/lib/gomon/options.rb
CHANGED
@@ -2,14 +2,37 @@ require 'active_support/core_ext/enumerable'
|
|
2
2
|
|
3
3
|
module Gomon
|
4
4
|
module Options
|
5
|
+
# Initialize options ivars
|
6
|
+
def init_options(defaults = {})
|
7
|
+
@options = {}
|
8
|
+
@singular_options = []
|
9
|
+
|
10
|
+
add_options defaults
|
11
|
+
end
|
12
|
+
|
13
|
+
# Add a singular option
|
14
|
+
def add_singular_option(option)
|
15
|
+
@singular_options << option.to_s
|
16
|
+
@singular_options.uniq!
|
17
|
+
end
|
18
|
+
|
19
|
+
# Add many singular options
|
20
|
+
def add_singular_options(options)
|
21
|
+
return unless options
|
22
|
+
options = options.split(' ') if options.is_a?(String)
|
23
|
+
|
24
|
+
@singular_options += options.map(&:to_s)
|
25
|
+
@singular_options.uniq!
|
26
|
+
end
|
27
|
+
|
5
28
|
# Add a single options
|
6
29
|
def add_option(key, value)
|
7
|
-
|
30
|
+
@options[key.to_sym] = value.to_s
|
8
31
|
end
|
9
32
|
|
10
33
|
# Add many options
|
11
34
|
def add_options(opts = {})
|
12
|
-
|
35
|
+
opts.each { |key, value| add_option key, value }
|
13
36
|
end
|
14
37
|
|
15
38
|
# Returns the options string
|
@@ -19,11 +42,15 @@ module Gomon
|
|
19
42
|
|
20
43
|
# Returns options as an array
|
21
44
|
def options_as_array
|
45
|
+
# Valued options
|
22
46
|
ary = @options.each_with_object([]) do |(key, value), memo|
|
23
|
-
memo << "--#{key} '#{value}'"
|
47
|
+
memo << "--#{key.to_s} '#{value.to_s}'"
|
24
48
|
end
|
25
49
|
|
26
|
-
|
50
|
+
# Singular options
|
51
|
+
ary = @singular_options.each_with_object(ary) do |opt, memo|
|
52
|
+
memo << "--#{opt.to_s}"
|
53
|
+
end
|
27
54
|
|
28
55
|
ary
|
29
56
|
end
|
data/lib/gomon/restore.rb
CHANGED
@@ -2,7 +2,7 @@ require 'gomon/base'
|
|
2
2
|
|
3
3
|
module Gomon
|
4
4
|
class Restore < Base
|
5
|
-
attr_reader :tool
|
5
|
+
attr_reader :tool, :path
|
6
6
|
|
7
7
|
def initialize(opts = {})
|
8
8
|
@path = opts.delete(:path) || 'dump/*'
|
@@ -12,12 +12,12 @@ module Gomon
|
|
12
12
|
|
13
13
|
# String to execute
|
14
14
|
def cmd_string
|
15
|
-
"#{super} '#{
|
15
|
+
"#{super} '#{path}'"
|
16
16
|
end
|
17
17
|
|
18
18
|
# Hook to execute before restoring
|
19
19
|
def before_cmd
|
20
|
-
puts "Restoring from dir `#{
|
20
|
+
puts "Restoring from dir `#{path}` into `#{options[:db]}` at #{options[:host]}..."
|
21
21
|
puts "Executing: #{cmd_string}"
|
22
22
|
end
|
23
23
|
|
data/lib/gomon/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -44,11 +44,12 @@ describe "Base" do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
context 'initialized with other' do
|
47
|
-
subject { Gomon::Base.new :other => "
|
47
|
+
subject { Gomon::Base.new :other => "first third" }
|
48
48
|
let(:options) { subject.instance_variable_get :@singular_options }
|
49
49
|
|
50
50
|
it 'should have singular options' do
|
51
|
-
expect(options).to
|
51
|
+
expect(options).to include 'first'
|
52
|
+
expect(options).to include 'third'
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
data/spec/dump_spec.rb
CHANGED
@@ -5,6 +5,7 @@ describe "Dump" do
|
|
5
5
|
subject { Gomon::Dump.new :username => 'john', :password => 'mcclane', :db => 'die_hard'}
|
6
6
|
let(:tool) { subject.instance_variable_get :@tool }
|
7
7
|
let(:options) { subject.instance_variable_get :@options }
|
8
|
+
let(:singular) { subject.instance_variable_get :@singular_options }
|
8
9
|
|
9
10
|
it 'should have a toolname' do
|
10
11
|
expect(tool).to eq 'mongodump'
|
@@ -12,7 +13,7 @@ describe "Dump" do
|
|
12
13
|
|
13
14
|
it 'should have a command string' do
|
14
15
|
cmd = "mongodump"
|
15
|
-
options.each { |k, v|
|
16
|
-
expect(subject.cmd_string).to
|
16
|
+
options.each { |k, v| expect(subject.cmd_string).to include "--#{k} '#{v}'" }
|
17
|
+
singular.each { |k| expect(subject.cmd_string).to include "--#{k}" }
|
17
18
|
end
|
18
19
|
end
|
data/spec/options_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe "Options" do
|
|
5
5
|
class TestObject
|
6
6
|
include Gomon::Options
|
7
7
|
|
8
|
-
def initialize;
|
8
|
+
def initialize; init_options; end
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -24,8 +24,24 @@ describe "Options" do
|
|
24
24
|
expect(options[:key2]).to eq 'value2'
|
25
25
|
end
|
26
26
|
|
27
|
-
it 'should
|
28
|
-
|
27
|
+
it 'should add options with symbols as keys' do
|
28
|
+
subject.add_options 'key' => 'value'
|
29
|
+
expect(options[:key]).to eq 'value'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should add one singular option' do
|
33
|
+
subject.add_singular_option 'hank'
|
34
|
+
expect(singular).to include 'hank'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should add several singular options given as string' do
|
38
|
+
subject.add_singular_options 'rocky balboa'
|
39
|
+
expect(singular).to include 'rocky', 'balboa'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should add several singular options given as array' do
|
43
|
+
subject.add_singular_options ['rocky', 'balboa']
|
44
|
+
expect(singular).to include 'rocky', 'balboa'
|
29
45
|
end
|
30
46
|
|
31
47
|
it 'can override previous keys' do
|
@@ -35,8 +51,20 @@ describe "Options" do
|
|
35
51
|
expect(options[:key]).to eq 'new_value'
|
36
52
|
end
|
37
53
|
|
54
|
+
it 'should not add many times the same singular option' do
|
55
|
+
subject.add_singular_options ['rocky', 'rocky']
|
56
|
+
expect(singular).to include 'rocky'
|
57
|
+
expect(singular.size).to eq 1
|
58
|
+
end
|
59
|
+
|
38
60
|
it 'can merge options into a string' do
|
39
61
|
subject.add_options :key1 => 'value1', :key2 => 'value2'
|
40
|
-
|
62
|
+
subject.add_singular_options %w(john jim jones)
|
63
|
+
|
64
|
+
expect(subject.cli_options).to include "--key1 'value1'"
|
65
|
+
expect(subject.cli_options).to include "--key2 'value2'"
|
66
|
+
expect(subject.cli_options).to include "--john"
|
67
|
+
expect(subject.cli_options).to include "--jim"
|
68
|
+
expect(subject.cli_options).to include "--jones"
|
41
69
|
end
|
42
70
|
end
|
data/spec/restore_spec.rb
CHANGED
@@ -6,6 +6,7 @@ describe "Restore" do
|
|
6
6
|
let(:tool) { subject.instance_variable_get :@tool }
|
7
7
|
let(:path) { subject.instance_variable_get :@path }
|
8
8
|
let(:options) { subject.instance_variable_get :@options }
|
9
|
+
let(:singular) { subject.instance_variable_get :@singular_options }
|
9
10
|
|
10
11
|
it 'should have a toolname' do
|
11
12
|
expect(tool).to eq 'mongorestore'
|
@@ -21,8 +22,8 @@ describe "Restore" do
|
|
21
22
|
|
22
23
|
it 'should have a command string' do
|
23
24
|
cmd = "mongorestore"
|
24
|
-
options.each { |k, v|
|
25
|
-
|
26
|
-
expect(subject.cmd_string).to
|
25
|
+
options.each { |k, v| expect(subject.cmd_string).to include "--#{k} '#{v}'" }
|
26
|
+
singular.each { |k| expect(subject.cmd_string).to include "--#{k}" }
|
27
|
+
expect(subject.cmd_string).to satisfy { |str| str.end_with? "'#{path}'" }
|
27
28
|
end
|
28
29
|
end
|