From d607fc941cd135f95b7064df5af1af520d8ca02f Mon Sep 17 00:00:00 2001 From: Jeff Byrnes Date: Mon, 29 Oct 2018 14:48:15 -0400 Subject: [PATCH] Switch to using cookstyle (#565) Maintain the current style choices, but does away with a lot of unnecessary rule bending/exclusion. Maintains backwards-compatible w/ ChefDK v2.x w/ the Style/TrailingCommaInLiteral rule. However, this rule breaks newer configurations, so it should eventually be removed. --- .rubocop.yml | 54 ++++++++------------------------------- Rakefile | 1 + providers/monitor.rb | 2 +- recipes/_agent6_config.rb | 4 +-- recipes/dd-agent.rb | 4 +-- recipes/dd-handler.rb | 4 +-- 6 files changed, 18 insertions(+), 51 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 6b6c5be0..e48df3b8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,61 +1,27 @@ # inherit_from: .rubocop_todo.yml # Stylistic choices of this cookbook. -AllCops: - Exclude: - - 'vendor/**/*' # ignore non-local files - - Guardfile - -# We enjoy parameters lining up for visual comprehension -Layout/SpaceBeforeFirstArg: +# We still have HashRockets in a lot of places, ignore them. +Style/HashSyntax: Enabled: false -# We like longer lines -Metrics/LineLength: - Max: 160 - -# Exclude Rakefile and tests since there are long blocks that aren't worth shortening there -# Also, slightly longer blocks are fine -Metrics/BlockLength: - Max: 30 - Exclude: - - 'Rakefile' - - 'spec/**/*.rb' - - 'test/**/*.rb' - - -# Slightly longer methods are fine -Metrics/MethodLength: - Max: 15 - # We're fine with multi-line blocks defined with `{...}` instead of `do...end` Style/BlockDelimiters: Enabled: false -Style/FrozenStringLiteralComment: - Enabled: false +Style/PercentLiteralDelimiters: + PreferredDelimiters: + default: '[]' + '%i': '[]' + '%w': '[]' -Style/SafeNavigation: +Style/TrailingCommaInArrayLiteral: Enabled: false -# TODO: add encoding headers -Style/Encoding: +Style/TrailingCommaInHashLiteral: Enabled: false -# Our code has names with dashes. -Style/FileName: - Enabled: false - -# We still have HashRockets in a lot of places, ignore them. -Style/HashSyntax: - Enabled: false - -# Increase readability of large numbers after 5 digits (ports) only. -Style/NumericLiterals: - MinDigits: 6 - -# TODO: see if this should be replaced by Chef::Application.fatal! -Style/SignalException: +Style/TrailingCommaInLiteral: Enabled: false # Only enforce a wordlist for 5 or longer lists. diff --git a/Rakefile b/Rakefile index dd5d9a6e..0b9dbd16 100755 --- a/Rakefile +++ b/Rakefile @@ -6,6 +6,7 @@ require 'foodcritic' require 'kitchen/rake_tasks' require 'rake/clean' require 'rspec/core/rake_task' +require 'cookstyle' require 'rubocop/rake_task' task :default => %i[ diff --git a/providers/monitor.rb b/providers/monitor.rb index 749212cd..a5f476b3 100644 --- a/providers/monitor.rb +++ b/providers/monitor.rb @@ -7,7 +7,7 @@ def whyrun_supported? true end -action :add do # rubocop:disable Metrics/BlockLength +action :add do Chef::Log.debug "Adding monitoring for #{new_resource.name}" template ::File.join(yaml_dir, "#{new_resource.name}.yaml") do if node['platform_family'] == 'windows' diff --git a/recipes/_agent6_config.rb b/recipes/_agent6_config.rb index 801f0ea0..19238007 100644 --- a/recipes/_agent6_config.rb +++ b/recipes/_agent6_config.rb @@ -20,8 +20,8 @@ is_windows = node['platform_family'] == 'windows' agent6_config_file = ::File.join(node['datadog']['agent6_config_dir'], 'datadog.yaml') -template agent6_config_file do # rubocop:disable Metrics/BlockLength - def template_vars # rubocop:disable Metrics/AbcSize, Metrics/MethodLength +template agent6_config_file do + def template_vars additional_endpoints = {} node['datadog']['extra_endpoints'].each do |_, endpoint| next unless endpoint['enabled'] diff --git a/recipes/dd-agent.rb b/recipes/dd-agent.rb index 9ea9340c..44d23936 100644 --- a/recipes/dd-agent.rb +++ b/recipes/dd-agent.rb @@ -63,8 +63,8 @@ end agent_config_file = ::File.join(node['datadog']['config_dir'], 'datadog.conf') - template agent_config_file do # rubocop:disable Metrics/BlockLength - def template_vars # rubocop:disable Metrics/AbcSize + template agent_config_file do + def template_vars api_keys = [Chef::Datadog.api_key(node)] dd_urls = [node['datadog']['url']] node['datadog']['extra_endpoints'].each do |_, endpoint| diff --git a/recipes/dd-handler.rb b/recipes/dd-handler.rb index 387954b5..33203f0b 100644 --- a/recipes/dd-handler.rb +++ b/recipes/dd-handler.rb @@ -49,7 +49,7 @@ end # Create the handler to run at the end of the Chef execution -chef_handler 'Chef::Handler::Datadog' do # rubocop:disable Metrics/BlockLength +chef_handler 'Chef::Handler::Datadog' do def extra_endpoints extra_endpoints = [] node['datadog']['extra_endpoints'].each do |_, endpoint| @@ -61,7 +61,7 @@ def extra_endpoints extra_endpoints end - def handler_config # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + def handler_config extra_config = node['datadog']['handler_extra_config'].reject { |_, v| v.nil? } config = extra_config.merge( :api_key => Chef::Datadog.api_key(node),