Class: RuboCop::Cop::Chef::Modernize::ExecuteScExe

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

Overview

Chef Infra Client 14.0 and later includes :create, :delete, and :configure actions with the full idempotency of the windows_service resource. See the windows_service documentation at docs.chef.io/resources/windows_service for additional details on creating services with the windows_service resource.

Examples:


### incorrect
execute "Delete chef-client service" do
  command "sc.exe delete chef-client"
  action :run
end

### correct
windows_service 'chef-client' do
  action :delete
end

Constant Summary collapse

MSG =
'Chef Infra Client 14.0 and later includes :create, :delete, and :configure actions with the full idempotency of the windows_service resource. See the windows_service documentation at https://docs.chef.io/resources/windows_service for additional details on creating services with the windows_service resource'
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

block execute resources



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

def on_block(node)
  match_property_in_resource?(:execute, 'command', node) do |code_property|
    property_data = method_arg_ast_to_string(code_property)
    return unless property_data && property_data.match?(/^sc.exe/i)
    add_offense(node, severity: :refactor)
  end
end

#on_send(node) ⇒ Object

non block execute resources



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

def on_send(node)
  # use a regex on source instead of .value in case there's string interpolation which adds a complex dstr type
  # with a nested string and a begin. Source allows us to avoid a lot of defensive programming here
  return unless node&.arguments.first&.source&.match?(/^("|')sc.exe/)

  add_offense(node, severity: :refactor)
end