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

Commit

Permalink
Patch to chef-dk to resolve issue #186
Browse files Browse the repository at this point in the history
- Add powershell and cmd to chef shell-init
- Handle stray quotes in powershell
- Powershell documentation in help message
  • Loading branch information
bwmeier authored and btm committed Nov 13, 2014
1 parent 2a14aff commit 666c2ed
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions lib/chef-dk/command/shell_init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module ChefDK
module Command
class ShellInit < ChefDK::Command::Base

SUPPORTED_SHELLS = %w[ bash zsh sh ].map(&:freeze).freeze
SUPPORTED_SHELLS = %w[ bash zsh sh powershell cmd].map(&:freeze).freeze

banner(<<-HELP)
Usage: chef shell-init
Expand All @@ -32,11 +32,19 @@ class ShellInit < ChefDK::Command::Base
To enable for just the current shell session:
For sh, zsh, and bash:
eval "$(chef shell-init SHELL_NAME)"
For powershell:
chef shell-init powershell | Invoke-Expression
To permanently enable:
For sh, zsh, and bash:
echo 'eval "$(chef shell-init SHELL_NAME)"' >> ~/.YOUR_SHELL_RC_FILE
For powershell:
"chef shell-init powershell | Invoke-Expression" >> $PROFILE
Supported shells: #{SUPPORTED_SHELLS.join(' ')}
OPTIONS:
Expand All @@ -51,8 +59,6 @@ def omnibus_root
end

def run(argv)
# Currently we don't have any shell-specific features, so we ignore the
# shell name. We'll need it if we add completion.
remaining_args = parse_options(argv)
shell_name = remaining_args.first
if shell_name.nil?
Expand All @@ -67,9 +73,36 @@ def run(argv)

env = omnibus_env.dup
path = env.delete("PATH")
msg("export PATH=#{path}")
env.each do |var_name, value|
msg("export #{var_name}=#{value}")
if shell_name == "powershell"
# some of these environment variables have quotes around them as written
# if that is not the case, add them for powershell set-item invocation
quotechars = %w[ ' " ]
if (!quotechars.include?(path[0]))
path = "'#{path}'"
end
# note that this requires the fix for #180
# https://github.com/opscode/chef-dk/issues/180
msg("Set-Item Env:\\PATH #{path}")
env.each do |var_name, value|
# some of these environment variables have quotes around them as written
# if that is not the case, add them for powershell set-item invocation
if (!quotechars.include?(value[0]))
value = "'#{value}'"
end
msg("Set-Item Env:\\#{var_name} #{value}")
end
elsif shell_name == "cmd"
# note that this requires the fix for #180
# https://github.com/opscode/chef-dk/issues/180
msg("SET PATH=#{path}")
env.each do |var_name, value|
msg("SET #{var_name}=#{value}")
end
else
msg("export PATH=#{path}")
env.each do |var_name, value|
msg("export #{var_name}=#{value}")
end
end
0
end
Expand Down

0 comments on commit 666c2ed

Please sign in to comment.