Module: Pvectl::Commands::CreateResourceCommand Abstract
- Included in:
- CreateContainer, CreateVm
- Defined in:
- lib/pvectl/commands/create_resource_command.rb,
sig/pvectl/commands/create_resource_command.rbs
Overview
Include this module and implement template methods.
Shared functionality for resource creation commands.
Template method pattern: provides common create workflow (interactive detection, confirmation, dry-run, config loading) while specialization classes define resource-specific hooks.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ void
Hook called when module is included.
Instance Method Summary collapse
-
#build_create_service(connection, task_repo) ⇒ Object
Create service instance.
-
#build_params_from_flags ⇒ Hash
Parameters built from CLI flags.
-
#build_wizard ⇒ Object
Wizard instance for interactive mode.
- #display_resource_summary(params) ⇒ void
-
#display_summary(params) ⇒ void
Displays the creation summary.
-
#display_summary_and_confirm(params) ⇒ Symbol?
Displays a creation summary and prompts for confirmation.
-
#execute ⇒ Integer
Executes the create command.
-
#initialize(args, options, global_options) ⇒ CreateResourceCommand
Initializes a create command.
-
#interactive_mode? ⇒ Boolean
Determines whether to use interactive mode.
-
#load_config ⇒ void
Loads configuration from file or environment.
- #output_result(result) ⇒ void
-
#perform_create ⇒ Integer
Validates flags and performs flag-based creation.
-
#perform_create_with_params(params) ⇒ Integer
Creates a resource with the given parameters.
-
#perform_interactive ⇒ Integer
Runs the interactive wizard flow.
-
#required_params_missing? ⇒ Boolean
True if required params for non-interactive mode are missing.
-
#resolve_default_node ⇒ String?
Resolves the default node from configuration.
-
#resource_id_label ⇒ String
Human label for resource ID ("VMID" or "CTID").
-
#resource_label ⇒ String
Human label for resource ("VM" or "container").
-
#service_options ⇒ Hash
Builds service options from command options.
-
#usage_error(message) ⇒ Integer
Outputs usage error and returns exit code.
Class Method Details
.included(base) ⇒ void
This method returns an undefined value.
Hook called when module is included.
39 40 41 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 39 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#build_create_service(connection, task_repo) ⇒ Object
Returns create service instance.
97 98 99 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 97 def build_create_service(connection, task_repo) raise NotImplementedError, "#{self.class} must implement #build_create_service" end |
#build_params_from_flags ⇒ Hash
Returns parameters built from CLI flags.
85 86 87 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 85 def build_params_from_flags raise NotImplementedError, "#{self.class} must implement #build_params_from_flags" end |
#build_wizard ⇒ Object
Returns wizard instance for interactive mode.
90 91 92 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 90 def build_wizard raise NotImplementedError, "#{self.class} must implement #build_wizard" end |
#display_resource_summary(params) ⇒ void
This method returns an undefined value.
109 110 111 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 109 def display_resource_summary(params) raise NotImplementedError, "#{self.class} must implement #display_resource_summary" end |
#display_summary(params) ⇒ void
This method returns an undefined value.
Displays the creation summary.
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 193 def display_summary(params) $stdout.puts "" $stdout.puts " Create #{resource_label} - Summary" $stdout.puts " #{'─' * 40}" $stdout.puts " #{resource_id_label}:#{' ' * (10 - resource_id_label.length)}#{params[:vmid] || params[:ctid] || '(auto)'}" display_resource_summary(params) $stdout.puts " #{'─' * 40}" $stdout.puts " (dry-run mode -- no #{resource_label} will be created)" if [:"dry-run"] $stdout.puts "" end |
#display_summary_and_confirm(params) ⇒ Symbol?
Displays a creation summary and prompts for confirmation.
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 178 def display_summary_and_confirm(params) display_summary(params) return nil if [:yes] $stdout.print "Create this #{resource_label}? [y/N] " $stdout.flush answer = $stdin.gets&.strip&.downcase answer == "y" ? nil : :cancelled end |
#execute ⇒ Integer
Executes the create command.
57 58 59 60 61 62 63 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 57 def execute if interactive_mode? perform_interactive else perform_create end end |
#initialize(args, options, global_options) ⇒ CreateResourceCommand
Initializes a create command.
48 49 50 51 52 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 48 def initialize(args, , ) @args = args = = end |
#interactive_mode? ⇒ Boolean
Determines whether to use interactive mode.
118 119 120 121 122 123 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 118 def interactive_mode? return true if [:interactive] return false if [:"no-interactive"] $stdin.tty? && required_params_missing? end |
#load_config ⇒ void
This method returns an undefined value.
Loads configuration from file or environment.
217 218 219 220 221 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 217 def load_config service = Pvectl::Config::Service.new service.load(config: [:config]) @config = service.current_config end |
#output_result(result) ⇒ void
This method returns an undefined value.
103 104 105 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 103 def output_result(result) raise NotImplementedError, "#{self.class} must implement #output_result" end |
#perform_create ⇒ Integer
Validates flags and performs flag-based creation.
140 141 142 143 144 145 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 140 def perform_create params = build_params_from_flags perform_create_with_params(params) rescue ArgumentError => e usage_error(e.) end |
#perform_create_with_params(params) ⇒ Integer
Creates a resource with the given parameters.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 151 def perform_create_with_params(params) return ExitCodes::SUCCESS if display_summary_and_confirm(params) == :cancelled return ExitCodes::SUCCESS if [:"dry-run"] load_config connection = Pvectl::Connection.new(@config) task_repo = Pvectl::Repositories::Task.new(connection) service = build_create_service(connection, task_repo) result = service.execute(**params) output_result(result) result.failed? ? ExitCodes::GENERAL_ERROR : ExitCodes::SUCCESS 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 |
#perform_interactive ⇒ Integer
Runs the interactive wizard flow.
128 129 130 131 132 133 134 135 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 128 def perform_interactive wizard = build_wizard wizard_params = wizard.run return ExitCodes::SUCCESS unless wizard_params [:start] = wizard_params.delete(:start) if wizard_params.key?(:start) perform_create_with_params(wizard_params) end |
#required_params_missing? ⇒ Boolean
Returns true if required params for non-interactive mode are missing.
80 81 82 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 80 def required_params_missing? raise NotImplementedError, "#{self.class} must implement #required_params_missing?" end |
#resolve_default_node ⇒ String?
Resolves the default node from configuration.
207 208 209 210 211 212 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 207 def resolve_default_node load_config unless @config @config&.default_node rescue StandardError nil end |
#resource_id_label ⇒ String
Returns human label for resource ID ("VMID" or "CTID").
75 76 77 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 75 def resource_id_label raise NotImplementedError, "#{self.class} must implement #resource_id_label" end |
#resource_label ⇒ String
Returns human label for resource ("VM" or "container").
70 71 72 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 70 def resource_label raise NotImplementedError, "#{self.class} must implement #resource_label" end |
#service_options ⇒ Hash
Builds service options from command options.
226 227 228 229 230 231 232 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 226 def opts = {} opts[:timeout] = [:timeout] if [:timeout] opts[:async] = true if [:async] opts[:start] = true if [:start] opts end |
#usage_error(message) ⇒ Integer
Outputs usage error and returns exit code.
238 239 240 241 |
# File 'lib/pvectl/commands/create_resource_command.rb', line 238 def usage_error() $stderr.puts "Error: #{message}" ExitCodes::USAGE_ERROR end |