Class: RuboCop::Cop::Chef::Modernize::PowershellDownloadFile

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

Overview

Use the remote_file resource to download files instead of shelling out to PowerShell. remote_file is idempotent, supports checksum verification, proxies, and conditional downloads with etags and last-modified headers, none of which a script gets for free.

Examples:


# bad
powershell_script 'download the installer' do
  code 'Invoke-WebRequest -Uri https://example.com/foo.msi -OutFile C:\foo.msi'
end

# good
remote_file 'C:\foo.msi' do
  source 'https://example.com/foo.msi'
end

Constant Summary collapse

MSG =
'Use the remote_file resource to download files instead of downloading them in a PowerShell script resource'
DOWNLOAD_COMMANDS =

the PowerShell ways of pulling a file down over HTTP

Regexp.union(
  /invoke-webrequest\s/i,
  /\bstart-bitstransfer\s/i,
  /\bwget\s/i,        # PowerShell alias for Invoke-WebRequest
  /\bcurl\s/i,        # PowerShell alias for Invoke-WebRequest
  /system\.net\.webclient/i
).freeze

Instance Method Summary collapse

Methods included from RuboCop::Chef::CookbookHelpers

#each_action_symbol, #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



52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/chef/modernize/powershell_download_file.rb', line 52

def on_block(node)
  match_property_in_resource?(%i(powershell_script pwsh_script), 'code', node) do |code_property|
    property_data = method_arg_ast_to_string(code_property)
    next unless property_data&.match?(DOWNLOAD_COMMANDS)

    add_offense(node, severity: :refactor)
  end
end