diff --git a/lib/chef-dk/command/shell_init.rb b/lib/chef-dk/command/shell_init.rb index b55937ac5..cccf6c882 100644 --- a/lib/chef-dk/command/shell_init.rb +++ b/lib/chef-dk/command/shell_init.rb @@ -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 @@ -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: @@ -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? @@ -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