Class: RuboCop::Cop::Chef::ChefModernize::ShellOutToChocolatey

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

Overview

Use the Chocolatey resources built into Chef Infra Client instead of shelling out to the choco command

powershell_script 'add artifactory choco source' do
  code "choco source add -n=artifactory -s='https://mycorp.jfrog.io/mycorp/api/nuget/chocolatey-remote' -u foo -p bar"x
  not_if 'choco source list | findstr artifactory'
end

Examples:


# bad
execute 'install package foo' do
  command "choco install --source=artifactory \"foo\" -y --no-progress --ignore-package-exit-codes"
end

Constant Summary collapse

MSG =
'Use the Chocolatey resources built into Chef Infra Client instead of shelling out to the choco command'.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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/chef/modernize/shellouts_to_chocolatey.rb', line 40

def on_block(node)
  match_property_in_resource?(:powershell_script, 'code', node) do |code_property|
    property_data = method_arg_ast_to_string(code_property)
    if property_data && property_data.match?(/^choco /i)
      add_offense(node, location: :expression, message: MSG, severity: :refactor)
    end
  end

  match_property_in_resource?(:execute, 'command', node) do |code_property|
    property_data = method_arg_ast_to_string(code_property)
    if property_data && property_data.match?(/^choco /i)
      add_offense(node, location: :expression, message: MSG, severity: :refactor)
    end
  end
end