Class: GDKBox::Completion

Inherits:
Object
  • Object
show all
Defined in:
lib/gdkbox/completion.rb

Overview

Generates shell completion scripts for the gdkbox CLI.

The list of subcommands and their flags is introspected from the Thor CLI so it stays in sync automatically. Box names and skill names are completed dynamically at completion time by shelling back out to gdkbox ls and gdkbox skills.

Constant Summary collapse

SHELLS =
%w[bash zsh].freeze
BOX_COMMANDS =

Subcommands whose first positional argument is an existing box.

%w[status dispatch ssh code install-agent set-key start stop rm add-skill].freeze

Instance Method Summary collapse

Constructor Details

#initialize(cli_class = CLI) ⇒ Completion

Returns a new instance of Completion.



16
17
18
# File 'lib/gdkbox/completion.rb', line 16

def initialize(cli_class = CLI)
  @cli = cli_class
end

Instance Method Details

#script(shell) ⇒ Object

The completion script for the given shell.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gdkbox/completion.rb', line 21

def script(shell)
  unless SHELLS.include?(shell)
    raise Error, "Unsupported shell '#{shell}'. Supported: #{SHELLS.join(', ')}."
  end

  body = function + "\ncomplete -F _gdkbox gdkbox\n"
  return body if shell == "bash"

  # zsh can run bash-style completion functions via bashcompinit.
  "autoload -U +X bashcompinit && bashcompinit\n#{body}"
end