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)

Constant Summary collapse

BOOLEAN_CLASSES =
[TrueClass,FalseClass]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param_hash, params_definition) ⇒ CommandLineBuilder



24
25
26
27
28
29
30
# File 'lib/aspera/command_line_builder.rb', line 24

def initialize(param_hash,params_definition)
  @param_hash=param_hash  # keep reference so that it can be modified by caller before calling `process_params`
  @params_definition=params_definition
  @result_env={}
  @result_args=[]
  @used_param_names=[]
end

Class Method Details

.yes_to_true(value) ⇒ Object

transform yes/no to trye/false



48
49
50
51
52
53
54
# File 'lib/aspera/command_line_builder.rb', line 48

def self.yes_to_true(value)
  case value
  when 'yes'; return true
  when 'no'; return false
  end
  raise "unsupported value: #{value}"
end

Instance Method Details

#add_command_line_options(options) ⇒ Object

add options directly to ascp command line



57
58
59
60
# File 'lib/aspera/command_line_builder.rb', line 57

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

adds keys :env :args with resulting values after processing warns if some parameters were not used



39
40
41
42
43
44
45
# File 'lib/aspera/command_line_builder.rb', line 39

def add_env_args(env,args)
  Log.log.debug("ENV=#{@result_env}, ARGS=#{@result_args}")
  warn_unrecognized_params
  env.merge!(@result_env)
  args.push(*@result_args)
  return nil
end

#process_param(param_name, action = nil) ⇒ Object

Process a parameter from transfer specification and generate command line param or env var

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/aspera/command_line_builder.rb', line 72

def process_param(param_name,action=nil)
  options=@params_definition[param_name]
  action=options[:type] if action.nil?
  # should not happen
  raise "Internal error: ask processing of param #{param_name}" if options.nil?
  # by default : not mandatory
  options[:mandatory]||=false
  if options.has_key?(:accepted_types)
    # single type is placed in array
    options[:accepted_types]=[options[:accepted_types]] unless options[:accepted_types].is_a?(Array)
  else
    # by default : string, unless it's without arg
    options[:accepted_types]=action.eql?(:opt_without_arg) ? BOOLEAN_CLASSES : [String]
  end
  # check mandatory parameter (nil is valid value)
  raise Fasp::Error.new("mandatory parameter: #{param_name}") if options[:mandatory] and !@param_hash.has_key?(param_name)
  parameter_value=@param_hash[param_name]
  parameter_value=options[:default] if parameter_value.nil? and options.has_key?(:default)
  # check provided type
  raise Fasp::Error.new("#{param_name} is : #{parameter_value.class} (#{parameter_value}), shall be #{options[:accepted_types]}, ") unless parameter_value.nil? or options[:accepted_types].inject(false){|m,v|m or parameter_value.is_a?(v)}
  @used_param_names.push(param_name) unless action.eql?(:defer)

  # process only non-nil values
  return nil if parameter_value.nil?

  if options.has_key?(:translate_values)
    # translate using conversion table
    new_value=options[:translate_values][parameter_value]
    raise "unsupported value: #{parameter_value}" if new_value.nil?
    parameter_value=new_value
  end
  raise "unsupported value: #{parameter_value}" unless options[:accepted_values].nil? or options[:accepted_values].include?(parameter_value)
  if options[:encode]
    newvalue=options[:encode].call(parameter_value)
    raise Fasp::Error.new("unsupported #{param_name}: #{parameter_value}") if newvalue.nil?
    parameter_value=newvalue
  end

  case action
  when :ignore,:defer # ignore this parameter or process later
    return
  when :get_value # just get value
    return parameter_value
  when :envvar # set in env var
    # define ascp parameter in env var from transfer spec
    @result_env[env_name(param_name,options)] = parameter_value
  when :opt_without_arg # if present and true : just add option without value
    add_param=false
    case parameter_value
    when false# nothing to put on command line, no creation by default
    when true; add_param=true
    else raise Fasp::Error.new("unsupported #{param_name}: #{parameter_value}")
    end
    add_param=!add_param if options[:add_on_false]
    add_command_line_options([switch_name(param_name,options)]) if add_param
  when :opt_with_arg # transform into command line option with value
    #parameter_value=parameter_value.to_s if parameter_value.is_a?(Integer)
    parameter_value=[parameter_value] unless parameter_value.is_a?(Array)
    # if transfer_spec value is an array, applies option many times
    parameter_value.each{|v|add_command_line_options([switch_name(param_name,options),v])}
  else
    raise "Error"
  end
end

#process_paramsObject



62
63
64
65
66
# File 'lib/aspera/command_line_builder.rb', line 62

def process_params
  @params_definition.keys.each do |k|
    process_param(k)
  end
end

#warn_unrecognized_paramsObject



32
33
34
35
# File 'lib/aspera/command_line_builder.rb', line 32

def warn_unrecognized_params
  # warn about non translated arguments
  @param_hash.each_pair{|key,val|Log.log.warn("unrecognized parameter: #{key} = \"#{val}\"") if !@used_param_names.include?(key)}
end