diff --git a/lib/poise_file/resources/poise_file.rb b/lib/poise_file/resources/poise_file.rb index 8b03d37..5b11550 100644 --- a/lib/poise_file/resources/poise_file.rb +++ b/lib/poise_file/resources/poise_file.rb @@ -91,6 +91,8 @@ def default_format case path when /\.json$/ 'json' + when /\.ini$/ + 'ini' when /\.ya?ml$/ 'yaml' else @@ -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) diff --git a/poise-file.gemspec b/poise-file.gemspec index 5cec165..9ac78d8 100644 --- a/poise-file.gemspec +++ b/poise-file.gemspec @@ -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' diff --git a/test/cookbook/recipes/default.rb b/test/cookbook/recipes/default.rb index d72ea3b..cacaaf0 100644 --- a/test/cookbook/recipes/default.rb +++ b/test/cookbook/recipes/default.rb @@ -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 diff --git a/test/integration/default/serverspec/default_spec.rb b/test/integration/default/serverspec/default_spec.rb index 3c51a91..d478321 100644 --- a/test/integration/default/serverspec/default_spec.rb +++ b/test/integration/default/serverspec/default_spec.rb @@ -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" } diff --git a/test/spec/resources/poise_file_spec.rb b/test/spec/resources/poise_file_spec.rb index 89e297c..6bb69c6 100644 --- a/test/spec/resources/poise_file_spec.rb +++ b/test/spec/resources/poise_file_spec.rb @@ -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