Module: Pvectl::Commands::EditResourceCommand Abstract

Included in:
EditContainer, EditNode, EditVm
Defined in:
lib/pvectl/commands/edit_resource_command.rb

Overview

This module is abstract.

Include this module and implement template methods.

Shared functionality for resource edit commands.

Template method pattern: provides common edit workflow (config loading, editor session, diff display, dry-run) while specialization classes define resource-specific hooks.

Examples:

Specialization

class EditVm
  include EditResourceCommand
  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/edit_resource_command.rb', line 39

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

Instance Method Details

#executeInteger

Executes the edit command.

Loads configuration, builds the edit service, and runs it. Handles cancelled edits, successful updates with diff display, dry-run mode, and errors.

Returns:

  • (Integer)

    exit code



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
96
# File 'lib/pvectl/commands/edit_resource_command.rb', line 61

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

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

  if result.nil?
    $stdout.puts "Edit cancelled, no changes made."
    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 an edit 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/edit_resource_command.rb', line 48

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