Skip to content

Commit 58a2299

Browse files
committed
(maint) Fix RuboCop offenses
1 parent 8b54621 commit 58a2299

File tree

4 files changed

+312
-320
lines changed

4 files changed

+312
-320
lines changed

files/rb_task_helper.rb

+51-53
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,59 @@
11
# frozen_string_literal: true
22

33
# Puppet Agent task helper
4-
module PuppetAgent
5-
module RbTaskHelper
6-
private
7-
8-
def error_result(error_type, error_message)
9-
{
10-
'_error' => {
11-
'msg' => error_message,
12-
'kind' => error_type,
13-
'details' => {},
14-
},
15-
}
16-
end
17-
18-
def puppet_bin_present?
19-
File.exist?(puppet_bin)
20-
end
21-
22-
# Returns the path to the Puppet agent executable
23-
def puppet_bin
24-
@puppet_bin ||= if Puppet.features.microsoft_windows?
25-
puppet_bin_windows
26-
else
27-
'/opt/puppetlabs/bin/puppet'
28-
end
29-
end
30-
31-
# Returns the path to the Puppet agent executable on Windows
32-
def puppet_bin_windows
33-
require 'win32/registry'
34-
35-
install_dir = begin
36-
Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Puppet Labs\Puppet') do |reg|
37-
# Rescue missing key
38-
dir = begin
39-
reg['RememberedInstallDir64']
40-
rescue StandardError
41-
''
42-
end
43-
# Both keys may exist, make sure the dir exists
44-
break dir if File.exist?(dir)
45-
46-
# Rescue missing key
47-
begin
48-
reg['RememberedInstallDir']
49-
rescue StandardError
50-
''
51-
end
4+
module PuppetAgent::RbTaskHelper
5+
private
6+
7+
def error_result(error_type, error_message)
8+
{
9+
'_error' => {
10+
'msg' => error_message,
11+
'kind' => error_type,
12+
'details' => {},
13+
},
14+
}
15+
end
16+
17+
def puppet_bin_present?
18+
File.exist?(puppet_bin)
19+
end
20+
21+
# Returns the path to the Puppet agent executable
22+
def puppet_bin
23+
@puppet_bin ||= if Puppet.features.microsoft_windows?
24+
puppet_bin_windows
25+
else
26+
'/opt/puppetlabs/bin/puppet'
27+
end
28+
end
29+
30+
# Returns the path to the Puppet agent executable on Windows
31+
def puppet_bin_windows
32+
require 'win32/registry'
33+
34+
install_dir = begin
35+
Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Puppet Labs\Puppet') do |reg|
36+
# Rescue missing key
37+
dir = begin
38+
reg['RememberedInstallDir64']
39+
rescue StandardError
40+
''
41+
end
42+
# Both keys may exist, make sure the dir exists
43+
break dir if File.exist?(dir)
44+
45+
# Rescue missing key
46+
begin
47+
reg['RememberedInstallDir']
48+
rescue StandardError
49+
''
5250
end
53-
rescue Win32::Registry::Error
54-
# Rescue missing registry path
55-
''
5651
end
52+
rescue Win32::Registry::Error
53+
# Rescue missing registry path
54+
''
55+
end
5756

58-
File.join(install_dir, 'bin', 'puppet.bat')
59-
end
57+
File.join(install_dir, 'bin', 'puppet.bat')
6058
end
6159
end

tasks/delete_local_filebucket.rb

+37-39
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,53 @@
99
require_relative File.join(params['_installdir'], 'puppet_agent', 'files', 'rb_task_helper.rb')
1010

1111
# Task to delete local filebucket
12-
module PuppetAgent
13-
class DeleteLocalFilebucket
14-
include PuppetAgent::RbTaskHelper
12+
class PuppetAgent::DeleteLocalFilebucket
13+
include PuppetAgent::RbTaskHelper
1514

16-
def initialize(force)
17-
@force = force
18-
end
15+
def initialize(force)
16+
@force = force
17+
end
1918

20-
def run
21-
unless puppet_bin_present?
22-
return error_result(
23-
'puppet_agent/no-puppet-bin-error',
24-
"Puppet executable '#{puppet_bin}' does not exist",
25-
)
26-
end
19+
def run
20+
unless puppet_bin_present?
21+
return error_result(
22+
'puppet_agent/no-puppet-bin-error',
23+
"Puppet executable '#{puppet_bin}' does not exist",
24+
)
25+
end
2726

28-
begin
29-
path = clientbucketdir
30-
if path && !path.empty? && (File.directory?(path) || force)
31-
FileUtils.rm_r(Dir.glob("#{path}/*"), secure: true, force: force)
32-
{ "success": true }
33-
else
34-
error_result(
35-
'puppet_agent/cannot-remove-error',
36-
"clientbucketdir: '#{path}' does not exist or is not a directory",
37-
)
38-
end
39-
rescue StandardError => e
27+
begin
28+
path = clientbucketdir
29+
if path && !path.empty? && (File.directory?(path) || force)
30+
FileUtils.rm_r(Dir.glob("#{path}/*"), secure: true, force: force)
31+
{ "success": true }
32+
else
4033
error_result(
41-
'puppet_agent/cannot-remove-error',
42-
"#{e.class}: #{e.message}",
43-
)
34+
'puppet_agent/cannot-remove-error',
35+
"clientbucketdir: '#{path}' does not exist or is not a directory",
36+
)
4437
end
38+
rescue StandardError => e
39+
error_result(
40+
'puppet_agent/cannot-remove-error',
41+
"#{e.class}: #{e.message}",
42+
)
4543
end
44+
end
4645

47-
private
48-
49-
def clientbucketdir
50-
options = {
51-
failonfail: false,
52-
override_locale: false,
53-
}
46+
private
5447

55-
command = "#{puppet_bin} config print clientbucketdir"
56-
Puppet::Util::Execution.execute(command, options).strip
57-
end
48+
def clientbucketdir
49+
options = {
50+
failonfail: false,
51+
override_locale: false,
52+
}
5853

59-
attr_reader :force
54+
command = "#{puppet_bin} config print clientbucketdir"
55+
Puppet::Util::Execution.execute(command, options).strip
6056
end
57+
58+
attr_reader :force
6159
end
6260

6361
if __FILE__ == $PROGRAM_NAME

tasks/facts_diff.rb

+44-46
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,61 @@
99
require_relative File.join(params['_installdir'], 'puppet_agent', 'files', 'rb_task_helper.rb')
1010

1111
# Task to run `puppet facts diff` command
12-
module PuppetAgent
13-
class FactsDiff
14-
include PuppetAgent::RbTaskHelper
12+
class PuppetAgent::FactsDiff
13+
include PuppetAgent::RbTaskHelper
1514

16-
def initialize(exclude)
17-
@exclude = exclude
18-
end
15+
def initialize(exclude)
16+
@exclude = exclude
17+
end
1918

20-
def run
21-
unless puppet_bin_present?
22-
return error_result(
23-
'puppet_agent/no-puppet-bin-error',
24-
"Puppet executable '#{puppet_bin}' does not exist",
25-
)
26-
end
19+
def run
20+
unless puppet_bin_present?
21+
return error_result(
22+
'puppet_agent/no-puppet-bin-error',
23+
"Puppet executable '#{puppet_bin}' does not exist",
24+
)
25+
end
2726

28-
unless suitable_puppet_version?
29-
return error_result(
30-
'puppet_agent/no-suitable-puppet-version',
31-
"puppet facts diff command is only available on puppet 6.x(>= 6.20.0), target has: #{Puppet.version}",
32-
)
33-
end
27+
unless suitable_puppet_version?
28+
return error_result(
29+
'puppet_agent/no-suitable-puppet-version',
30+
"puppet facts diff command is only available on puppet 6.x(>= 6.20.0), target has: #{Puppet.version}",
31+
)
32+
end
3433

35-
if @exclude && !exclude_parameter_supported?
36-
return error_result(
37-
'puppet_agent/exclude-parameter-not-supported',
38-
"exclude parameter is only available on puppet >= 6.22.0, target has: #{Puppet.version}",
39-
)
40-
end
34+
if @exclude && !exclude_parameter_supported?
35+
return error_result(
36+
'puppet_agent/exclude-parameter-not-supported',
37+
"exclude parameter is only available on puppet >= 6.22.0, target has: #{Puppet.version}",
38+
)
39+
end
4140

42-
options = {
43-
failonfail: true,
44-
override_locale: false
45-
}
41+
options = {
42+
failonfail: true,
43+
override_locale: false
44+
}
4645

47-
command = [puppet_bin, 'facts', 'diff']
48-
command << '--exclude' << "\"#{Regexp.new(@exclude)}\"" if @exclude && !@exclude.empty?
46+
command = [puppet_bin, 'facts', 'diff']
47+
command << '--exclude' << "\"#{Regexp.new(@exclude)}\"" if @exclude && !@exclude.empty?
4948

50-
run_result = Puppet::Util::Execution.execute(command, options)
49+
run_result = Puppet::Util::Execution.execute(command, options)
5150

52-
minified_run_result = run_result.delete("\n").delete(' ')
53-
minified_run_result == '{}' ? 'No differences found' : run_result
54-
end
51+
minified_run_result = run_result.delete("\n").delete(' ')
52+
minified_run_result == '{}' ? 'No differences found' : run_result
53+
end
5554

56-
private
55+
private
5756

58-
def suitable_puppet_version?
59-
puppet_version = Puppet.version
60-
Puppet::Util::Package.versioncmp(puppet_version, '6.20.0') >= 0 &&
61-
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
62-
end
57+
def suitable_puppet_version?
58+
puppet_version = Puppet.version
59+
Puppet::Util::Package.versioncmp(puppet_version, '6.20.0') >= 0 &&
60+
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
61+
end
6362

64-
def exclude_parameter_supported?
65-
puppet_version = Puppet.version
66-
Puppet::Util::Package.versioncmp(puppet_version, '6.22.0') >= 0 &&
67-
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
68-
end
63+
def exclude_parameter_supported?
64+
puppet_version = Puppet.version
65+
Puppet::Util::Package.versioncmp(puppet_version, '6.22.0') >= 0 &&
66+
Puppet::Util::Package.versioncmp(puppet_version, '7.0.0') < 0
6967
end
7068
end
7169

0 commit comments

Comments
 (0)