Class: Pvectl::Commands::Config::GetContexts

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/commands/config/get_contexts.rb

Overview

Handler for the ‘pvectl config get-contexts` command.

Lists all contexts defined in the configuration file with an indicator showing which context is currently active. Uses the unified OutputHelper for formatting output.

Examples:

Usage

pvectl config get-contexts
pvectl config get-contexts -o json
pvectl config get-contexts -o yaml
pvectl config get-contexts -o wide
pvectl config get-contexts --no-color

Class Method Summary collapse

Class Method Details

.execute(global_options) ⇒ Integer

Executes the get-contexts command.

Parameters:

  • global_options (Hash)

    global CLI options (includes :config, :output, :color)

Returns:

  • (Integer)

    exit code (0 for success)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pvectl/commands/config/get_contexts.rb', line 45

def self.execute(global_options)
  config_path = global_options[:config]

  service = Pvectl::Config::Service.new
  service.load(config: config_path)

  contexts = service.contexts
  current_context_name = service.current_context_name
  presenter = Pvectl::Presenters::Config::Context.new

  Pvectl::Formatters::OutputHelper.print(
    data: contexts,
    presenter: presenter,
    format: global_options[:output] || "table",
    color_flag: global_options[:color],
    current_context: current_context_name
  )

  0
end

.register_subcommand(parent) ⇒ void

This method returns an undefined value.

Registers the get-contexts subcommand.

Parameters:

  • parent (GLI::Command)

    parent config command



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pvectl/commands/config/get_contexts.rb', line 24

def self.register_subcommand(parent)
  parent.desc "List all available contexts"
  parent.long_desc <<~HELP
    List all contexts defined in the configuration file. The currently
    active context is marked with an asterisk (*).

    EXAMPLES
      $ pvectl config get-contexts
  HELP
  parent.command :"get-contexts" do |get_ctx|
    get_ctx.action do |global_options, _options, _args|
      exit_code = execute(global_options)
      exit exit_code if exit_code != 0
    end
  end
end