Class: Mortar::InstallCompletionsCommand

Inherits:
Command
  • Object
show all
Includes:
TTYHelper
Defined in:
lib/mortar/install_completions_command.rb

Constant Summary collapse

DEFAULT_PATHS =
[
  '/etc/bash_completion.d/mortar.bash',
  '/usr/local/etc/bash_completion.d/mortar.bash',
  '/usr/share/zsh/site-functions/_mortar',
  '/usr/local/share/zsh/site-functions/_mortar',
  '/usr/local/share/zsh-completions/_mortar',
  File.join(Dir.home, '.bash_completion.d', 'mortar.bash')
].freeze
COMPLETION_FILE_PATH =
File.expand_path(
  '../../opt/bash-completion.sh',
  Pathname.new(__dir__).realpath
).freeze

Constants inherited from Command

Command::CHECKSUM_ANNOTATION, Command::LABEL

Instance Method Summary collapse

Methods included from TTYHelper

#pastel

Instance Method Details

#executeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mortar/install_completions_command.rb', line 25

def execute
  return uninstall if remove?

  installed = []

  DEFAULT_PATHS.each do |path|
    next unless File.directory?(File.dirname(path))

    begin
      FileUtils.ln_sf(COMPLETION_FILE_PATH, path)
      installed << path
    rescue Errno::EACCES, Errno::EPERM
      nil # To keep Mr. Rubocop happy
    end
  end

  if installed.empty?
    warn "Installation failed"
    warn "Try with sudo or set up user bash completions in ~/.bash_completion to include files from ~/.bash_completion.d"
    exit 1
  else
    puts "Completions installed to:"
    installed.each do |path|
      puts "  - #{path}"
    end
    puts
    puts "The completions will be reloaded when you start a new shell."
    puts "To load now, use:"
    puts pastel.cyan("  source \"#{COMPLETION_FILE_PATH}\"")
    exit 0
  end
end

#uninstallObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mortar/install_completions_command.rb', line 58

def uninstall
  failures = false
  DEFAULT_PATHS.each do |path|
    begin
      if File.exist?(path)
        File.unlink(path)
        puts "Removed #{path}"
      end
    rescue Errno::EACCESS, Errno::EPERM
      failures = true
      warn "Failed to remove #{path} : permission denied"
    end
  end
  exit 1 unless failures
end