Class: Pvectl::Commands::Config::UseContext

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

Overview

Handler for the ‘pvectl config use-context` command.

Switches the active context in the configuration file. The context name must exist in the configuration.

Examples:

Usage

pvectl config use-context production
pvectl config use-context dev

Class Method Summary collapse

Class Method Details

.execute(context_name, global_options) ⇒ Integer

Executes the use-context command.

Parameters:

  • context_name (String)

    name of the context to switch to

  • global_options (Hash)

    global CLI options (includes :config path)

Returns:

  • (Integer)

    exit code (0 for success)

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pvectl/commands/config/use_context.rb', line 53

def self.execute(context_name, global_options)
  config_path = global_options[:config]
  service = Pvectl::Config::Service.new
  service.load(config: config_path)

  service.use_context(context_name)

  puts "Switched to context \"#{context_name}\"."
  0
rescue Pvectl::Config::ContextNotFoundError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::CONFIG_ERROR
end

.register_subcommand(parent) ⇒ void

This method returns an undefined value.

Registers the use-context subcommand.

Parameters:

  • parent (GLI::Command)

    parent config command



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pvectl/commands/config/use_context.rb', line 20

def self.register_subcommand(parent)
  parent.desc "Switch to a different context"
  parent.long_desc <<~HELP
    Switch the active context. The context determines which Proxmox
    cluster and credentials are used for subsequent commands.

    EXAMPLES
      Switch to production:
        $ pvectl config use-context production

    NOTES
      Changes are persisted to the config file immediately.
      Use PVECTL_CONTEXT env var for temporary overrides.
  HELP
  parent.command :"use-context" do |use_ctx|
    use_ctx.arg_name "CONTEXT_NAME"
    use_ctx.action do |global_options, _options, args|
      if args.empty?
        $stderr.puts "Error: context name is required"
        exit ExitCodes::USAGE_ERROR
      end
      exit_code = execute(args[0], global_options)
      exit exit_code if exit_code != 0
    end
  end
end