diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..0c4a490 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,35 @@ +AllCops: + Exclude: + - test/**/* + - vendor/**/* + - spec/**/* +Documentation: + Enabled: false +AlignParameters: + Enabled: false +Encoding: + Enabled: false +HashSyntax: + Enabled: false +LineLength: + Enabled: false +MethodLength: + Max: 30 +Style/FileName: + Enabled: false +# +# we like our neatly-aligned code too much +# +SingleSpaceBeforeFirstArg: + Enabled: false +# +# couldn't figure it out so set this +# +ClassAndModuleChildren: + EnforcedStyle: compact + +# +# libraries/bamboo is to complex for rubocop +# +Metrics/AbcSize: + Max: 17 \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index c312316..2ee48cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,4 +18,4 @@ before_script: - bundle exec berks install script: - - bundle exec rspec --color --format documentation + - bundle exec rake diff --git a/Berksfile b/Berksfile index 820f880..93f4389 100644 --- a/Berksfile +++ b/Berksfile @@ -3,6 +3,6 @@ source 'https://supermarket.chef.io' metadata group :dev do - cookbook 'test', - path: 'spec/support/cookbooks/test' + cookbook 'test', + path: 'spec/support/cookbooks/test' end diff --git a/Gemfile b/Gemfile index ce68109..b190af7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,8 @@ -source "http://rubygems.org" +source 'http://rubygems.org' gem 'berkshelf' - +gem 'foodcritic' +gem 'rubocop' group :dev do - gem 'chefspec' + gem 'chefspec' end diff --git a/Rakefile.rb b/Rakefile.rb new file mode 100644 index 0000000..0e2cbef --- /dev/null +++ b/Rakefile.rb @@ -0,0 +1,30 @@ +require 'foodcritic' +require 'rspec/core/rake_task' +require 'rubocop/rake_task' +require 'rake/dsl_definition' + +namespace :style do + desc 'Run RuboCop style and lint checks' + RuboCop::RakeTask.new(:rubocop) + + desc 'Run Foodcritic lint checks' + FoodCritic::Rake::LintTask.new(:foodcritic) do |t| + t.options = { + tags: %w(~FC023 ), + fail_tags: ['any'], + # include_rules: '', + context: true + } + end +end + +desc 'Run ChefSpec examples' +RSpec::Core::RakeTask.new(:spec) do |t| + t.rspec_opts = '--color --format documentation' +end + +desc 'Run all style checks' +task :style => ['style:rubocop', 'style:foodcritic', 'spec'] + +# Default +task :default => [:style] diff --git a/metadata.rb b/metadata.rb index 0c830e4..daba403 100644 --- a/metadata.rb +++ b/metadata.rb @@ -1,13 +1,13 @@ -name 'patch' -maintainer 'Jens Segers' +name 'patch' +maintainer 'Jens Segers' maintainer_email '' -license 'MIT' -description 'Some handy Chef resources for when you want to append, replace or delete and lines in files.' +license 'MIT' +description 'Some handy Chef resources for when you want to append, replace or delete and lines in files.' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '2.1.1' -source_url 'https://github.com/jenssegers/chef-patch' if respond_to?(:source_url) -issues_url 'https://github.com/jenssegers/chef-patch/issues' if respond_to?(:issues_url) +version '2.1.1' +source_url 'https://github.com/jenssegers/chef-patch' if respond_to?(:source_url) +issues_url 'https://github.com/jenssegers/chef-patch/issues' if respond_to?(:issues_url) %w(amazon centos debian fedora redhat scientific ubuntu).each do |os| - supports os + supports os end diff --git a/resources/append_line.rb b/resources/append_line.rb index 607c37b..79064b3 100644 --- a/resources/append_line.rb +++ b/resources/append_line.rb @@ -8,30 +8,29 @@ property :line, :kind_of => String, :required => true action :run do - - file_path = file || path || name - - # Check if the file already contains the line - unless ::File.exists?(file_path) && ::File.read(file_path) =~ /^#{Regexp.escape(line)}$/ - - # Append to file - converge_by("Append #{name}") do - ruby_block "#{name}" do - block do - begin - file = ::File.open(file_path, "a") - file.puts line - ensure - file.close - end - end - end - end - - Chef::Log.info "+ #{line}" - - # Notify that a node was updated successfully - updated_by_last_action(true) - - end + file_path = file || path || name + + # Check if the file already contains the line + unless ::File.exist?(file_path) && ::File.read(file_path) =~ /^#{Regexp.escape(line)}$/ + + # Append to file + converge_by("Append #{name}") do + ruby_block name do + block do + begin + file = ::File.open(file_path, 'a') + file.puts line + ensure + file.close + end + end + end + end + + Chef::Log.info "+ #{line}" + + # Notify that a node was updated successfully + updated_by_last_action(true) + + end end diff --git a/resources/delete_line.rb b/resources/delete_line.rb index 31b2b4a..96070af 100644 --- a/resources/delete_line.rb +++ b/resources/delete_line.rb @@ -8,37 +8,36 @@ property :line, :kind_of => [String, Regexp], :required => true action :run do - - file_path = file || path || name - - # Check if we got a regex or a string - if line.is_a?(Regexp) - regex = line - else - regex = Regexp.new(Regexp.escape(line)) - end - - # Check if file matches the regex - if ::File.read(file_path) =~ regex - - # Delete the line - converge_by("Delete line #{name}") do - ruby_block "#{name}" do - block do - file = Chef::Util::FileEdit.new(file_path) - file.search_file_delete_line(regex) - file.write_file - - # Remove backup file - ::File.delete(file_path + ".old") if ::File.exist?(file_path + ".old") - end - end - end - - Chef::Log.info "- #{line}" - - # Notify that a node was updated successfully - updated_by_last_action(true) - - end + file_path = file || path || name + + # Check if we got a regex or a string + if line.is_a?(Regexp) + regex = line + else + regex = Regexp.new(Regexp.escape(line)) + end + + # Check if file matches the regex + if ::File.read(file_path) =~ regex + + # Delete the line + converge_by("Delete line #{name}") do + ruby_block name do + block do + file = Chef::Util::FileEdit.new(file_path) + file.search_file_delete_line(regex) + file.write_file + + # Remove backup file + ::File.delete(file_path + '.old') if ::File.exist?(file_path + '.old') + end + end + end + + Chef::Log.info "- #{line}" + + # Notify that a node was updated successfully + updated_by_last_action(true) + + end end diff --git a/resources/insert_line_after_match.rb b/resources/insert_line_after_match.rb index ab31d08..2550b99 100644 --- a/resources/insert_line_after_match.rb +++ b/resources/insert_line_after_match.rb @@ -9,40 +9,35 @@ property :insert, :kind_of => String, :required => true action :run do + file_path = file || path || name - file_path = file || path || name - - # Check if we got a regex or a string - if line.is_a?(Regexp) - regex = line - else - regex = Regexp.new(Regexp.escape(line)) + # Check if we got a regex or a string + if line.is_a?(Regexp) + regex = line + else + regex = Regexp.new(Regexp.escape(line)) end - unless ::File.exists?(file_path) && ::File.foreach(file_path).grep(/#{insert}/).size > 0 + unless ::File.exist?(file_path) && ::File.foreach(file_path).grep(/#{insert}/).size > 0 - # Check if file matches the regex - if ::File.read(file_path) =~ regex + # Check if file matches the regex + if ::File.read(file_path) =~ regex - # Replace the matching text - converge_by("insert_line_after_match #{name}") do - ruby_block "#{name}" do - block do - file = Chef::Util::FileEdit.new(file_path) - file.insert_line_after_match(regex, insert) - file.write_file + # Replace the matching text + converge_by("insert_line_after_match #{name}") do + ruby_block name do + block do + file = Chef::Util::FileEdit.new(file_path) + file.insert_line_after_match(regex, insert) + file.write_file + end end - end - end - - Chef::Log.info "+ #{insert}" + end - # Notify that a node was updated successfully - updated_by_last_action(true) - - end + Chef::Log.info "+ #{insert}" - # Chef::Log.warn "wiebenik + #{regins}" - # Chef::Log.warn "watbenik - #{regex}" + # Notify that a node was updated successfully + updated_by_last_action(true) + end end end diff --git a/resources/replace.rb b/resources/replace.rb index a79ce1f..b9d0ddf 100644 --- a/resources/replace.rb +++ b/resources/replace.rb @@ -9,38 +9,36 @@ property :with, :kind_of => String, :required => true action :run do - - file_path = file || path || name - - # Check if we got a regex or a string - if replace.is_a?(Regexp) - regex = replace - else - regex = Regexp.new(Regexp.escape(replace)) - end - - # Check if file matches the regex - if ::File.read(file_path) =~ regex - - # Replace the matching text - converge_by("Replace #{name}") do - ruby_block "#{name}" do - block do - file = Chef::Util::FileEdit.new(file_path) - file.search_file_replace(regex, with) - file.write_file - - # Remove backup file - ::File.delete(file_path + ".old") if ::File.exist?(file_path + ".old") - end - end - end - - Chef::Log.info "- #{replace}" - Chef::Log.info "+ #{with}" - - # Notify that a node was updated successfully - updated_by_last_action(true) - - end + file_path = file || path || name + + # Check if we got a regex or a string + if replace.is_a?(Regexp) + regex = replace + else + regex = Regexp.new(Regexp.escape(replace)) + end + + # Check if file matches the regex + if ::File.read(file_path) =~ regex + + # Replace the matching text + converge_by("Replace #{name}") do + ruby_block name do + block do + file = Chef::Util::FileEdit.new(file_path) + file.search_file_replace(regex, with) + file.write_file + + # Remove backup file + ::File.delete(file_path + '.old') if ::File.exist?(file_path + '.old') + end + end + end + + Chef::Log.info "- #{replace}" + Chef::Log.info "+ #{with}" + + # Notify that a node was updated successfully + updated_by_last_action(true) + end end diff --git a/resources/replace_line.rb b/resources/replace_line.rb index 8935e84..df096b1 100644 --- a/resources/replace_line.rb +++ b/resources/replace_line.rb @@ -9,38 +9,36 @@ property :with, :kind_of => String, :required => true action :run do - - file_path = file || path || name - - # Check if we got a regex or a string - if replace.is_a?(Regexp) - regex = replace - else - regex = Regexp.new(Regexp.escape(replace)) - end - - # Check if file matches the regex - if ::File.read(file_path) =~ regex - - # Replace the line - converge_by("Replace line #{name}") do - ruby_block "#{name}" do - block do - file = Chef::Util::FileEdit.new(file_path) - file.search_file_replace_line(regex, with) - file.write_file - - # Remove backup file - ::File.delete(file_path + ".old") if ::File.exist?(file_path + ".old") - end - end - end - - Chef::Log.info "- #{replace}" - Chef::Log.info "+ #{with}" - - # Notify that a node was updated successfully - updated_by_last_action(true) - - end + file_path = file || path || name + + # Check if we got a regex or a string + if replace.is_a?(Regexp) + regex = replace + else + regex = Regexp.new(Regexp.escape(replace)) + end + + # Check if file matches the regex + if ::File.read(file_path) =~ regex + + # Replace the line + converge_by("Replace line #{name}") do + ruby_block name do + block do + file = Chef::Util::FileEdit.new(file_path) + file.search_file_replace_line(regex, with) + file.write_file + + # Remove backup file + ::File.delete(file_path + '.old') if ::File.exist?(file_path + '.old') + end + end + end + + Chef::Log.info "- #{replace}" + Chef::Log.info "+ #{with}" + + # Notify that a node was updated successfully + updated_by_last_action(true) + end end