Class: Aspera::CommandLineBuilder

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

Overview

Helper class to build command line from a parameter list (key-value hash) Constructor takes hash: { 'param1':'value1', ...} process_param is called repeatedly with all known parameters add_to_exec_spec is called to get resulting param list and env var (also checks that all params were used)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, schema, convert) ⇒ CommandLineBuilder

Returns a new instance of CommandLineBuilder.

Parameters:

  • object (Hash) —

    object with parameters

  • schema (Schema::Reader) —

    JSON schema

  • convert (Object) —

    function to convert value



85
86
87
88
89
90
91
# File 'lib/aspera/command_line_builder.rb', line 85

def initialize(object, schema, convert)
  @object = object # keep reference so that it can be modified by caller before calling `process_params`
  @schema = schema
  @convert = convert
  @result = ExecSpec.new
  @processed_parameters = []
end

Class Method Details

.read_schema(name_sym, ascp: false) ⇒ Aspera::Schema::Reader

Called by provider of definition before constructor of this class so that schema has all mandatory fields

Parameters:

  • name_sym (Symbol) —

    name of the schema to read

  • ascp (Boolean) (defaults to: false) —

    true if ascp mode

Returns:



44
45
46
# File 'lib/aspera/command_line_builder.rb', line 44

def read_schema(name_sym, ascp: false)
  validate_schema(Schema::Registry.instance.reader(name_sym), ascp: ascp)
end

.supported_by_agent(agent, properties) ⇒ Boolean

Returns true if given agent supports that field.

Parameters:

  • agent (Symbol) —

    Transfer agent name

  • properties (Hash) —

    Transfer spec parameter information

Returns:

  • (Boolean) —

    true if given agent supports that field



51
52
53
# File 'lib/aspera/command_line_builder.rb', line 51

def supported_by_agent(agent, properties)
  !properties.key?('x-agents') || properties['x-agents'].include?(agent.to_s)
end

Instance Method Details

#add_command_line_options(*options) ⇒ Object

Add options directly to command line



117
118
119
120
121
# File 'lib/aspera/command_line_builder.rb', line 117

def add_command_line_options(*options)
  options = options.first if options.first.is_a?(Array) && options.length.eql?(1)
  Aspera.assert_type(options, Array)
  options.each { |o| @result.args.push(o.to_s) }
end

#add_to_exec_spec(exec_spec) ⇒ Object

Add processed parameters to env and args, warns about unused parameters

Parameters:



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/aspera/command_line_builder.rb', line 104

def add_to_exec_spec(exec_spec)
  Log.dump(:exec_spec, @result)
  # warn about non translated arguments
  @object.each_pair do |name, value|
    Log.log.warn { "Unknown transfer spec parameter: #{name} = \"#{value}\"" } unless @processed_parameters.include?(name)
  end
  # set result
  exec_spec.env.merge!(@result.env)
  exec_spec.args.concat(@result.args)
  return
end

#process_params ⇒ Object



123
124
125
126
127
# File 'lib/aspera/command_line_builder.rb', line 123

def process_params
  @schema['properties'].each_key do |k|
    process_param(k)
  end
end

#read_param(name) ⇒ Object



129
130
131
# File 'lib/aspera/command_line_builder.rb', line 129

def read_param(name)
  return process_param(name, read: true)
end

#required(name, required) ⇒ Object

Change required-ness of property in schema



94
95
96
97
98
99
100
# File 'lib/aspera/command_line_builder.rb', line 94

def required(name, required)
  if required
    @schema['required'].push(name) unless @schema['required']&.include?(name)
  else
    @schema['required'].delete(name)
  end
end