Class: ChefDK::Command::ShellInit

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-dk/command/shell_init.rb

Constant Summary collapse

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

Instance Method Summary collapse

Methods inherited from Base

#initialize, #needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_install?, #stderr, #stdout, #system_command

Constructor Details

This class inherits a constructor from ChefDK::Command::Base

Instance Method Details

#export(shell, var, val) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/chef-dk/command/shell_init.rb', line 87

def export(shell, var, val)
  case shell
  when 'sh', 'bash', 'zsh'
    posix_shell_export(var, val)
  when 'fish'
    fish_shell_export(var, val)
  when 'powershell', 'posh'
    powershell_export(var, val)
  end
end

#fish_shell_export(var, val) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef-dk/command/shell_init.rb', line 102

def fish_shell_export(var, val)
  # Fish's syntax for setting PATH is special. Path elements are
  # divided by spaces (instead of colons). We also send STDERR to
  # /dev/null to avoid Fish's helpful warnings about nonexistent
  # PATH elements.
  if var == 'PATH'
    msg(%Q(set -gx #{var} "#{val.split(':').join('" "')}" 2>/dev/null;))
  else
    msg(%Q(set -gx #{var} "#{val}";))
  end
end

#omnibus_rootObject



59
60
61
# File 'lib/chef-dk/command/shell_init.rb', line 59

def omnibus_root
  config[:omnibus_dir] || super
end

#posix_shell_export(var, val) ⇒ Object



98
99
100
# File 'lib/chef-dk/command/shell_init.rb', line 98

def posix_shell_export(var, val)
  msg(%Q(export #{var}="#{val}"))
end

#powershell_export(var, val) ⇒ Object



114
115
116
# File 'lib/chef-dk/command/shell_init.rb', line 114

def powershell_export(var, val)
  msg(%Q($env:#{var}="#{val}"))
end

#run(argv) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef-dk/command/shell_init.rb', line 63

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?
    err("Please specify what shell you are using\n")
    err(opt_parser.to_s)
    return 1
  elsif !SUPPORTED_SHELLS.include?(shell_name)
    err("Shell `#{shell_name}' is not currently supported")
    err("Supported shells are: #{SUPPORTED_SHELLS.join(' ')}")
    return 1
  end

  env = omnibus_env.dup
  path = env.delete("PATH")
  export(shell_name, "PATH", path)
  env.each do |var_name, value|
    export(shell_name, var_name, value)
  end
  0
end