class RuboCop::Cop::Chef::Modernize::ExecuteTzUtil

Instead of using the execute or powershell_script resources to run the ‘tzutil` command, use Chef Infra Client’s built-in timezone resource which is available in Chef Infra Client 14.6 and later.

@example

#### incorrect
execute 'set tz' do
  command 'tzutil.exe /s UTC'
end

execute 'tzutil /s UTC'

powershell_script 'set windows timezone' do
  code "tzutil.exe /s UTC"
  not_if { shell_out('tzutil.exe /g').stdout.include?('UTC') }
end

#### correct
timezone 'UTC'

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/chef/modernize/execute_tzutil.rb, line 61
def on_block(node)
  match_property_in_resource?(:execute, 'command', node) do |code_property|
    next unless calls_tzutil?(code_property)
    add_offense(node, severity: :refactor)
  end

  match_property_in_resource?(:powershell_script, 'code', node) do |code_property|
    next unless calls_tzutil?(code_property)
    add_offense(node, severity: :refactor)
  end
end
on_send(node) click to toggle source
# File lib/rubocop/cop/chef/modernize/execute_tzutil.rb, line 54
def on_send(node)
  execute_resource?(node) do
    return unless node.arguments.first.value.match?(/^tzutil/i)
    add_offense(node, severity: :refactor)
  end
end

Private Instance Methods

calls_tzutil?(ast_obj) click to toggle source
# File lib/rubocop/cop/chef/modernize/execute_tzutil.rb, line 75
def calls_tzutil?(ast_obj)
  property_data = method_arg_ast_to_string(ast_obj)
  return true if property_data && property_data.match?(/^tzutil/i)
end