Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for variable expansion when setting environment variables #6

Closed
Closed
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ end

This will export an `EDITOR` environment variable with a value of `vim`.

By default environment variables set using `magic_shell_environment` do not permit variable expansion. You can allow this using the `expand` option:

```ruby
magic_shell_environment 'PATH' do
value '/my/custom.path:$PATH'
expand true
end
```

You can also remove environment variables:

```ruby
Expand Down
6 changes: 5 additions & 1 deletion providers/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
environment_name = new_resource.environment_variable.gsub(/ /,"_")
if !new_resource.value.nil?
Chef::Log.info("Adding #{environment_name}.sh to /etc/profile.d/")

# Variable expansion option
quote = !!new_resource.expand ? '"' : "'"

file_contents = "# This file was generated by Chef for #{node["fqdn"]}\n"
file_contents += "export #{environment_name}='#{new_resource.value}'"
file_contents += "export #{environment_name}=#{quote}#{new_resource.value}#{quote}"
resource = file "/etc/profile.d/#{environment_name}.sh" do
owner "root"
group "root"
Expand Down
3 changes: 3 additions & 0 deletions resources/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
attribute :value,
:kind_of => String,
:default => :add
attribute :expand,
:kind_of => [TrueClass, FalseClass],
:default => false

# Default action for Chef <= 10.8
def initialize(*args)
Expand Down
4 changes: 4 additions & 0 deletions spec/cookbooks/environment/recipes/add_with_expansion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
magic_shell_environment 'PATH' do
value '/my/custom/path:$PATH'
expand true
end
3 changes: 3 additions & 0 deletions spec/cookbooks/environment/recipes/add_without_expansion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
magic_shell_environment 'PATH' do
value '/my/custom/path:$PATH'
end
10 changes: 10 additions & 0 deletions spec/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,15 @@
chef_run.converge('environment::add_explicit')
expect(chef_run).to set_a_magic_shell_environment_variable('RAILS_ENV').with_value('production')
end

it 'variable expansion is disabled by default' do
chef_run.converge('environment::add_without_expansion')
expect(chef_run).to set_a_magic_shell_environment_variable('PATH').with_value('/my/custom/path:$PATH').with_quote("'")
end

it 'variable expansion can be enabled' do
chef_run.converge('environment::add_with_expansion')
expect(chef_run).to set_a_magic_shell_environment_variable('PATH').with_value('/my/custom/path:$PATH').with_quote('"')
end
end
end
7 changes: 6 additions & 1 deletion spec/support/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
@value = value
end

chain :with_quote do |quote|
@quote = quote
end

failure_message_for_should do |chef_run|
if chef_run.file(filepath)
"expected `#{filepath}` to have content:\n\n #{content}\n\nbut got:\n\n #{chef_run.file(filepath).content.split("\n").join("\n ")}"
Expand All @@ -34,6 +38,7 @@ def filepath
end

def content
@value ? "export #{@variable}='#{@value}'" : "export #{@variable}"
q = @quote || "'"
@value ? "export #{@variable}=#{q}#{@value}#{q}" : "export #{@variable}"
end
end