Module: Pvectl::Commands::SetResourceCommand Abstract

Included in:
SetContainer, SetNode, SetVm
Defined in:
lib/pvectl/commands/set_resource_command.rb

Overview

This module is abstract.

Include this module and implement template methods.

Shared functionality for resource set commands.

Template method pattern: provides common set workflow (config loading, key-value parsing, diff display, dry-run) while specialization classes define resource-specific hooks.

Examples:

Specialization

class SetVm
  include SetResourceCommand
  private
  def resource_label = "VM"
  def resource_id_label = "VMID"
  # ...
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Hook called when module is included.

Parameters:

  • base (Class)

    the class including this module



39
40
41
# File 'lib/pvectl/commands/set_resource_command.rb', line 39

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#executeInteger

Executes the set command.

Returns:

  • (Integer)

    exit code



57
58
59
60
61
62
63
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
# File 'lib/pvectl/commands/set_resource_command.rb', line 57

def execute
  resource_id = @args.first
  return usage_error("#{resource_id_label} is required") unless resource_id

  key_values = parse_key_values(@args[1..])
  return usage_error("At least one key=value pair is required") if key_values.empty?

  load_config
  connection = Pvectl::Connection.new(@config)
  service = build_set_service(connection)
  result = service.execute(**execute_params(resource_id, key_values))

  if result.nil?
    $stdout.puts "No changes detected."
    return ExitCodes::SUCCESS
  end

  if result.successful?
    display_diff(result)
    if @options[:"dry-run"]
      $stdout.puts "(dry-run mode — no changes applied)"
    else
      $stdout.puts "#{resource_label} #{resource_id} updated successfully."
    end
    ExitCodes::SUCCESS
  else
    $stderr.puts "Error: #{result.error}"
    ExitCodes::GENERAL_ERROR
  end
rescue Pvectl::Config::ConfigNotFoundError,
       Pvectl::Config::InvalidConfigError,
       Pvectl::Config::ContextNotFoundError,
       Pvectl::Config::ClusterNotFoundError,
       Pvectl::Config::UserNotFoundError
  raise
rescue StandardError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::GENERAL_ERROR
end

#initialize(args, options, global_options) ⇒ Object

Initializes a set command.

Parameters:

  • args (Array<String>)

    command arguments

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options



48
49
50
51
52
# File 'lib/pvectl/commands/set_resource_command.rb', line 48

def initialize(args, options, global_options)
  @args = args
  @options = options
  @global_options = global_options
end