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_env_args` 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



79
80
81
82
83
84
85
86
87
88
# File 'lib/aspera/command_line_builder.rb', line 79

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 = {
    env:  {},
    args: []
  }
  @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



40
41
42
# File 'lib/aspera/command_line_builder.rb', line 40

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



47
48
49
# File 'lib/aspera/command_line_builder.rb', line 47

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



114
115
116
117
118
# File 'lib/aspera/command_line_builder.rb', line 114

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_env_args(env_args) ⇒ Object

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

Parameters:

  • env_args (Hash)

    with :env and :args



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/aspera/command_line_builder.rb', line 101

def add_env_args(env_args)
  Log.dump(:env_args, @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
  env_args[:env].merge!(@result[:env])
  env_args[:args].concat(@result[:args])
  return
end

#process_paramsObject



120
121
122
123
124
# File 'lib/aspera/command_line_builder.rb', line 120

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

#read_param(name) ⇒ Object



126
127
128
# File 'lib/aspera/command_line_builder.rb', line 126

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

#required(name, required) ⇒ Object

Change required-ness of property in schema



91
92
93
94
95
96
97
# File 'lib/aspera/command_line_builder.rb', line 91

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