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



72
73
74
75
76
77
78
79
80
81
# File 'lib/aspera/command_line_builder.rb', line 72

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(source_path, name) ⇒ Object

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



65
66
67
# File 'lib/aspera/command_line_builder.rb', line 65

def read_schema(source_path, name)
  YAML.load_file(File.join(File.dirname(source_path), "#{name}.schema.yaml"))
end

.supported_by_agent(agent, properties) ⇒ Object

Returns true if given agent supports that field.

Returns:

  • true if given agent supports that field



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

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

.validate_schema(schema, ascp: false) ⇒ Object

Fill default values for some fields in the schema

Parameters:

  • schema (Hash)

    The JSON schema



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/aspera/command_line_builder.rb', line 46

def validate_schema(schema, ascp: false)
  schema['properties'].each do |name, info|
    Aspera.assert_type(info, Hash){"#{info.class} for #{name}"}
    unsupported_keys = info.keys - PROPERTY_KEYS
    Aspera.assert(unsupported_keys.empty?){"Unsupported definition keys: #{unsupported_keys}"}
    # By default : string, unless it's without arg (switch)
    # info['type'] ||= info['x-cli-switch'] ? 'boolean' : 'string'
    Aspera.assert(info.key?('type') || info.key?('enum')){"Missing type for #{name} in #{schema['description']}"}
    Aspera.assert(info['type'].eql?('boolean')){"switch must be bool: #{name}"} if info['x-cli-switch']
    # Add default cli option name if not present, and if supported in "direct".
    info['x-cli-option'] = "--#{name.to_s.tr('_', '-')}" if info['x-cli-option'].eql?(true) || (info['x-cli-switch'].eql?(true) && !info.key?('x-cli-option'))
    Aspera.assert(%w[x-cli-option x-cli-envvar x-cli-special].any?{ |i| info.key?(i)}, type: :warn){name} if ascp && supported_by_agent(CLI_AGENT, info)
    # info['x-cli-option'] = "--#{name.to_s.tr('_', '-')}" if ascp && !info.key?('x-cli-option') && !info['x-cli-envvar'] && (info.key?('x-cli-switch') || supported_by_agent(CLI_AGENT, info))
    info.freeze
    validate_schema(info, ascp: ascp) if info['type'].eql?('object') && info['properties']
  end
end

Instance Method Details

#add_command_line_options(*options) ⇒ Object

Add options directly to command line



107
108
109
110
111
# File 'lib/aspera/command_line_builder.rb', line 107

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



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

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



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

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

#read_param(name) ⇒ Object



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

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

#required(name, required) ⇒ Object

Change required-ness of property in schema



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

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