Class: Aspera::Cli::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/cli/main.rb

Overview

The main CLI class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#plugin_envObject (readonly)

Returns the value of attribute plugin_env.



19
20
21
# File 'lib/aspera/cli/main.rb', line 19

def plugin_env
  @plugin_env
end

Class Method Details

.result_transfer(statuses) ⇒ Object

Process statuses of finished transfer sessions raise exception if there is one error else returns an empty status



202
203
204
205
206
# File 'lib/aspera/cli/main.rb', line 202

def self.result_transfer(statuses)
  worst=TransferAgent.session_status(statuses)
  raise worst unless worst.eql?(:success)
  return Main.result_nothing
end

Instance Method Details

#optionsObject



208
# File 'lib/aspera/cli/main.rb', line 208

def options;@opt_mgr;end

#process_command_lineObject

this is the main function called by initial script just after constructor



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/aspera/cli/main.rb', line 213

def process_command_line
  Log.log.debug('process_command_line')
  exception_info=nil
  begin
    # find plugins, shall be after parse! ?
    @plugin_env[:config].add_plugins_from_lookup_folders
    # help requested without command ? (plugins must be known here)
    exit_with_usage(true) if @option_help and @opt_mgr.command_or_arg_empty?
    generate_bash_completion if @bash_completion
    # load global default options and process
    @plugin_env[:config].add_plugin_default_preset(Plugins::Config::CONF_GLOBAL_SYM)
    @opt_mgr.parse_options!
    # dual execution locking
    lock_port=@opt_mgr.get_option(:lock_port,:optional)
    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
        @tcp_server=TCPServer.new('127.0.0.1',lock_port.to_i)
      rescue => e
        raise CliError,"Another instance is already running (lock port=#{lock_port})."
      end
    end
    if @option_show_config and @opt_mgr.command_or_arg_empty?
      command_sym=Plugins::Config::CONF_PLUGIN_SYM
    else
      command_sym=@opt_mgr.get_next_command(@plugin_env[:config].plugins.keys.dup.unshift(:help))
    end
    # main plugin is not dynamically instanciated
    case command_sym
    when :help
      exit_with_usage(true)
    when Plugins::Config::CONF_PLUGIN_SYM
      command_plugin=@plugin_env[:config]
    else
      # get plugin, set options, etc
      command_plugin=get_plugin_instance_with_options(command_sym)
      # parse plugin specific options
      @opt_mgr.parse_options!
    end
    # help requested for current plugin
    exit_with_usage(false) if @option_help
    if @option_show_config
      @plugin_env[:formater].display_results({:type=>:single_object,:data=>@opt_mgr.declared_options(false)})
      Process.exit(0)
    end
    # execute and display
    @plugin_env[:formater].display_results(command_plugin.execute_action)
    # finish
    @plugin_env[:transfer].shutdown
  rescue CliBadArgument => e;          exception_info=[e,'Argument',:usage]
  rescue CliNoSuchId => e;             exception_info=[e,'Identifier']
  rescue CliError => e;                exception_info=[e,'Tool',:usage]
  rescue Fasp::Error => e;             exception_info=[e,"FASP(ascp]"]
  rescue Aspera::RestCallError => e; exception_info=[e,"Rest"]
  rescue SocketError => e;             exception_info=[e,"Network"]
  rescue StandardError => e;           exception_info=[e,"Other",:debug]
  rescue Interrupt => e;               exception_info=[e,"Interruption",:debug]
  end
  # cleanup file list files
  TempFileManager.instance.cleanup
  # 1- processing of error condition
  unless exception_info.nil?
    @plugin_env[:formater].display_message(:error,"ERROR:".bg_red.gray.blink+" "+exception_info[1]+": "+exception_info[0].message)
    @plugin_env[:formater].display_message(:error,"Use '-h' option to get help.") if exception_info[2].eql?(:usage)
  end
  # 2- processing of command not processed (due to exception or bad command line)
  @opt_mgr.final_errors.each do |msg|
    @plugin_env[:formater].display_message(:error,"ERROR:".bg_red.gray.blink+" Argument: "+msg)
  end
  # 3- in case of error, fail the process status
  unless exception_info.nil?
    if Log.instance.level.eql?(:debug)
      # will force to show stack trace
      raise exception_info[0]
    else
      @plugin_env[:formater].display_message(:error,"Use '--log-level=debug' to get more details.") if exception_info[2].eql?(:debug)
      Process.exit(1)
    end
  end
  return nil
end

#program_nameObject



210
# File 'lib/aspera/cli/main.rb', line 210

def program_name;PROGRAM_NAME;end