Class: Pvectl::Commands::DeleteBackup

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

Overview

Handler for the pvectl delete backup command.

Deletes a backup identified by its full volume ID (volid). Requires --yes flag for confirmation.

Examples:

Delete a backup

pvectl delete backup local:backup/vzdump-qemu-100-2024_01_15.vma.zst --yes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_type, args, options, global_options) ⇒ DeleteBackup

Initializes a delete backup command.



31
32
33
34
35
36
37
# File 'lib/pvectl/commands/delete_backup.rb', line 31

def initialize(resource_type, args, options, global_options)
  @resource_type = resource_type
  @args = Array(args).compact
  @options = options
  @global_options = global_options
  @volid = @args.first
end

Class Method Details

.execute(resource_type, args, options, global_options) ⇒ Integer

Executes the delete backup command.



21
22
23
# File 'lib/pvectl/commands/delete_backup.rb', line 21

def self.execute(resource_type, args, options, global_options)
  new(resource_type, args, options, global_options).execute
end

Instance Method Details

#determine_exit_code(results) ⇒ Integer

Determines exit code based on results.



122
123
124
125
126
127
# File 'lib/pvectl/commands/delete_backup.rb', line 122

def determine_exit_code(results)
  return ExitCodes::SUCCESS if results.all?(&:successful?)
  return ExitCodes::SUCCESS if results.all?(&:pending?)

  ExitCodes::GENERAL_ERROR
end

#executeInteger

Executes the delete backup command.



42
43
44
45
46
47
48
# File 'lib/pvectl/commands/delete_backup.rb', line 42

def execute
  return usage_error("Resource type required (backup)") unless @resource_type == "backup"
  return usage_error("Backup volid is required") if @volid.nil? || @volid.empty?
  return usage_error("Confirmation required: use --yes to confirm deletion") unless @options[:yes]

  perform_operation
end

#load_configvoid

This method returns an undefined value.

Loads configuration from file or environment.



88
89
90
91
92
# File 'lib/pvectl/commands/delete_backup.rb', line 88

def load_config
  service = Pvectl::Config::Service.new
  service.load(config: @global_options[:config])
  @config = service.current_config
end

#output_results(results) ⇒ void

This method returns an undefined value.

Outputs operation results using the configured formatter.



108
109
110
111
112
113
114
115
116
# File 'lib/pvectl/commands/delete_backup.rb', line 108

def output_results(results)
  presenter = Pvectl::Presenters::SnapshotOperationResult.new
  format = @global_options[:output] || "table"
  color_flag = @global_options[:color]

  formatter = Pvectl::Formatters::Registry.for(format)
  output = formatter.format(results, presenter, color: color_flag)
  puts output
end

#perform_operationInteger

Performs the backup deletion operation.



55
56
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
# File 'lib/pvectl/commands/delete_backup.rb', line 55

def perform_operation
  load_config
  connection = Pvectl::Connection.new(@config)

  backup_repo = Pvectl::Repositories::Backup.new(connection)
  resolver = Pvectl::Utils::ResourceResolver.new(connection)
  task_repo = Pvectl::Repositories::Task.new(connection)

  service = Pvectl::Services::Backup.new(
    backup_repo: backup_repo,
    resource_resolver: resolver,
    task_repo: task_repo,
    options: service_options
  )

  result = service.delete(@volid)

  output_results([result])
  determine_exit_code([result])
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

#service_optionsHash

Builds service options from command options.



97
98
99
100
101
102
# File 'lib/pvectl/commands/delete_backup.rb', line 97

def service_options
  opts = {}
  opts[:timeout] = @options[:timeout] if @options[:timeout]
  opts[:async] = true if @options[:async]
  opts
end

#usage_error(message) ⇒ Integer

Outputs usage error and returns exit code.



133
134
135
136
# File 'lib/pvectl/commands/delete_backup.rb', line 133

def usage_error(message)
  $stderr.puts "Error: #{message}"
  ExitCodes::USAGE_ERROR
end