Class: Pvectl::Commands::EditDns

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

Overview

Handler for the pvectl edit dns command.

Opens the DNS resolver configuration for a node in a YAML editor. On save, applies the changes via PUT /nodes/node/dns.

The node identifier may be supplied either as a positional argument (matching pvectl edit node NAME semantics) or via the --node flag.

Examples:

Basic usage

pvectl edit dns pve1
pvectl edit dns --node pve1

Dry-run mode

pvectl edit dns pve1 --dry-run

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options, global_options) ⇒ EditDns

Initializes the command.

Parameters:

  • args (Array<String>)

    command arguments

  • options (Hash)

    command options

  • global_options (Hash)

    global CLI options



36
37
38
39
40
# File 'lib/pvectl/commands/edit_dns.rb', line 36

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

Class Method Details

.execute(args, options, global_options) ⇒ Integer

Executes the edit dns command.

Parameters:

  • args (Array<String>)

    command arguments (positional NODE)

  • options (Hash)

    command options (:node, :editor, :"dry-run")

  • global_options (Hash)

    global CLI options

Returns:

  • (Integer)

    exit code



27
28
29
# File 'lib/pvectl/commands/edit_dns.rb', line 27

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

Instance Method Details

#build_edit_service(connection) ⇒ Services::EditDns

Builds the EditDns service with a connection.

Parameters:

Returns:



88
89
90
91
92
93
94
95
# File 'lib/pvectl/commands/edit_dns.rb', line 88

def build_edit_service(connection)
  dns_repo = Pvectl::Repositories::Dns.new(connection)
  Pvectl::Services::EditDns.new(
    dns_repository: dns_repo,
    editor_session: build_editor_session,
    options: service_options
  )
end

#build_editor_sessionEditorSession?

Builds an editor session from --editor option.

Returns:

  • (EditorSession, nil)

    editor session or nil if no --editor flag



132
133
134
135
136
137
# File 'lib/pvectl/commands/edit_dns.rb', line 132

def build_editor_session
  editor_cmd = @options[:editor]
  return nil unless editor_cmd

  Pvectl::EditorSession.new(editor: ->(path) { system(editor_cmd, path) })
end

#display_diff(result) ⇒ void

This method returns an undefined value.

Displays a formatted diff from the operation result.

Parameters:



101
102
103
104
105
106
107
108
109
# File 'lib/pvectl/commands/edit_dns.rb', line 101

def display_diff(result)
  diff = result.resource&.dig(:diff)
  return unless diff
  return if diff[:changed].empty? && diff[:added].empty? && diff[:removed].empty?

  $stdout.puts "\nChanges:"
  $stdout.puts Pvectl::ConfigSerializer.format_diff(diff)
  $stdout.puts ""
end

#executeInteger

Executes the edit flow.

Returns:

  • (Integer)

    exit code



45
46
47
48
49
50
51
52
53
54
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
# File 'lib/pvectl/commands/edit_dns.rb', line 45

def execute
  node_name = @args.first || @options[:node]
  return usage_error("NODE is required (positional argument or --node flag)") unless node_name && !node_name.empty?

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

  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 "DNS configuration for node #{node_name} 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

#load_configvoid

This method returns an undefined value.

Loads configuration from file or environment.



114
115
116
117
118
# File 'lib/pvectl/commands/edit_dns.rb', line 114

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

#service_optionsHash

Builds service options from CLI options.

Returns:

  • (Hash)

    service options



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

def service_options
  opts = {}
  opts[:dry_run] = true if @options[:"dry-run"]
  opts
end

#usage_error(message) ⇒ Integer

Outputs usage error and returns exit code.

Parameters:

  • message (String)

    error message

Returns:

  • (Integer)

    exit code



143
144
145
146
# File 'lib/pvectl/commands/edit_dns.rb', line 143

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