class ChefCLI::Command::ShellInit
def check_license_acceptance
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
def completion_for(shell)
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
def completion_template_for(shell)
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
def emit_shell_cmd(cmd)
def emit_shell_cmd(cmd) msg(cmd) unless cmd.empty? end
def expand_completion_template_path(basename)
def expand_completion_template_path(basename) File.join(File.expand_path("../completions", __dir__), basename) end
def export(shell, var, val)
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
def fish_shell_export(var, val)
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
def initialize
def initialize super @shell_completion_template_context = nil end
def omnibus_root
def omnibus_root config[:omnibus_dir] || super end
def posix_shell_export(var, val)
def posix_shell_export(var, val) emit_shell_cmd(%Q{export #{var}="#{val}"}) end
def powershell_export(var, val)
def powershell_export(var, val) emit_shell_cmd(%Q{$env:#{var}="#{val}"}) end
def run(argv)
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
def shell_completion_template_context
def shell_completion_template_context @shell_completion_template_context ||= ShellCompletionTemplateContext.new end