Skip to content

Commit 27b4eed

Browse files
Helentphoney
Helen
authored andcommitted
Update to enable Rubocop and contains Rubocop fixes in lib/ (#974)
* Rubocop fixes
1 parent dca37cf commit 27b4eed

File tree

22 files changed

+397
-835
lines changed

22 files changed

+397
-835
lines changed

.rubocop.yml

Lines changed: 85 additions & 497 deletions
Large diffs are not rendered by default.

.sync.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ appveyor.yml:
33
delete: true
44
spec/spec_helper.rb:
55
allow_deprecations: true
6+
.travis.yml:
7+
extras:
8+
- rvm: 2.1.9
9+
bundler_args: --without system_tests
10+
script: bundle exec rubocop lib

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@ matrix:
2828
- rvm: 2.1.9
2929
bundler_args: --without system_tests
3030
env: PUPPET_GEM_VERSION="~> 4.0"
31+
- rvm: 2.1.9
32+
bundler_args: --without system_tests
33+
script: bundle exec rubocop lib
3134
notifications:
3235
email: false

lib/facter/mysql_server_id.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
def get_mysql_id
2-
Facter.value(:macaddress).split(':')[2..-1].inject(0) { |total,value| (total << 6) + value.hex }
1+
def mysql_id_get
2+
Facter.value(:macaddress).split(':')[2..-1].reduce(0) { |total, value| (total << 6) + value.hex }
33
end
44

5-
Facter.add("mysql_server_id") do
5+
Facter.add('mysql_server_id') do
66
setcode do
7-
get_mysql_id rescue nil
7+
begin
8+
mysql_id_get
9+
rescue
10+
nil
11+
end
812
end
913
end

lib/facter/mysql_version.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
Facter.add("mysql_version") do
1+
Facter.add('mysql_version') do
22
setcode do
33
mysql_ver = Facter::Util::Resolution.exec('mysql --version')
4-
if mysql_ver
5-
mysql_ver.match(/\d+\.\d+\.\d+/)[0]
6-
end
4+
mysql_ver.match(%r{\d+\.\d+\.\d+})[0] if mysql_ver
75
end
86
end

lib/facter/mysqld_version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Facter.add("mysqld_version") do
1+
Facter.add('mysqld_version') do
22
setcode do
33
Facter::Util::Resolution.exec('mysqld -V 2>/dev/null')
44
end

lib/puppet/parser/functions/mysql_deepmerge.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
# Recursively merges two or more hashes together and returns the resulting hash.
12
module Puppet::Parser::Functions
2-
newfunction(:mysql_deepmerge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
3+
newfunction(:mysql_deepmerge, type: :rvalue, doc: <<-'ENDHEREDOC') do |args|
34
Recursively merges two or more hashes together and returns the resulting hash.
45
56
For example:
@@ -18,12 +19,12 @@ module Puppet::Parser::Functions
1819
ENDHEREDOC
1920

2021
if args.length < 2
21-
raise Puppet::ParseError, ("mysql_deepmerge(): wrong number of arguments (#{args.length}; must be at least 2)")
22+
raise Puppet::ParseError, "mysql_deepmerge(): wrong number of arguments (#{args.length}; must be at least 2)"
2223
end
2324

24-
result = Hash.new
25+
result = {}
2526
args.each do |arg|
26-
next if arg.is_a? String and arg.empty? # empty string is synonym for puppet's undef
27+
next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef
2728
# If the argument was not a hash, skip it.
2829
unless arg.is_a?(Hash)
2930
raise Puppet::ParseError, "mysql_deepmerge: unexpected argument type #{arg.class}, only expects hash arguments"
@@ -32,25 +33,25 @@ module Puppet::Parser::Functions
3233
# Now we have to traverse our hash assigning our non-hash values
3334
# to the matching keys in our result while following our hash values
3435
# and repeating the process.
35-
overlay( result, arg )
36+
overlay(result, arg)
3637
end
37-
return( result )
38+
return(result)
3839
end
3940
end
4041

41-
def has_normalized!(hash, key)
42-
return true if hash.has_key?( key )
43-
return false unless key.match(/-|_/)
44-
other_key = key.include?('-') ? key.gsub( '-', '_' ) : key.gsub( '_', '-' )
45-
return false unless hash.has_key?( other_key )
46-
hash[key] = hash.delete( other_key )
47-
return true;
42+
def normalized?(hash, key)
43+
return true if hash.key?(key)
44+
return false unless key =~ %r{-|_}
45+
other_key = key.include?('-') ? key.tr('-', '_') : key.tr('_', '-')
46+
return false unless hash.key?(other_key)
47+
hash[key] = hash.delete(other_key)
48+
true
4849
end
4950

50-
def overlay( hash1, hash2 )
51+
def overlay(hash1, hash2)
5152
hash2.each do |key, value|
52-
if(has_normalized!( hash1, key ) and value.is_a?(Hash) and hash1[key].is_a?(Hash))
53-
overlay( hash1[key], value )
53+
if normalized?(hash1, key) && value.is_a?(Hash) && hash1[key].is_a?(Hash)
54+
overlay(hash1[key], value)
5455
else
5556
hash1[key] = value
5657
end

lib/puppet/parser/functions/mysql_dirname.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
# Returns the dirname of a path.
12
module Puppet::Parser::Functions
2-
newfunction(:mysql_dirname, :type => :rvalue, :doc => <<-EOS
3+
newfunction(:mysql_dirname, type: :rvalue, doc: <<-EOS
34
Returns the dirname of a path.
45
EOS
5-
) do |arguments|
6+
) do |arguments|
67

7-
raise(Puppet::ParseError, "mysql_dirname(): Wrong number of arguments " +
8-
"given (#{arguments.size} for 1)") if arguments.size < 1
8+
if arguments.empty?
9+
raise(Puppet::ParseError, 'mysql_dirname(): Wrong number of arguments ' \
10+
"given (#{arguments.size} for 1)")
11+
end
912

1013
path = arguments[0]
1114
return File.dirname(path)
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
# hash a string as mysql's "PASSWORD()" function would do it
21
require 'digest/sha1'
3-
2+
# Returns the mysql password hash from the clear text password.
3+
# Hash a string as mysql's "PASSWORD()" function would do it
44
module Puppet::Parser::Functions
5-
newfunction(:mysql_password, :type => :rvalue, :doc => <<-EOS
5+
newfunction(:mysql_password, type: :rvalue, doc: <<-EOS
66
Returns the mysql password hash from the clear text password.
77
EOS
8-
) do |args|
8+
) do |args|
99

10-
raise(Puppet::ParseError, 'mysql_password(): Wrong number of arguments ' +
11-
"given (#{args.size} for 1)") if args.size != 1
10+
if args.size != 1
11+
raise(Puppet::ParseError, 'mysql_password(): Wrong number of arguments ' \
12+
"given (#{args.size} for 1)")
13+
end
1214

1315
return '' if args[0].empty?
14-
return args[0] if args[0] =~ /\*[A-F0-9]{40}$/
16+
return args[0] if args[0] =~ %r{\*[A-F0-9]{40}$}
1517
'*' + Digest::SHA1.hexdigest(Digest::SHA1.digest(args[0])).upcase
1618
end
1719
end
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1+
# When given a hash this function strips out all blank entries.
12
module Puppet::Parser::Functions
2-
newfunction(:mysql_strip_hash, :type => :rvalue, :arity => 1, :doc => <<-EOS
3-
TEMPORARY FUNCTION: EXPIRES 2014-03-10
4-
When given a hash this function strips out all blank entries.
3+
newfunction(:mysql_strip_hash, type: :rvalue, arity: 1, doc: <<-EOS
4+
TEMPORARY FUNCTION: EXPIRES 2014-03-10
5+
When given a hash this function strips out all blank entries.
56
EOS
6-
) do |args|
7+
) do |args|
78

89
hash = args[0]
910
unless hash.is_a?(Hash)
1011
raise(Puppet::ParseError, 'mysql_strip_hash(): Requires hash to work with')
1112
end
1213

1314
# Filter out all the top level blanks.
14-
hash.reject{|k,v| v == ''}.each do |k,v|
15-
if v.is_a?(Hash)
16-
v.reject!{|ki,vi| vi == '' }
17-
end
15+
hash.reject { |_k, v| v == '' }.each do |_k, v|
16+
v.reject! { |_ki, vi| vi == '' } if v.is_a?(Hash)
1817
end
19-
2018
end
2119
end

lib/puppet/provider/mysql.rb

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1+
# Puppet provider for mysql
12
class Puppet::Provider::Mysql < Puppet::Provider
2-
33
# Without initvars commands won't work.
44
initvars
55

66
# Make sure we find mysql commands on CentOS and FreeBSD
7-
ENV['PATH']=ENV['PATH'] + ':/usr/libexec:/usr/local/libexec:/usr/local/bin'
7+
ENV['PATH'] = ENV['PATH'] + ':/usr/libexec:/usr/local/libexec:/usr/local/bin'
88

9+
# rubocop:disable Style/HashSyntax
910
commands :mysql => 'mysql'
1011
commands :mysqld => 'mysqld'
1112
commands :mysqladmin => 'mysqladmin'
13+
# rubocop:enable Style/HashSyntax
1214

1315
# Optional defaults file
1416
def self.defaults_file
15-
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
16-
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
17-
else
18-
nil
19-
end
17+
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf" if File.file?("#{Facter.value(:root_home)}/.my.cnf")
2018
end
2119

2220
def self.mysqld_type
2321
# find the mysql "dialect" like mariadb / mysql etc.
24-
mysqld_version_string.scan(/mariadb/i) { return "mariadb" }
25-
mysqld_version_string.scan(/\s\(percona/i) { return "percona" }
26-
return "mysql"
22+
mysqld_version_string.scan(%r{mariadb}i) { return 'mariadb' }
23+
mysqld_version_string.scan(%r{\s\(percona}i) { return 'percona' }
24+
'mysql'
2725
end
2826

2927
def mysqld_type
3028
self.class.mysqld_type
3129
end
3230

3331
def self.mysqld_version_string
34-
# As the possibility of the mysqld being remote we need to allow the version string to be overridden, this can be done by facter.value as seen below. In the case that it has not been set and the facter value is nil we use the mysql -v command to ensure we report the correct version of mysql for later use cases.
32+
# As the possibility of the mysqld being remote we need to allow the version string to be overridden,
33+
# this can be done by facter.value as seen below. In the case that it has not been set and the facter
34+
# value is nil we use the mysql -v command to ensure we report the correct version of mysql for later use cases.
3535
@mysqld_version_string ||= Facter.value(:mysqld_version) || mysqld('-V')
3636
end
3737

@@ -43,7 +43,7 @@ def self.mysqld_version
4343
# note: be prepared for '5.7.6-rc-log' etc results
4444
# versioncmp detects 5.7.6-log to be newer then 5.7.6
4545
# this is why we need the trimming.
46-
mysqld_version_string.scan(/\d+\.\d+\.\d+/).first unless mysqld_version_string.nil?
46+
mysqld_version_string.scan(%r{\d+\.\d+\.\d+}).first unless mysqld_version_string.nil?
4747
end
4848

4949
def mysqld_version
@@ -77,39 +77,33 @@ def self.cmd_table(table)
7777
table_string = ''
7878

7979
# We can't escape *.* so special case this.
80-
if table == '*.*'
81-
table_string << '*.*'
82-
# Special case also for PROCEDURES
83-
elsif table.start_with?('PROCEDURE ')
84-
table_string << table.sub(/^PROCEDURE (.*)(\..*)/, 'PROCEDURE `\1`\2')
85-
else
86-
table_string << table.sub(/^(.*)(\..*)/, '`\1`\2')
87-
end
80+
table_string << if table == '*.*'
81+
'*.*'
82+
# Special case also for PROCEDURES
83+
elsif table.start_with?('PROCEDURE ')
84+
table.sub(%r{^PROCEDURE (.*)(\..*)}, 'PROCEDURE `\1`\2')
85+
else
86+
table.sub(%r{^(.*)(\..*)}, '`\1`\2')
87+
end
8888
table_string
8989
end
9090

9191
def self.cmd_privs(privileges)
92-
if privileges.include?('ALL')
93-
return 'ALL PRIVILEGES'
94-
else
95-
priv_string = ''
96-
privileges.each do |priv|
97-
priv_string << "#{priv}, "
98-
end
92+
return 'ALL PRIVILEGES' if privileges.include?('ALL')
93+
priv_string = ''
94+
privileges.each do |priv|
95+
priv_string << "#{priv}, "
9996
end
10097
# Remove trailing , from the last element.
101-
priv_string.sub(/, $/, '')
98+
priv_string.sub(%r{, $}, '')
10299
end
103100

104101
# Take in potential options and build up a query string with them.
105102
def self.cmd_options(options)
106103
option_string = ''
107104
options.each do |opt|
108-
if opt == 'GRANT'
109-
option_string << ' WITH GRANT OPTION'
110-
end
105+
option_string << ' WITH GRANT OPTION' if opt == 'GRANT'
111106
end
112107
option_string
113108
end
114-
115109
end

lib/puppet/provider/mysql_database/mysql.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mysql'))
2-
Puppet::Type.type(:mysql_database).provide(:mysql, :parent => Puppet::Provider::Mysql) do
2+
Puppet::Type.type(:mysql_database).provide(:mysql, parent: Puppet::Provider::Mysql) do
33
desc 'Manages MySQL databases.'
44

5-
commands :mysql => 'mysql'
5+
commands mysql: 'mysql'
66

77
def self.instances
8-
mysql([defaults_file, '-NBe', 'show databases'].compact).split("\n").collect do |name|
8+
mysql([defaults_file, '-NBe', 'show databases'].compact).split("\n").map do |name|
99
attributes = {}
1010
mysql([defaults_file, '-NBe', "show variables like '%_database'", name].compact).split("\n").each do |line|
11-
k,v = line.split(/\s/)
11+
k, v = line.split(%r{\s})
1212
attributes[k] = v
1313
end
14-
new(:name => name,
15-
:ensure => :present,
16-
:charset => attributes['character_set_database'],
17-
:collate => attributes['collation_database']
18-
)
14+
new(name: name,
15+
ensure: :present,
16+
charset: attributes['character_set_database'],
17+
collate: attributes['collation_database'])
1918
end
2019
end
2120

@@ -24,9 +23,8 @@ def self.instances
2423
def self.prefetch(resources)
2524
databases = instances
2625
resources.keys.each do |database|
27-
if provider = databases.find { |db| db.name == database }
28-
resources[database].provider = provider
29-
end
26+
provider = databases.find { |db| db.name == database }
27+
resources[database].provider = provider if provider
3028
end
3129
end
3230

@@ -56,13 +54,12 @@ def exists?
5654
def charset=(value)
5755
mysql([defaults_file, '-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}"].compact)
5856
@property_hash[:charset] = value
59-
charset == value ? (return true) : (return false)
57+
(charset == value) ? (return true) : (return false)
6058
end
6159

6260
def collate=(value)
6361
mysql([defaults_file, '-NBe', "alter database `#{resource[:name]}` COLLATE #{value}"].compact)
6462
@property_hash[:collate] = value
65-
collate == value ? (return true) : (return false)
63+
(collate == value) ? (return true) : (return false)
6664
end
67-
6865
end

0 commit comments

Comments
 (0)