gratan 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e05682c907c1fe62bbe627e89029656293f73cee
4
- data.tar.gz: 4fc1a578ad94f8acbf50bfc006a2187bffb53ec8
3
+ metadata.gz: 65d0a9808bf9dba9899060c4c7e220beaf8c0d5a
4
+ data.tar.gz: 355bea40e2a79d9c907ac7ffdc162cda0b2ab5b7
5
5
  SHA512:
6
- metadata.gz: a3c03ecaa3d3f4d4b17778e1ea324e62ad50eaccad230fb5bacea75b4b9b6e80cabd7e904138db0f227120685ebe2565c01c8c41587921c4b6b592b30f7f239c
7
- data.tar.gz: e2b836e3061aeeb482a7e879578670527777b7dd89441020f5b89a4d6503c20f2234a4d28cee1047db390c2e49e857b08502ccfa5198bada0b3b8556cf5e114e
6
+ metadata.gz: 58003b9ec0af4f3000c3df6f3c7eff440cf4c0672fac07fc486e8afc0b5eb9864fb865c15fd53991beb7e8e4ad0cc2d317d4c2e9caf902d7f2dfccdc73f16eb8
7
+ data.tar.gz: 8600de836683f357bdd0faf21e5a5ac6bd888c40767fa2413b39b1b1c5b387be177047c081e980516e2e998ba4445bc8d2f92768910916978bbd456b128378cd
data/bin/gratan CHANGED
@@ -38,6 +38,7 @@ ARGV.options do |opt|
38
38
  opt.on('-e', '--export') { mode = :export }
39
39
  opt.on('' , '--with-identifier') { options[:with_identifier] = true }
40
40
  opt.on('' , '--split') { split = true }
41
+ opt.on('' , '--chunk-by-user') { options[:chunk_by_user] = true }
41
42
  opt.on('-o', '--output FILE') {|v| output_file = v }
42
43
  opt.on('' , '--ignore-user REGEXP') {|v| options[:ignore_user] = Regexp.new(v) }
43
44
  opt.on('' , '--target-user REGEXP') {|v| options[:target_user] = Regexp.new(v) }
data/gratan.gemspec CHANGED
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'mysql2'
22
- spec.add_dependency "term-ansicolor"
22
+ spec.add_dependency 'term-ansicolor'
23
+ spec.add_dependency 'deep_merge'
23
24
  spec.add_development_dependency 'bundler'
24
25
  spec.add_development_dependency 'rake', '~> 10.0'
25
26
  spec.add_development_dependency 'rspec', '>= 3.0.0'
data/lib/gratan.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require 'logger'
2
- require 'mysql2'
3
2
  require 'singleton'
4
- require 'term/ansicolor'
5
3
  require 'time'
6
4
 
5
+ require 'deep_merge'
6
+ require 'mysql2'
7
+ require 'term/ansicolor'
8
+
7
9
  module Gratan; end
8
10
  require 'gratan/logger'
9
11
  require 'gratan/client'
data/lib/gratan/client.rb CHANGED
@@ -10,6 +10,10 @@ class Gratan::Client
10
10
  options = @options.merge(options)
11
11
  exported = Gratan::Exporter.export(@driver, options)
12
12
 
13
+ if options[:chunk_by_user]
14
+ exported = chunk_by_user(exported)
15
+ end
16
+
13
17
  if block_given?
14
18
  exported.sort_by {|user_host, attrs|
15
19
  user_host[0].empty? ? 'root' : user_host[0]
@@ -22,11 +26,33 @@ class Gratan::Client
22
26
  yield(user, dsl)
23
27
  }
24
28
  else
25
- exported = Gratan::Exporter.export(@driver, options)
26
29
  Gratan::DSL.convert(exported, options)
27
30
  end
28
31
  end
29
32
 
33
+ def chunk_by_user(exported)
34
+ chunked = {}
35
+
36
+ exported.sort_by {|user_host, attrs|
37
+ user_host[0]
38
+ }.chunk {|user_host, attrs|
39
+ user_host[0]
40
+ }.each {|user, grants|
41
+ merged_attrs = {}
42
+ hosts = []
43
+
44
+ grants.each do |user_host, attrs|
45
+ hosts << user_host[1]
46
+ merged_attrs.deep_merge!(attrs)
47
+ end
48
+
49
+ user_host = [user, hosts.sort]
50
+ chunked[user_host] = merged_attrs
51
+ }
52
+
53
+ chunked
54
+ end
55
+
30
56
  def apply(file, options = {})
31
57
  options = @options.merge(options)
32
58
 
@@ -1,3 +1,3 @@
1
1
  module Gratan
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.5'
3
3
  end
@@ -0,0 +1,62 @@
1
+ describe 'Gratan::Client#export' do
2
+ context 'when chunked by user' do
3
+ subject { client(chunk_by_user: true) }
4
+
5
+ before do
6
+ apply(subject) do
7
+ <<-RUBY
8
+ user "scott", "%" do
9
+ on "*.*" do
10
+ grant "USAGE"
11
+ end
12
+
13
+ on "test.*" do
14
+ grant "SELECT"
15
+ end
16
+
17
+ on "test3.*" do
18
+ grant "UPDATE"
19
+ end
20
+ end
21
+
22
+ user "scott", "localhost" do
23
+ on "*.*" do
24
+ grant "USAGE"
25
+ end
26
+
27
+ on "test2.*" do
28
+ grant "INSERT"
29
+ end
30
+
31
+ on "test3.*" do
32
+ grant "DELETE"
33
+ end
34
+ end
35
+ RUBY
36
+ end
37
+ end
38
+
39
+ it do
40
+ expect(subject.export.strip).to eq <<-RUBY.strip
41
+ user "scott", ["%", "localhost"] do
42
+ on "*.*" do
43
+ grant "USAGE"
44
+ end
45
+
46
+ on "test.*" do
47
+ grant "SELECT"
48
+ end
49
+
50
+ on "test3.*" do
51
+ grant "UPDATE"
52
+ grant "DELETE"
53
+ end
54
+
55
+ on "test2.*" do
56
+ grant "INSERT"
57
+ end
58
+ end
59
+ RUBY
60
+ end
61
+ end
62
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gratan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-09 00:00:00.000000000 Z
11
+ date: 2014-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: deep_merge
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -161,6 +175,7 @@ files:
161
175
  - spec/drop/drop_user_2_spec.rb
162
176
  - spec/drop/drop_user_spec.rb
163
177
  - spec/drop/expire_user_spec.rb
178
+ - spec/export/export_chunk_spec.rb
164
179
  - spec/export/export_spec.rb
165
180
  - spec/misc/misc_spec.rb
166
181
  - spec/misc/require_spec.rb
@@ -185,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
200
  version: '0'
186
201
  requirements: []
187
202
  rubyforge_project:
188
- rubygems_version: 2.4.1
203
+ rubygems_version: 2.0.14
189
204
  signing_key:
190
205
  specification_version: 4
191
206
  summary: Gratan is a tool to manage MySQL permissions using Ruby DSL.
@@ -208,6 +223,7 @@ test_files:
208
223
  - spec/drop/drop_user_2_spec.rb
209
224
  - spec/drop/drop_user_spec.rb
210
225
  - spec/drop/expire_user_spec.rb
226
+ - spec/export/export_chunk_spec.rb
211
227
  - spec/export/export_spec.rb
212
228
  - spec/misc/misc_spec.rb
213
229
  - spec/misc/require_spec.rb