mycnf 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (9) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +141 -0
  6. data/Rakefile +10 -0
  7. data/lib/mycnf.rb +95 -0
  8. data/mycnf.gemspec +19 -0
  9. metadata +79 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9a27cad6635542e4994d69a6809ee7b2c599cfdf
4
+ data.tar.gz: 2861b7c32f778e02bf6aa58688874e0d90c42b1a
5
+ SHA512:
6
+ metadata.gz: 2533937b64227d63ee63b99596153244a7039c13a1286cc9c7a7b40da9967920602432bba4080a36076de1863aa1e780be99c7fc6231ac84ac5532d5ab121c8b
7
+ data.tar.gz: 5673c28cce4465e895c8cbe850e0ee73b84fdafc5ba712229a83b7c5d872ebb8f2b51de367ed145024408d7978e2363600d7f5566e6f849604d21b4596030f99
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mycnf.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 studio3104
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,141 @@
1
+ # mycnf
2
+
3
+ parser of `my.cnf`
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mycnf'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mycnf
20
+
21
+ ## Usage
22
+
23
+ ###### my.cnf
24
+
25
+ ```sh
26
+ $ cat /etc/my.cnf.1
27
+ [client]
28
+ port = 3306
29
+ socket = /var/lib/mysql/mysql.sock
30
+
31
+ [mysql]
32
+ no_auto_rehash
33
+
34
+ [mysqld]
35
+ datadir = /var/lib/mysql
36
+ port = 3306
37
+ socket = /var/lib/mysql/mysql.sock
38
+ ```
39
+
40
+ ```sh
41
+ $ cat /etc/my.cnf.2
42
+ [client]
43
+ port = 3308
44
+ socket = /var/lib/mysql/mysql.sock
45
+
46
+ [mysql]
47
+ no_auto_rehash
48
+ safe-updates
49
+
50
+ [mysqld]
51
+ datadir = /var/lib/mysql
52
+ port = 3308
53
+ socket = /var/lib/mysql/mysql.sock
54
+ ```
55
+
56
+ #### parse
57
+
58
+ ```ruby
59
+ MyCnf.parse('/etc/my.cnf.1')
60
+ ```
61
+ ```ruby
62
+ {
63
+ client: {
64
+ port: 3306, socket: '/var/lib/mysql/mysql.sock'
65
+ },
66
+ mysql: {
67
+ no_auto_rehash: ''
68
+ },
69
+ mysqld: {
70
+ datadir: '/var/lib/mysql', port: 3306, socket: '/var/lib/mysql/mysql.sock'
71
+ }
72
+ }
73
+ ```
74
+
75
+ #### generate
76
+
77
+ ```ruby
78
+ MyCnf.generate({
79
+ client: {
80
+ port: 3306,
81
+ socket: '/var/lib/mysql/mysql.sock'
82
+ }
83
+ })
84
+ ```
85
+ ```ruby
86
+ [client]
87
+ port = 3306
88
+ socket = /var/lib/mysql/mysql.sock
89
+ ```
90
+
91
+ #### compare
92
+
93
+ ```ruby
94
+ MyCnf.compare(MyCnf.parse('/etc/my.cnf.1'), MyCnf.parse('/etc/my.cnf.2'))
95
+ MyCnf.compare_files('/etc/my.cnf.1', '/etc/my.cnf.2')
96
+ ```
97
+ ```ruby
98
+ {
99
+ client: {
100
+ port: [ 3306, 3308 ],
101
+ socket: [ '/var/lib/mysql/mysql.sock', '/var/lib/mysql/mysql.sock' ]
102
+ },
103
+ mysql: {
104
+ no_auto_rehash: [ '', '' ],
105
+ safe_updates: [ nil, '' ]
106
+ },
107
+ mysqld: {
108
+ datadir: [ '/var/lib/mysql', '/var/lib/mysql' ],
109
+ port: [ 3306, 3308 ],
110
+ socket: [ '/var/lib/mysql/mysql.sock', '/var/lib/mysql/mysql.sock' ]
111
+ }
112
+ }
113
+ ```
114
+
115
+ #### diff
116
+
117
+ ```ruby
118
+ MyCnf.diff(MyCnf.parse('/etc/my.cnf.1'), MyCnf.parse('/etc/my.cnf.2'))
119
+ MyCnf.diff_files('/etc/my.cnf.1', '/etc/my.cnf.2')
120
+ ```
121
+ ```ruby
122
+ {
123
+ client: {
124
+ port: [ 3306, 3308 ]
125
+ },
126
+ mysql: {
127
+ safe_updates: [ nil, '' ]
128
+ },
129
+ mysqld: {
130
+ port: [ 3306, 3308 ]
131
+ }
132
+ }
133
+ ```
134
+
135
+ ## Contributing
136
+
137
+ 1. Fork it ( https://github.com/studio3104/mycnf/fork )
138
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
139
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
140
+ 4. Push to the branch (`git push origin my-new-feature`)
141
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task :default => [:test]
5
+
6
+ Rake::TestTask.new do |test|
7
+ test.libs << 'test'
8
+ test.test_files = Dir[ 'test/**/test_*.rb' ]
9
+ test.verbose = true
10
+ end
data/lib/mycnf.rb ADDED
@@ -0,0 +1,95 @@
1
+ class MyCnf
2
+ def self.parse(file_path)
3
+ cnf = {}
4
+ section = nil
5
+ File.read(file_path).each_line do |line|
6
+ line.gsub!(/#.*/, '')
7
+ line.strip!
8
+
9
+ next if line == ''
10
+
11
+ if /\[(.+)\]/ =~ line
12
+ section = $1.to_sym
13
+ next
14
+ end
15
+
16
+ next if section.nil?
17
+ next unless /^([^\=]+)\=?(.*)/ =~ line
18
+ param, value = [ $1.strip, $2.strip ]
19
+ param = param.gsub('-', '_').to_sym
20
+
21
+ value = case value
22
+ when /^\d+$/; value.to_i
23
+ when /^true$/i; true
24
+ when /^false$/i; false
25
+ else value
26
+ end
27
+
28
+ cnf[section] ||= {}
29
+ cnf[section][param] = value
30
+ end
31
+ cnf
32
+ end
33
+
34
+ def self.generate(hash)
35
+ result = ''
36
+ hash.each do |section, params|
37
+ result << %Q[[#{section.to_s}]\n]
38
+ params.each do |param, value|
39
+ if value == ''
40
+ result << %Q[#{param}\n]
41
+ else
42
+ result << %Q[#{param} = #{value}\n]
43
+ end
44
+ end
45
+ result << %Q[\n]
46
+ end
47
+ result.chomp
48
+ end
49
+
50
+ def self.diff_files(*file_pathes)
51
+ cnfs = file_pathes.map { |path| parse(path) }
52
+ diff(*cnfs)
53
+ end
54
+
55
+ def self.diff(*cnf)
56
+ compare = compare(*cnf)
57
+ result = {}
58
+ compare.each do |section, params|
59
+ params.each do |param, values|
60
+ if values.uniq.size != 1
61
+ result[section] ||= {}
62
+ result[section][param] = values
63
+ end
64
+ end
65
+ end
66
+ result
67
+ end
68
+
69
+ def self.compare_files(*file_pathes)
70
+ cnfs = file_pathes.map { |path| parse(path) }
71
+ compare(*cnfs)
72
+ end
73
+
74
+ def self.compare(*cnf)
75
+ size = cnf.size
76
+ result = {}
77
+ cnf.each_with_index do |c, i|
78
+ result = _compare(c, size, i, result)
79
+ end
80
+ result
81
+ end
82
+
83
+ private
84
+
85
+ def self._compare(cnf, size, index, result)
86
+ cnf.each do |section, params|
87
+ result[section] ||= {}
88
+ params.each do |param, value|
89
+ result[section][param] ||= Array.new(size)
90
+ result[section][param][index] = value
91
+ end
92
+ end
93
+ result
94
+ end
95
+ end
data/mycnf.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'mycnf'
5
+ spec.version = '0.0.1'
6
+ spec.authors = ['studio3104']
7
+ spec.email = ['studio3104.com@gmail.com']
8
+
9
+ spec.summary = %q{parser of my.cnf}
10
+ spec.description = %q{parser of my.cnf}
11
+ spec.homepage = 'https://github.com/studio3104/mycnf'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_development_dependency 'bundler', '~> 1.8'
18
+ spec.add_development_dependency 'rake', '~> 10.0'
19
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mycnf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - studio3104
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: parser of my.cnf
42
+ email:
43
+ - studio3104.com@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/mycnf.rb
54
+ - mycnf.gemspec
55
+ homepage: https://github.com/studio3104/mycnf
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.2.2
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: parser of my.cnf
79
+ test_files: []