Class: Aspera::Transfer::CommandLineBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/transfer/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



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

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

.doc_lines(node, formatter) ⇒ Array<String>

Documentation of the command line option of a property: annotate of Schema::Documentation

Parameters:

Returns:

  • (Array<String>) —

    Lines added to the description, empty if no option



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aspera/transfer/command_line_builder.rb', line 67

def doc_lines(node, formatter)
  envvar_prefix = ''
  cli_option =
    if node.key?('x-cli-envvar')
      envvar_prefix = 'env:'
      node['x-cli-envvar']
    elsif node['x-cli-switch']
      false_part = node.key?('x-cli-false') ? " / #{node['x-cli-false']}" : ''
      "#{node['x-cli-option']}#{false_part}"
    elsif node['x-cli-option']
      arg_type = node.key?('enum') ? '{enum}' : "{#{[node['type']].flatten.join('|')}}"
      conversion_tag = node.key?('x-cli-convert') ? 'conversion' : nil
      sep = node['x-cli-option'].start_with?('--') ? '=' : ' '
      "#{node['x-cli-option']}#{sep}#{"(#{conversion_tag})" if conversion_tag}#{arg_type}"
    end
  return [] unless cli_option
  short = node.key?('x-cli-short') ? "(#{node['x-cli-short']})" : nil
  ["(#{'special:' if node['x-cli-special']}#{envvar_prefix}#{formatter.markdown_text("`#{cli_option}`")})#{short}"]
end

.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:



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

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



59
60
61
# File 'lib/aspera/transfer/command_line_builder.rb', line 59

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



150
151
152
153
154
# File 'lib/aspera/transfer/command_line_builder.rb', line 150

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:



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/aspera/transfer/command_line_builder.rb', line 137

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



156
157
158
159
160
# File 'lib/aspera/transfer/command_line_builder.rb', line 156

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

#read_param(name) ⇒ Object



162
163
164
# File 'lib/aspera/transfer/command_line_builder.rb', line 162

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

#required(name, required) ⇒ Object

Change required-ness of property in schema



127
128
129
130
131
132
133
# File 'lib/aspera/transfer/command_line_builder.rb', line 127

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