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

Commit

Permalink
Add the sh/bash configuration file format.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Bellone committed Sep 18, 2017
1 parent 0b4c1b0 commit 8de1d67
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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 @@ -89,6 +89,8 @@ def default_format
# have string content, it's just raw content by default.
return 'text' if pattern || content.is_a?(String)
case path
when /\.(ba)?sh$/
'sh'
when /\.json$/
'json'
when /\.ya?ml$/
Expand Down Expand Up @@ -209,6 +211,20 @@ def content_for_pattern
# @return [String]
def content_for_format
case @new_resource.format.to_s
when 'sh'
prefix = []
serializer = lambda do |(key, value)|
if value.kind_of? Hash
prefix.push(key)
r = value.map(&serializer)
prefix.pop
r
else
identifier = [prefix, key].flatten.compact.map(&:upcase).join('_')
"export #{identifier}=\"#{value}\""
end
end
@new_resource.content.to_hash.map(&serializer).join("\n")
when 'json'
require 'chef/json_compat'
# Make sure we include the trailing newline because YAML has one.
Expand Down
20 changes: 20 additions & 0 deletions test/spec/resources/poise_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@
end

describe 'formats' do
context 'with a .sh path' do
recipe(subject: false) do
poise_file "#{node['temp_path']}/test.sh" do
content foo: 'bar', bar: { baz: 'qux', quux: 'quuz' }
end
end

its(['test.sh']) { is_expected.to eq %Q(export FOO="bar"\nexport BAR_BAZ="qux"\nexport BAR_QUUX="quuz") }
end # /context with a .sh path

context 'with a .bash path' do
recipe(subject: false) do
poise_file "#{node['temp_path']}/test.bash" do
content foo: 'bar', bar: { baz: 'qux', quux: 'quuz' }
end
end

its(['test.bash']) { is_expected.to eq %Q(export FOO="bar"\nexport BAR_BAZ="qux"\nexport BAR_QUUX="quuz") }
end # /context with a .bash path

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

0 comments on commit 8de1d67

Please sign in to comment.