Class: Aspera::Cli::Runner
- Inherits:
-
Object
- Object
- Aspera::Cli::Runner
- Defined in:
- lib/aspera/cli/runner.rb
Overview
The main CLI class
Constant Summary collapse
- STATUS_FIELD =
Plugins store transfer result using this key and use result_transfer_multiple()
'status'
Class Method Summary collapse
-
.result_transfer(statuses) ⇒ Result
Process statuses of finished transfer sessions.
-
.result_transfer_multiple(status_table) ⇒ Result
Used when one command executes several transfer jobs (each job being possibly multi session) Each element has a key STATUS_FIELD which contains the result of possibly multiple sessions.
Instance Method Summary collapse
-
#initialize(argv) ⇒ nil
constructor
Minimum initialization, no exception raised.
-
#run ⇒ nil
This is the main function called by initial script just after constructor Processes command line arguments, executes commands, and handles exceptions.
-
#show_usage(all: true, exit: true) ⇒ nil
Display usage information and help.
Constructor Details
Class Method Details
.result_transfer(statuses) ⇒ Result
Process statuses of finished transfer sessions
41 42 43 44 45 |
# File 'lib/aspera/cli/runner.rb', line 41 def result_transfer(statuses) worst = TransferAgent.session_status(statuses) raise worst unless worst.eql?(:success) return Result::Nothing.new end |
.result_transfer_multiple(status_table) ⇒ Result
Used when one command executes several transfer jobs (each job being possibly multi session) Each element has a key STATUS_FIELD which contains the result of possibly multiple sessions
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/aspera/cli/runner.rb', line 51 def result_transfer_multiple(status_table) global_status = :success # Transform status array into string and find if there was problem status_table.each do |item| worst = TransferAgent.session_status(item[STATUS_FIELD]) global_status = worst unless worst.eql?(:success) item[STATUS_FIELD] = item[STATUS_FIELD].join(',') end raise global_status unless global_status.eql?(:success) return Result::ObjectList.new(status_table) end |
Instance Method Details
#run ⇒ nil
This is the main function called by initial script just after constructor Processes command line arguments, executes commands, and handles exceptions
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/aspera/cli/runner.rb', line 78 def run # Catch exception information , if any exception_info = nil # False if command shall not be executed (e.g. --show-config) execute_command = true # Catch exceptions begin # Find plugins, shall be after parse! ? Plugins::Factory.instance.add_plugins_from_lookup_folders # Help requested without command ? (plugins must be known here) show_usage if @option_help && @context..command_or_arg_empty? @context.config.periodic_check_newer_gem_version command_sym = if @option_show_config && @context..command_or_arg_empty? COMMAND_CONFIG else @context..get_next_command(Plugins::Factory.instance.plugin_list.unshift(COMMAND_HELP)) end # Command will not be executed, but we need manual @context..fail_on_missing_mandatory = false if @option_help || @option_show_config # Main plugin is not dynamically instantiated case command_sym when COMMAND_HELP show_usage when COMMAND_CONFIG command_plugin = @context.config else # Get plugin, set options, etc command_plugin = (command_sym) # Parse plugin specific options @context.. end # Help requested for current plugin show_usage(all: false) if @option_help if @option_show_config @context.formatter.display_results(Result::SingleObject.new(@context..(only_defined: true).stringify_keys)) execute_command = false end # Locking for single execution (only after "per plugin" option, in case lock port is there) lock_port = @context..get_option(:lock_port) if !lock_port.nil? begin # No need to close later, will be freed on process exit. must save in member else it is garbage collected Log.log.debug{"Opening lock port #{lock_port}"} # Loopback address, could also be 'localhost' @tcp_server = TCPServer.new('127.0.0.1', lock_port) rescue StandardError => e execute_command = false Log.log.warn{"Another instance is already running (#{e.})."} end end pid_file = @context..get_option(:pid_file) if !pid_file.nil? File.write(pid_file, Process.pid) Log.log.debug{"Wrote pid #{Process.pid} to #{pid_file}"} at_exit{File.delete(pid_file)} end # Execute and display (if not exclusive execution) @context.formatter.display_results(command_plugin.execute_action) if execute_command # Save config file if command modified it @context.config.save_config_file_if_needed # Finish @context.transfer.shutdown rescue Net::SSH::AuthenticationFailed => e; exception_info = {e: e, t: 'SSH', security: true} rescue OpenSSL::SSL::SSLError => e; exception_info = {e: e, t: 'SSL'} rescue Cli::BadArgument => e; exception_info = {e: e, t: 'Argument', usage: true} rescue Cli::MissingArgument => e; exception_info = {e: e, t: 'Missing'} rescue Cli::BadIdentifier => e; exception_info = {e: e, t: 'Identifier'} rescue Cli::SchemaRequest => e; exception_info = {e: e, t: 'Schema'} rescue Cli::Error => e; exception_info = {e: e, t: 'Tool', usage: true} rescue Transfer::Error => e; exception_info = {e: e, t: 'Transfer'} rescue RestCallError => e; exception_info = {e: e, t: 'Rest'} rescue SocketError => e; exception_info = {e: e, t: 'Network'} rescue StandardError => e; exception_info = {e: e, t: "Other(#{e.class.name})", debug: true} rescue Interrupt => e; exception_info = {e: e, t: 'Interruption', debug: true} end # Cleanup file list files TempFileManager.instance.cleanup # 1- processing of error condition unless exception_info.nil? Log.log.warn(exception_info[:e].) if Log.instance.logger_type.eql?(:syslog) && exception_info[:security] Log.log.error{"#{exception_info[:t]}: #{exception_info[:e].}"} unless exception_info[:e].is_a?(Cli::SchemaRequest) Log.log.debug{(['Backtrace:'] + exception_info[:e].backtrace).join("\n")} if exception_info[:debug] @context.formatter.(:error, 'Use option -h to get help.') if exception_info[:usage] # Is that a known error condition with proposal for remediation ? Hints.hint_for(exception_info[:e], @context.formatter) # Requested help for a Hash parameter/option ? if exception_info[:e].is_a?(Cli::SchemaRequest) Log.log.info{"#{exception_info[:t]}: #{exception_info[:e].}"} schema_path = exception_info[:e].path if schema_path.nil? Log.log.warn{'Sorry, no schema provided yet. Please refer to the manual or API.'} else builder = Schema::Documentation.new(TerminalFormatter, Schema::Registry.instance.reader(schema_path)).build @context.formatter.display_results(Result::ObjectList.new(builder.rows, fields: builder.columns)) end end end # 2- processing of command not processed (due to exception or bad command line) if execute_command || @option_show_config @context..final_errors.each do |msg| Log.log.error{"Argument: #{msg}"} # Add code as exception if there is not already an error exception_info = {e: Exception.new(msg), t: 'UnusedArg'} if exception_info.nil? end end # 3- in case of error, fail the process status unless exception_info.nil? # Show stack trace in debug mode raise exception_info[:e] if Log.log.debug? # Else give hint and exit @context.formatter.(:error, 'Use --log-level=debug to get more details.') if exception_info[:debug] Process.exit(1) end return end |
#show_usage(all: true, exit: true) ⇒ nil
Display usage information and help
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/aspera/cli/runner.rb', line 200 def show_usage(all: true, exit: true) # Display main plugin options (+config) @context.formatter.(:error, @context..parser) if all @context.only_manual! # List plugins that have a "require" field, i.e. all but main plugin Plugins::Factory.instance.plugin_list.each do |plugin_name_sym| # Config was already included in the global options next if plugin_name_sym.eql?(COMMAND_CONFIG) # Override main option parser with a brand new, to avoid having global options @context. = Manager.new(Info::CMD_NAME) @context..parser. = '' # Remove default banner (plugin_name_sym) # Display generated help for plugin options @context.formatter.(:error, @context..parser.help) end end Process.exit(0) if exit end |