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, suffix = nil) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aspera/command_line_builder.rb', line 52

def read_schema(source_path, suffix=nil)
  suffix = "_#{suffix}" unless suffix.nil?
  schema = YAML.load_file("#{source_path[0..-4]}#{suffix}.schema.yaml")
  schema['properties'].each do |name, properties|
    Aspera.assert_type(properties, Hash){name}
    unsupported_keys = properties.keys - SCHEMA_KEYS
    Aspera.assert(unsupported_keys.empty?){"Unsupported definition keys: #{unsupported_keys}"}
    # by default : string, unless it's without arg
    properties['type'] ||= properties['x-cli-switch'] ? 'boolean' : 'string'
    # add default cli option name if not present, and if supported in "direct".
    properties['x-cli-option'] = '--' + name.to_s.tr('_', '-') if !properties.key?('x-cli-option') && !properties['x-cli-envvar'] && (properties.key?('x-cli-switch') || supported_by_agent(CLI_AGENT, properties))
    properties.freeze
  end
  schema['required'] = [] unless schema.key?('required')
  schema.freeze
end

.supported_by_agent(agent, properties) ⇒ Object

Returns true if given agent supports that field.

Returns:

  • 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)
end

Instance Method Details

#add_command_line_options(options) ⇒ Object

add options directly to command line



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

def add_command_line_options(options)
  return if options.nil?
  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.log.debug{"add_env_args: ENV=#{@result[:env]}, ARGS=#{@result[:args]}"}
  # warn about non translated arguments
  @object.each_pair do |name, value|
    Log.log.warn{raise "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 nil
end

#process_paramsObject



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

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

#read_param(name) ⇒ Object



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

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