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

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

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:

  • name of the context to switch to

  • global CLI options (includes :config path)

Returns:

  • exit code (0 for success)

Raises:

  • if context doesn't exist



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 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 "    Switch the active context. The context determines which Proxmox\n    cluster and credentials are used for subsequent commands.\n\n    EXAMPLES\n      Switch to production:\n        $ pvectl config use-context production\n\n    NOTES\n      Changes are persisted to the config file immediately.\n      Use PVECTL_CONTEXT env var for temporary overrides.\n  HELP\n  parent.command :\"use-context\" do |use_ctx|\n    use_ctx.arg_name \"CONTEXT_NAME\"\n    use_ctx.action do |global_options, _options, args|\n      if args.empty?\n        $stderr.puts \"Error: context name is required\"\n        exit ExitCodes::USAGE_ERROR\n      end\n      exit_code = execute(args[0], global_options)\n      exit exit_code if exit_code != 0\n    end\n  end\nend\n"