Class: Pvectl::Commands::Config::SetContext
- Inherits:
-
Object
- Object
- Pvectl::Commands::Config::SetContext
- Defined in:
- lib/pvectl/commands/config/set_context.rb,
sig/pvectl/commands/config/set_context.rbs
Overview
Handler for the pvectl config set-context command.
Creates a new context or modifies an existing one in the configuration. Requires --cluster and --user flags for new contexts.
Class Method Summary collapse
-
.execute(context_name, options, global_options) ⇒ Integer
Executes the set-context command.
-
.register_subcommand(parent) ⇒ void
Registers the set-context subcommand.
-
.validate_cluster_exists!(service, cluster_name) ⇒ void
Validates that the cluster exists in the configuration.
-
.validate_user_exists!(service, user_name) ⇒ void
Validates that the user exists in the configuration.
Class Method Details
.execute(context_name, options, global_options) ⇒ Integer
Executes the set-context command.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/pvectl/commands/config/set_context.rb', line 64 def self.execute(context_name, , ) config_path = [:config] service = Pvectl::Config::Service.new service.load(config: config_path) existing_context = service.context(context_name) action = existing_context ? "modified" : "created" # Use existing values if not provided cluster = [:cluster] || existing_context&.cluster_ref user = [:user] || existing_context&.user_ref default_node = [:"default-node"] || [:default_node] || existing_context&.default_node # Validate required fields for new contexts if cluster.nil? || user.nil? $stderr.puts "Error: --cluster and --user are required for new contexts" return ExitCodes::USAGE_ERROR end # Validate cluster and user exist validate_cluster_exists!(service, cluster) validate_user_exists!(service, user) service.set_context( name: context_name, cluster: cluster, user: user, default_node: default_node ) puts "Context \"#{context_name}\" #{action}." 0 rescue Pvectl::Config::ClusterNotFoundError => e $stderr.puts "Error: #{e.message}" ExitCodes::CONFIG_ERROR rescue Pvectl::Config::UserNotFoundError => e $stderr.puts "Error: #{e.message}" ExitCodes::CONFIG_ERROR end |
.register_subcommand(parent) ⇒ void
This method returns an undefined value.
Registers the set-context subcommand.
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 46 47 48 49 50 51 52 53 54 |
# File 'lib/pvectl/commands/config/set_context.rb', line 20 def self.register_subcommand(parent) parent.desc "Create or modify a context" parent.long_desc " Create a new context or modify an existing one. A context links\n a cluster definition with user credentials.\n\n EXAMPLES\n Create a new context:\n $ pvectl config set-context prod --cluster=production --user=admin-prod\n\n Set a default node:\n $ pvectl config set-context prod --default-node=pve1\n HELP\n parent.command :\"set-context\" do |set_ctx|\n set_ctx.arg_name \"CONTEXT_NAME\"\n\n set_ctx.desc \"Cluster name\"\n set_ctx.flag [:cluster]\n\n set_ctx.desc \"User name\"\n set_ctx.flag [:user]\n\n set_ctx.desc \"Default node\"\n set_ctx.flag [:\"default-node\"]\n\n set_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], options, global_options)\n exit exit_code if exit_code != 0\n end\n end\nend\n" |
.validate_cluster_exists!(service, cluster_name) ⇒ void
This method returns an undefined value.
Validates that the cluster exists in the configuration.
109 110 111 112 113 114 115 116 117 |
# File 'lib/pvectl/commands/config/set_context.rb', line 109 def self.validate_cluster_exists!(service, cluster_name) clusters = service.raw_config["clusters"] || [] found = clusters.any? { |c| c["name"] == cluster_name } unless found available = clusters.map { |c| c["name"] }.join(", ") raise Pvectl::Config::ClusterNotFoundError, "Cluster '#{cluster_name}' not found in configuration. Available: #{available}" end end |
.validate_user_exists!(service, user_name) ⇒ void
This method returns an undefined value.
Validates that the user exists in the configuration.
124 125 126 127 128 129 130 131 132 |
# File 'lib/pvectl/commands/config/set_context.rb', line 124 def self.validate_user_exists!(service, user_name) users = service.raw_config["users"] || [] found = users.any? { |u| u["name"] == user_name } unless found available = users.map { |u| u["name"] }.join(", ") raise Pvectl::Config::UserNotFoundError, "User '#{user_name}' not found in configuration. Available: #{available}" end end |