Skip to content

Commit

Permalink
Introducing delete_lines
Browse files Browse the repository at this point in the history
  • Loading branch information
jblaine committed Sep 12, 2013
1 parent beb1fba commit 00364eb
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ the resoures.
line "Why hello there, you beautiful person, you."
end

delete_lines "remove hash-comments from /some/file" do
path "/some/file"
pattern "^#.*"
end

# Notes
So far, the only resource implemented are

append_if_no_line.
append_if_no_line
replace_or_add
delete_lines

More to follow.

Expand Down
77 changes: 77 additions & 0 deletions libraries/provider_delete_lines.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#
# Cookbook Name:: line
# Library:: provider_delete_lines
#
# Author:: Sean OMeara <[email protected]>
# Author:: Jeff Blaine <[email protected]>
# Copyright 2012-2013, Opscode, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'fileutils'
require 'tempfile'

class Chef
class Provider
class DeleteLines < Chef::Provider

def load_current_resource
end

def action_edit
regex = /#{new_resource.pattern}/

if ::File.exists?(new_resource.path) then
begin
f = ::File.open(new_resource.path, "r+")

file_owner = f.lstat.uid
file_group = f.lstat.gid
file_mode = f.lstat.mode

temp_file = Tempfile.new('foo')

modified = false

f.lines.each do |line|
if line =~ regex then
modified = true
else
temp_file.puts line
end
end

f.close

if modified then
temp_file.rewind
FileUtils.copy_file(temp_file.path,new_resource.path)
FileUtils.chown(file_owner,file_group,new_resource.path)
FileUtils.chmod(file_mode,new_resource.path)
new_resource.updated_by_last_action(true)
end

ensure
temp_file.close
temp_file.unlink
end
end # ::File.exists
end # def action_edit

def action_nothing
end

end
end
end
50 changes: 50 additions & 0 deletions libraries/resource_delete_lines.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Cookbook Name:: line
# Library:: resource_delete_lines
#
# Author:: Sean OMeara <[email protected]>
# Author:: Jeff Blaine <[email protected]>
# Copyright 2012-2013, Opscode, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class Chef
class Resource
class DeleteLines < Chef::Resource

def initialize(name, run_context=nil)
super
@resource_name = :delete_lines
@action = :edit
@allowed_actions.push(:edit,:nothing)
end

def path(arg=nil)
set_or_return(
:path,
arg,
:kind_of => String
)
end

def pattern(arg=nil)
set_or_return(
:line,
arg,
:kind_of => String
)
end

end
end
end

0 comments on commit 00364eb

Please sign in to comment.