Class: RuboCop::Cop::Chef::Modernize::ExecuteTzUtil

Inherits:
Base
  • Object
show all
Extended by:
TargetChefVersion
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 run the ‘tzutil` command, use Chef Infra Client’s built-in timezone resource which is available in Chef Infra Client 14.6 and later.

Examples:


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

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

Methods included from RuboCop::Chef::CookbookHelpers

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

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



54
55
56
57
58
59
# 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