Class: RuboCop::Cop::Chef::ChefModernize::ExecuteTzUtil

Inherits:
RuboCop::Cop
  • Object
show all
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/modernize/execute_tzutil.rb

Overview

Instead of using the execute or powershell_script resources to 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.

# bad
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

# good
timezone 'UTC'

Constant Summary collapse

MSG =
'Use the timezone resource included in Chef Infra Client 14.6+ instead of shelling out to tzutil'.freeze

Instance Method Summary collapse

Methods included from RuboCop::Chef::CookbookHelpers

#match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Instance Method Details

#on_block(node) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/chef/modernize/execute_tzutil.rb', line 54

def on_block(node)
  match_property_in_resource?(:execute, 'command', node) do |code_property|
    add_offense(node, location: :expression, message: MSG, severity: :refactor) if calls_tzutil?(code_property)
  end

  match_property_in_resource?(:powershell_script, 'code', node) do |code_property|
    add_offense(node, location: :expression, message: MSG, severity: :refactor) if calls_tzutil?(code_property)
  end
end

#on_send(node) ⇒ Object



48
49
50
51
52
# File 'lib/rubocop/cop/chef/modernize/execute_tzutil.rb', line 48

def on_send(node)
  execute_resource?(node) do
    add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.arguments.first.value.match?(/^tzutil/i)
  end
end