Class: Pvectl::Commands::EditHosts
- Inherits:
-
Object
- Object
- Pvectl::Commands::EditHosts
- Defined in:
- lib/pvectl/commands/edit_hosts.rb,
sig/pvectl/commands/edit_hosts.rbs
Overview
Handler for the pvectl edit hosts command.
Opens /etc/hosts for a node in the user's editor. On save, POSTs the new content back via /nodes/node/hosts with the original digest for optimistic concurrency control.
The node identifier may be supplied either as a positional argument
(matching pvectl edit node NAME semantics) or via the --node flag.
Class Method Summary collapse
-
.execute(args, options, global_options) ⇒ Integer
Executes the edit hosts command.
Instance Method Summary collapse
-
#build_edit_service(connection) ⇒ Services::EditHosts
Builds the EditHosts service with a connection.
-
#build_editor_session ⇒ EditorSession?
Builds an editor session from --editor option.
-
#execute ⇒ Integer
Executes the edit flow.
-
#initialize(args, options, global_options) ⇒ EditHosts
constructor
Initializes the command.
-
#load_config ⇒ void
Loads configuration from file or environment.
-
#service_options ⇒ Hash
Builds service options from CLI options.
-
#usage_error(message) ⇒ Integer
Outputs usage error and returns exit code.
Constructor Details
#initialize(args, options, global_options) ⇒ EditHosts
Initializes the command.
37 38 39 40 41 |
# File 'lib/pvectl/commands/edit_hosts.rb', line 37 def initialize(args, , ) @args = args @options = @global_options = end |
Class Method Details
.execute(args, options, global_options) ⇒ Integer
Executes the edit hosts command.
28 29 30 |
# File 'lib/pvectl/commands/edit_hosts.rb', line 28 def self.execute(args, , ) new(args, , ).execute end |
Instance Method Details
#build_edit_service(connection) ⇒ Services::EditHosts
Builds the EditHosts service with a connection.
88 89 90 91 92 93 94 95 |
# File 'lib/pvectl/commands/edit_hosts.rb', line 88 def build_edit_service(connection) hosts_repo = Pvectl::Repositories::Hosts.new(connection) Pvectl::Services::EditHosts.new( hosts_repository: hosts_repo, editor_session: build_editor_session, options: ) end |
#build_editor_session ⇒ EditorSession?
Builds an editor session from --editor option.
118 119 120 121 122 123 |
# File 'lib/pvectl/commands/edit_hosts.rb', line 118 def build_editor_session editor_cmd = @options[:editor] return nil unless editor_cmd Pvectl::EditorSession.new(editor: ->(path) { system(editor_cmd, path) }) end |
#execute ⇒ Integer
Executes the edit flow.
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_hosts.rb', line 46 def execute node_name = @args.first || @options[:node] return usage_error("NODE is required (positional argument or --node flag)") if node_name.nil? || 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? if @options[:"dry-run"] $stdout.puts "(dry-run mode — no changes applied)" else $stdout.puts "/etc/hosts on 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.}" ExitCodes::GENERAL_ERROR end |
#load_config ⇒ void
This method returns an undefined value.
Loads configuration from file or environment.
100 101 102 103 104 |
# File 'lib/pvectl/commands/edit_hosts.rb', line 100 def load_config service = Pvectl::Config::Service.new service.load(config: @global_options[:config]) @config = service.current_config end |
#service_options ⇒ Hash
Builds service options from CLI options.
109 110 111 112 113 |
# File 'lib/pvectl/commands/edit_hosts.rb', line 109 def opts = {} opts[:dry_run] = true if @options[:"dry-run"] opts end |
#usage_error(message) ⇒ Integer
Outputs usage error and returns exit code.
129 130 131 132 |
# File 'lib/pvectl/commands/edit_hosts.rb', line 129 def usage_error() $stderr.puts "Error: #{}" ExitCodes::USAGE_ERROR end |