Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Add support for INI configuraiton file. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions lib/poise_file/resources/poise_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def default_format
case path
when /\.json$/
'json'
when /\.ini$/
'ini'
when /\.ya?ml$/
'yaml'
else
Expand Down Expand Up @@ -213,6 +215,20 @@ def content_for_format
require 'chef/json_compat'
# Make sure we include the trailing newline because YAML has one.
Chef::JSONCompat.to_json_pretty(@new_resource.content) + "\n"
when 'ini'
require 'iniparse'
IniParse.gen do |doc|
serializer = lambda do |key, value|
if value.kind_of? Hash
doc.section(key) do |s|
value.each_pair(&serializer)
end
else
doc.option(key, value)
end
end
@new_resource.content.to_hash.each_pair(&serializer)
end.to_ini
when 'yaml'
require 'yaml'
YAML.dump(@new_resource.content)
Expand Down
1 change: 1 addition & 0 deletions poise-file.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'chef', '>= 12.1', '< 14'
spec.add_dependency 'halite', '~> 1.0'
spec.add_dependency 'iniparse', '~> 1.4'
spec.add_dependency 'poise', '~> 2.0'

spec.add_development_dependency 'poise-boiler', '~> 1.0'
Expand Down
4 changes: 4 additions & 0 deletions test/cookbook/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
content 'here' => 'is my spout', 'when' => ['I', 'get', 'all', 'steamed', 'up']
end

poise_file '/poise_test.ini' do
content 'here' => 'is my spout', 'when' => ['I', 'get', 'all', 'steamed', 'up']
end

file '/poise_test_pattern' do
content "I must shout\ntip me over\n"
end
Expand Down
14 changes: 14 additions & 0 deletions test/integration/default/serverspec/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@
EOH
end

describe file('/poise_test.ini') do
it { is_expected.to be_a_file }
its(:content) { is_expected.to eq <<-EOH }
---
here: is my spout
when:
- I
- get
- all
- steamed
- up
EOH
end

describe file('/poise_test_pattern') do
it { is_expected.to be_a_file }
its(:content) { is_expected.to eq "I must yell\ntip me over\n" }
Expand Down
10 changes: 10 additions & 0 deletions test/spec/resources/poise_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
its(['test.json']) { is_expected.to eq "foo\nbar\n" }
end # /context with a .json path but string content

context 'with a .ini path' do
recipe(subject: false) do
poise_file "#{node['temp_path']}/test.ini" do
content 'foo' => { 'bar' => 'baz' }
end
end

its(['test.ini']) { is_expected.to eq %Q(foo = bar\n) }
end # /context with a .ini path

context 'with a .yaml path' do
recipe(subject: false) do
poise_file "#{node['temp_path']}/test.yaml" do
Expand Down