Class: ChefCLI::Command::ShellInit

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-cli/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

#needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #package_home, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initializeShellInit

Returns a new instance of ShellInit.



79
80
81
82
# File 'lib/chef-cli/command/shell_init.rb', line 79

def initialize
  super
  @shell_completion_template_context = nil
end

Instance Method Details

#check_license_acceptanceObject



180
181
182
183
# File 'lib/chef-cli/command/shell_init.rb', line 180

def check_license_acceptance
  # It gives a very weird error if users try to eval the shell-init command and it requests the
  # license from them. Instead, let users shell-init without accepting the license.
end

#completion_for(shell) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/chef-cli/command/shell_init.rb', line 118

def completion_for(shell)
  return "" unless (completion_template_basename = completion_template_for(shell))

  completion_template_path = expand_completion_template_path(completion_template_basename)
  erb = ERB.new(File.read(completion_template_path), nil, "-")
  context_binding = shell_completion_template_context.get_binding
  erb.result(context_binding)
end

#completion_template_for(shell) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chef-cli/command/shell_init.rb', line 127

def completion_template_for(shell)
  case shell
  when "bash"
    "bash.sh.erb"
  when "fish"
    "chef.fish.erb"
  when "zsh"
    "zsh.zsh.erb"
  else
    # Pull requests accepted!
    nil
  end
end

#emit_shell_cmd(cmd) ⇒ Object



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

def emit_shell_cmd(cmd)
  msg(cmd) unless cmd.empty?
end

#expand_completion_template_path(basename) ⇒ Object



141
142
143
# File 'lib/chef-cli/command/shell_init.rb', line 141

def expand_completion_template_path(basename)
  File.join(File.expand_path("../../completions", __FILE__), basename)
end

#export(shell, var, val) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/chef-cli/command/shell_init.rb', line 149

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



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/chef-cli/command/shell_init.rb', line 164

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"
    emit_shell_cmd(%Q{set -gx #{var} "#{val.split(":").join('" "')}" 2>/dev/null;})
  else
    emit_shell_cmd(%Q{set -gx #{var} "#{val}";})
  end
end

#omnibus_rootObject



84
85
86
# File 'lib/chef-cli/command/shell_init.rb', line 84

def omnibus_root
  config[:omnibus_dir] || super
end

#posix_shell_export(var, val) ⇒ Object



160
161
162
# File 'lib/chef-cli/command/shell_init.rb', line 160

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

#powershell_export(var, val) ⇒ Object



176
177
178
# File 'lib/chef-cli/command/shell_init.rb', line 176

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

#run(argv) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef-cli/command/shell_init.rb', line 88

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

  emit_shell_cmd(completion_for(shell_name))
  0
end

#shell_completion_template_contextObject



145
146
147
# File 'lib/chef-cli/command/shell_init.rb', line 145

def shell_completion_template_context
  @shell_completion_template_context ||= ShellCompletionTemplateContext.new
end