Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ before_script:
- bundle exec berks install

script:
- bundle exec rspec --color --format documentation
- bundle exec rake
4 changes: 2 additions & 2 deletions Berksfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions Rakefile.rb
Original file line number Diff line number Diff line change
@@ -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]
16 changes: 8 additions & 8 deletions metadata.rb
Original file line number Diff line number Diff line change
@@ -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
51 changes: 25 additions & 26 deletions resources/append_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
65 changes: 32 additions & 33 deletions resources/delete_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 22 additions & 27 deletions resources/insert_line_after_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 32 additions & 34 deletions resources/replace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading