summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-10-16 10:35:35 -0700
committerTim Smith <tsmith84@gmail.com>2020-10-16 10:35:35 -0700
commit04a23bb2d1b3243f09e7c63f2c755fbaefcd5d87 (patch)
treef3edd4a0f410d589ee10f30d4e4c873cd9a8a7ad
parentb4e7913972e70b42e632a33930417fb37d2bddd1 (diff)
downloadchef-04a23bb2d1b3243f09e7c63f2c755fbaefcd5d87.tar.gz
Avoid using complex regexes when we can use include?
The ifconfig part needs a complete rewrite, but this is better. At least it's not doing a regex twice for each thing it wants to match per line. It still does 5 regexes even if it matches on the first. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/provider/ifconfig.rb14
-rw-r--r--lib/chef/provider/mount/mount.rb2
-rw-r--r--lib/chef/provider/package/apt.rb2
-rw-r--r--lib/chef/provider/user/aix.rb2
-rw-r--r--lib/chef/resource/hostname.rb2
-rw-r--r--lib/chef/util/dsc/local_configuration_manager.rb2
6 files changed, 12 insertions, 12 deletions
diff --git a/lib/chef/provider/ifconfig.rb b/lib/chef/provider/ifconfig.rb
index 9ae2c38137..d08564e75d 100644
--- a/lib/chef/provider/ifconfig.rb
+++ b/lib/chef/provider/ifconfig.rb
@@ -126,18 +126,18 @@ class Chef
elsif line.match(addr_regex)[3] == ""
@int_name = line.match(addr_regex)[1]
@interfaces[@int_name] = {}
- @interfaces[@int_name]["mtu"] = (line =~ /mtu (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /mtu/ && @interfaces[@int_name]["mtu"].nil?
+ @interfaces[@int_name]["mtu"] = (line =~ /mtu (\S+)/ ? Regexp.last_match(1) : "nil") if line.include?("mtu") && @interfaces[@int_name]["mtu"].nil?
else
@int_name = "#{line.match(addr_regex)[1]}:#{line.match(addr_regex)[3]}"
@interfaces[@int_name] = {}
- @interfaces[@int_name]["mtu"] = (line =~ /mtu (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /mtu/ && @interfaces[@int_name]["mtu"].nil?
+ @interfaces[@int_name]["mtu"] = (line =~ /mtu (\S+)/ ? Regexp.last_match(1) : "nil") if line.include?("mtu") && @interfaces[@int_name]["mtu"].nil?
end
else
- @interfaces[@int_name]["inet_addr"] = (line =~ /inet (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /inet/ && @interfaces[@int_name]["inet_addr"].nil?
- @interfaces[@int_name]["bcast"] = (line =~ /broadcast (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /broadcast/ && @interfaces[@int_name]["bcast"].nil?
- @interfaces[@int_name]["mask"] = (line =~ /netmask (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /netmask/ && @interfaces[@int_name]["mask"].nil?
- @interfaces[@int_name]["hwaddr"] = (line =~ /ether (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /ether/ && @interfaces[@int_name]["hwaddr"].nil?
- @interfaces[@int_name]["metric"] = (line =~ /Metric:(\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /Metric:/ && @interfaces[@int_name]["metric"].nil?
+ @interfaces[@int_name]["inet_addr"] = (line =~ /inet (\S+)/ ? Regexp.last_match(1) : "nil") if line.include?("inet") && @interfaces[@int_name]["inet_addr"].nil?
+ @interfaces[@int_name]["bcast"] = (line =~ /broadcast (\S+)/ ? Regexp.last_match(1) : "nil") if line.include?("broadcast") && @interfaces[@int_name]["bcast"].nil?
+ @interfaces[@int_name]["mask"] = (line =~ /netmask (\S+)/ ? Regexp.last_match(1) : "nil") if line.include?("netmask") && @interfaces[@int_name]["mask"].nil?
+ @interfaces[@int_name]["hwaddr"] = (line =~ /ether (\S+)/ ? Regexp.last_match(1) : "nil") if line.include?("ether") && @interfaces[@int_name]["hwaddr"].nil?
+ @interfaces[@int_name]["metric"] = (line =~ /Metric:(\S+)/ ? Regexp.last_match(1) : "nil") if line.include?("Metric:") && @interfaces[@int_name]["metric"].nil?
end
next unless @interfaces.key?(new_resource.device)
diff --git a/lib/chef/provider/mount/mount.rb b/lib/chef/provider/mount/mount.rb
index 6e39f6549b..e3aa7e6d1c 100644
--- a/lib/chef/provider/mount/mount.rb
+++ b/lib/chef/provider/mount/mount.rb
@@ -201,7 +201,7 @@ class Chef
end
def network_device?
- @new_resource.device =~ /:/ || @new_resource.device =~ %r{//}
+ @new_resource.device.include?(":") || @new_resource.device.include?("//")
end
def device_should_exist?
diff --git a/lib/chef/provider/package/apt.rb b/lib/chef/provider/package/apt.rb
index 4fbacdc065..dbacaedb07 100644
--- a/lib/chef/provider/package/apt.rb
+++ b/lib/chef/provider/package/apt.rb
@@ -161,7 +161,7 @@ class Chef
def config_file_options
# If the user has specified config file options previously, respect those.
- return if Array(options).any? { |opt| opt =~ /--force-conf/ }
+ return if Array(options).any? { |opt| opt.include?("--force-conf") }
# It doesn't make sense to install packages in a scenario that can
# result in a prompt. Have users decide up-front whether they want to
diff --git a/lib/chef/provider/user/aix.rb b/lib/chef/provider/user/aix.rb
index 893f304a96..740f9943d3 100644
--- a/lib/chef/provider/user/aix.rb
+++ b/lib/chef/provider/user/aix.rb
@@ -68,7 +68,7 @@ class Chef
def check_lock
lock_info = shell_out!("lsuser", "-a", "account_locked", new_resource.username)
- if whyrun_mode? && passwd_s.stdout.empty? && lock_info.stderr.match(/does not exist/)
+ if whyrun_mode? && passwd_s.stdout.empty? && lock_info.stderr.include?("does not exist")
# if we're in whyrun mode and the user is not yet created we assume it would be
return false
end
diff --git a/lib/chef/resource/hostname.rb b/lib/chef/resource/hostname.rb
index e959084fa5..07a71864c2 100644
--- a/lib/chef/resource/hostname.rb
+++ b/lib/chef/resource/hostname.rb
@@ -224,7 +224,7 @@ class Chef
else # windows
WINDOWS_EC2_CONFIG = 'C:\Program Files\Amazon\Ec2ConfigService\Settings\config.xml'.freeze
- raise "Windows hostnames cannot contain a period." if new_resource.hostname.match?(/\./)
+ raise "Windows hostnames cannot contain a period." if new_resource.hostname.include?(".")
# suppress EC2 config service from setting our hostname
if ::File.exist?(WINDOWS_EC2_CONFIG)
diff --git a/lib/chef/util/dsc/local_configuration_manager.rb b/lib/chef/util/dsc/local_configuration_manager.rb
index 138011e89e..431936574a 100644
--- a/lib/chef/util/dsc/local_configuration_manager.rb
+++ b/lib/chef/util/dsc/local_configuration_manager.rb
@@ -105,7 +105,7 @@ class Chef::Util::DSC
def dsc_module_import_failure?(command_output)
!! (command_output =~ /\sCimException/ &&
- command_output =~ /ProviderOperationExecutionFailure/ &&
+ command_output.include?("ProviderOperationExecutionFailure") &&
command_output =~ /\smodule\s+is\s+installed/)
end