rule "FC004", "Use a service resource to start and stop services" do
  tags %w{portability services}
  recipe do |ast|
    find_resources(ast, type: "execute").find_all do |cmd|
      cmd_str = (resource_attribute(cmd, "command") || resource_name(cmd)).to_s
      (cmd_str.include?("/etc/init.d") || ["service ", "/sbin/service ",
       "start ", "stop ", "invoke-rc.d "].any? do |service_cmd|
         cmd_str.start_with?(service_cmd)
       end) && %w{start stop restart reload}.any? { |a| cmd_str.include?(a) }
    end
  end
end

rule "FC004", "Use a service resource to start and stop services" do
  tags %w{portability services}
  recipe do |ast|
    find_resources(ast, type: "execute").find_all do |cmd|
      cmd_str = (resource_attribute(cmd, "command") || resource_name(cmd)).to_s
      next if cmd_str.include?(".exe") # don't catch windows commands
      cmd_str.start_with?( "start ", "stop ", "reload ", "restart ") || # upstart jobs
        ( [ "/etc/init.d", "service ", "/sbin/service ", "invoke-rc.d ", "systemctl "].any? do |service_cmd| # upstart / sys-v / systemd
          cmd_str.start_with?(service_cmd)
        end && [" start", " stop", " restart", " reload"].any? { |a| cmd_str.include?(a) } ) # make sure we get exactly the commands not something like 'daemon-reload'
    end
  end
end

Refactor FC004 to catch more w/o as many false positives
Refactor FC004 to catch more w/o as many false positives

I sorta thing this should be a series of regex statements, but this does work and we have expanded specs to catch the false positives I encountered along the way

Signed-off-by: Tim Smith <764ef62106582a09ed09dfa0b6bff7c05fd7d1e4@chef.io>
