Hi
I’m quite new here, but I really love the project and the community.
You all are great
Well, I was writting a couple of new cookbooks for a project[1] where
I’m working on and I found that I need very often to check if some
string is in a file and replace just one string keepking the rest of
the file as it was.
For example a file that a user (or app) can write and usually does.
Maybe we like to be sure that specific value for a key is always the
right one, but not to change the part of the file where the user (or
app) has already changed.
I wrote some code for this (which is currently working), but I don’t
know if I did in the right place and in the right way. I love to have
it at Chef, but I guess it need to be proven useful and the community
agree. By now I put it in my cookbooks at:
cookbooks/the_cookbook/libraries/file.rb
Here is the actual code and example or recipe using it:
But I’ll write down here a simplier version:
def include?(str)
file_content = ::File.open(@path).read
return true if file_content =~ /#{str}/
return false
end
def replace(str, str2)
old_content = ::File.open(@path).read
content old_content.gsub(str, str2)
end
These methods are Chef::Resource::File’s methods. I wrote like that
because was the only way I got them working, but it seems to me that
it should be a Providers’ instead… I don’t know…
I hope you guy could give me some light about it. And also tell me if
you find this interesting enough to have it at Chef itself. I’m pretty
sure that I gonna use them a lot for my recipes…
Thanks a lot for your time.
Cheers
[1] http://www.guadalinex.org
http://goo.gl/WSoU2 (google treanslated version of the actual project)
Juanje