Class: Ronin::Core::CLI::CompletionCommand

Inherits:
Command
  • Object
show all
Includes:
CommandKit::Completion::Install
Defined in:
lib/ronin/core/cli/completion_command.rb

Overview

Common base class for all ronin-* completion commands.

Example

# lib/ronin/foo/cli/commands/completion.rb
require 'ronin/foo/root'
require 'ronin/core/cli/completion_command'

module Ronin
  module Foo
    class CLI
      class Command < Core::CLI::CompletionCommand

        man_dir File.join(ROOT,'man')
        man_page 'ronin-foo-completion.1'

        completion_file File.join(ROOT,'data','completions','ronin-foo')

      end
    end
  end
end

Since:

  • 0.2.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ CompletionCommand

Initializes the completion command.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for the command.

Since:

  • 0.2.0



114
115
116
117
118
# File 'lib/ronin/core/cli/completion_command.rb', line 114

def initialize(**kwargs)
  super(**kwargs)

  @mode = :print
end

Instance Attribute Details

#mode:print, ... (readonly)

The command mode.

Returns:

  • (:print, :install, :uninstall)

Since:

  • 0.2.0



106
107
108
# File 'lib/ronin/core/cli/completion_command.rb', line 106

def mode
  @mode
end

Class Method Details

.completion_file(new_completion_file = nil) ⇒ String

Gets or sets the completion file for the completion command.

Examples:

completion_file File.join(ROOT,'data','completions','ronin-foo')

Parameters:

  • new_completion_file (String, nil) (defaults to: nil)

    The optional path to the completion file.

Returns:

  • (String)

    The completion file path.

Raises:

  • (NotImplementedError)

    The command did not set the completion_file.

Since:

  • 0.2.0



73
74
75
76
77
78
79
# File 'lib/ronin/core/cli/completion_command.rb', line 73

def self.completion_file(new_completion_file=nil)
  if new_completion_file
    @completion_file = new_completion_file
  else
    @completion_file || raise(NotImplementedError,"#{self} did not set completion_file")
  end
end

Instance Method Details

#completion_fileString

The completion commands registered completion file.

Returns:

  • (String)

    The completion file path.

Raises:

  • (NotImplementedError)

    The command did not set the completion_file.

Since:

  • 0.2.0



129
130
131
# File 'lib/ronin/core/cli/completion_command.rb', line 129

def completion_file
  self.class.completion_file
end

#install_completion_file(completion_file = self.completion_file) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Installs the completion command's completion_file for the current SHELL.

Parameters:

  • completion_file (String) (defaults to: self.completion_file)

    The path to the completion file to install.

Since:

  • 0.2.0



185
186
187
# File 'lib/ronin/core/cli/completion_command.rb', line 185

def install_completion_file(completion_file=self.completion_file)
  super(completion_file)
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints the completion command's completion_file to stdout.

Parameters:

  • completion_file (String) (defaults to: self.completion_file)

    The path to the completion file to print.

Since:

  • 0.2.0



172
173
174
# File 'lib/ronin/core/cli/completion_command.rb', line 172

def print_completion_file(completion_file=self.completion_file)
  super(completion_file)
end

#runObject

Runs the completion command.

Since:

  • 0.2.0



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ronin/core/cli/completion_command.rb', line 136

def run
  if shell_type == :fish
    print_error "shell completions for the fish shell are not currently supported"
    exit(-1)
  end

  case @mode
  when :print
    print_completion_file
  when :install
    install_completion_file

    if shell_type == :zsh
      puts "Ensure that you have the following lines added to your ~/.zshrc:"
      puts
      puts "    autoload -Uz +X compinit && compinit"
      puts "    autoload -Uz +X bashcompinit && bashcompinit"
      puts
    end
  when :uninstall
    uninstall_completion_file

    puts "Completion rules successfully uninstalled. Please restart your shell."
  else
    raise(NotImplementedError,"mode not implemented: #{@mode.inspect}")
  end
end

#uninstall_completion_file(completion_file = self.completion_file) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Uninstalls the completion command's completion_file.

Parameters:

  • completion_file (String) (defaults to: self.completion_file)

    The path to the completion file to uninstall.

Since:

  • 0.2.0



197
198
199
# File 'lib/ronin/core/cli/completion_command.rb', line 197

def uninstall_completion_file(completion_file=self.completion_file)
  uninstall_completion_file_for(File.basename(completion_file))
end