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)

    with parameters

  • schema (Hash)

    JSON schema



75
76
77
78
79
80
81
82
83
84
# File 'lib/aspera/command_line_builder.rb', line 75

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(folder, name, ascp: false) ⇒ Object

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



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

def read_schema(folder, name, ascp: false)
  schema = YAML.load_file(File.join(folder, "#{name}.schema.yaml"))
  validate_schema(schema, 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



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

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



110
111
112
113
114
# File 'lib/aspera/command_line_builder.rb', line 110

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



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aspera/command_line_builder.rb', line 97

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



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

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

#read_param(name) ⇒ Object



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

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

#required(name, required) ⇒ Object

Change required-ness of property in schema



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

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