Class: Aspera::Cli::Option::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/cli/option/value.rb

Overview

Declared option: type, value, and where the value is stored.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option:, description: nil, allowed: Type::STRING, on_set: nil, shorthand: nil, convert: nil, deprecation: nil, schema: nil, complete: nil, security: nil) ⇒ Value

allowed:

  • nil No validation, so just a string
  • Class The single allowed Class
  • Array<Class> Multiple allowed classes
  • Array<Symbol> List of allowed values

Parameters:

  • option (Symbol) —

    Name of option

  • description (String, nil) (defaults to: nil) —

    Description for help; if nil, derived from schema

  • allowed (nil, Class, Array<Class>, Array<Symbol>) (defaults to: Type::STRING) —

    Allowed values

  • on_set (#call, nil) (defaults to: nil) —

    Called with the new value each time the value is set

  • shorthand (String, nil) (defaults to: nil) —

    For a Hash option: a String value is stored as {shorthand => value}

  • convert (#call, nil) (defaults to: nil) —

    For a Hash option: called with a new Hash value, returns the value to store, before merge (e.g. conversion of a deprecated form)

  • deprecation (Hash, Deprecation, nil) (defaults to: nil) —

    Deprecation: {last:, message:}, see Deprecation

  • schema (String) (defaults to: nil) —

    Declaration of schema

  • complete (Symbol, nil) (defaults to: nil) —

    Values proposed by shell completion, one of Complete::KINDS

  • security (Symbol, nil) (defaults to: nil) —

    Most restrictive security level at which the option is available (e.g. :low: executes code), see Security



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/aspera/cli/option/value.rb', line 65

def initialize(option:, description: nil, allowed: Type::STRING, on_set: nil, shorthand: nil, convert: nil, deprecation: nil, schema: nil, complete: nil, security: nil)
  Log.log.trace1 { "option: #{option}, allowed: #{allowed}" }
  @option = option
  @description = description
  @group = nil
  @short = nil
  @block = nil
  # by default passwords and secrets are sensitive, else specify when declaring the option
  @sensitive = Secret::Hider.instance.secret?(@option, '')
  @deprecation = Deprecation.create(deprecation)
  @schema = schema
  @shorthand = shorthand
  Aspera.assert(convert.nil? || convert.respond_to?(:call)) { "#{@option}: convert must respond to call" }
  @convert = convert
  Aspera.assert_values(complete, Complete::KINDS + [nil]) { "#{@option}: complete" }
  @complete = complete
  Aspera.assert_values(security, Security::LEVELS + [nil]) { "#{@option}: security" }
  @security = security
  @source = nil
  @value = nil
  Aspera.assert(on_set.nil? || on_set.respond_to?(:call)) { "#{@option}: on_set callback must respond to call" }
  @on_set = on_set
  @types = nil
  @values = nil
  @kind = :other
  allowed = infer_allowed_from_schema(schema, allowed) if schema
  apply_allowed(allowed) unless allowed.nil?
end

Instance Attribute Details

#block ⇒ Object

[Proc, nil] Block to call for flag options



48
49
50
# File 'lib/aspera/cli/option/value.rb', line 48

def block
  @block
end

#complete ⇒ Object (readonly)

[Symbol, nil] Values proposed by shell completion, one of Complete::KINDS



40
41
42
# File 'lib/aspera/cli/option/value.rb', line 40

def complete
  @complete
end

#deprecation ⇒ Object (readonly)

Returns the value of attribute deprecation.



34
35
36
# File 'lib/aspera/cli/option/value.rb', line 34

def deprecation
  @deprecation
end

#group ⇒ Object

[String] Help section group name (set by Option::Registry)



44
45
46
# File 'lib/aspera/cli/option/value.rb', line 44

def group
  @group
end

#kind ⇒ Object (readonly)

[Symbol] How values are converted: :flag, :boolean, :integer, :float, :enum, :enum_list, :string_list, :other



31
32
33
# File 'lib/aspera/cli/option/value.rb', line 31

def kind
  @kind
end

#option ⇒ Object (readonly)

[Symbol] Name of option



27
28
29
# File 'lib/aspera/cli/option/value.rb', line 27

def option
  @option
end

#schema ⇒ Object (readonly)

Returns the value of attribute schema.



34
35
36
# File 'lib/aspera/cli/option/value.rb', line 34

def schema
  @schema
end

#security ⇒ Object (readonly)

[Symbol, nil] Most restrictive security level at which the option is available, nil: all levels, see Security



36
37
38
# File 'lib/aspera/cli/option/value.rb', line 36

def security
  @security
end

#sensitive ⇒ Object (readonly)

Returns the value of attribute sensitive.



34
35
36
# File 'lib/aspera/cli/option/value.rb', line 34

def sensitive
  @sensitive
end

#short ⇒ Object

[String, nil] Short option char (set by Option::Registry)



46
47
48
# File 'lib/aspera/cli/option/value.rb', line 46

def short
  @short
end

#shorthand ⇒ Object (readonly)

[String, nil] For a Hash option: key of a String value



38
39
40
# File 'lib/aspera/cli/option/value.rb', line 38

def shorthand
  @shorthand
end

#source ⇒ Object (readonly)

[Symbol, nil] Option::Source of current value, nil if never set



33
34
35
# File 'lib/aspera/cli/option/value.rb', line 33

def source
  @source
end

#types ⇒ Object (readonly)

[Array, nil] List of allowed types, nil for no validation



29
30
31
# File 'lib/aspera/cli/option/value.rb', line 29

def types
  @types
end

#values ⇒ Object

[Array] List of allowed values (Symbols and specific values)



42
43
44
# File 'lib/aspera/cli/option/value.rb', line 42

def values
  @values
end

Instance Method Details

#allowed_info ⇒ String?

Returns allowed values, or expected types when not plain String, e.g. Hash|String.

Returns:

  • (String, nil) —

    allowed values, or expected types when not plain String, e.g. Hash|String



104
105
106
107
108
109
# File 'lib/aspera/cli/option/value.rb', line 104

def allowed_info
  return @values.join('|') if @values&.any?
  types = @types&.reject { |t| t.eql?(NilClass) }
  return if types.nil? || types.empty? || types.eql?(Type::STRING)
  types.map(&:name).join('|')
end

#assign_value(value, source:, warn_deprecation: true, merge: true, final: false, configured: false) ⇒ nil

Assign value to option. Value can be a String, then evaluated with ExtendedValue, or directly a value. Hash and Array values are merged with current value.

Parameters:

  • value (String, Object) —

    Value to assign to option

  • source (Symbol) —

    Option::Source of value

  • warn_deprecation (Boolean) (defaults to: true) —

    Emit deprecation warning (false for internal transfers)

  • merge (Boolean) (defaults to: true) —

    Merge Hash and Array with current value (false: value is already complete)

  • final (Boolean) (defaults to: false) —

    The value is final: values from a lower source (preset, environment) are ignored, not merged nor checked

  • configured (Boolean) (defaults to: false) —

    Value of a preset read from the configuration (by its name): set by the operator, not restricted by the security level (a client cannot change the configuration at levels medium and high), see Parser#add_option_preset

Returns:

  • (nil)

Raises:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/aspera/cli/option/value.rb', line 144

def assign_value(value, source:, warn_deprecation: true, merge: true, final: false, configured: false)
  # Value from a source with lower priority than current value: only fills containers
  lower = !@source.nil? && Option::Source.priority(source) < Option::Source.priority(@source)
  # Final value (set by the program for the whole run): a lower source is ignored, even for a container
  if lower && @final
    Log.log.debug { "#{source}: #{@option}: ignored, final value set from #{@source}" }
    return
  end
  if lower && ![Hash, Array].include?(@types&.first)
    Log.log.debug { "#{source}: #{@option}: ignored, already set from #{@source}" }
    return
  end
  Aspera.assert(!@deprecation, type: :warn) { "Option #{@option} is #{@deprecation}" } if warn_deprecation
  # Values set by the program, the environment or the configuration are not restricted: those from command line, a preset given by the client, interactive input are
  restricted = restricted_source?(source) && !configured
  Security.instance.check_level(@security, "option #{@option}") if restricted
  if schema_request?(value)
    return if lower
    raise SchemaRequest.new(:option, @option, @schema) unless @schema.nil?
    # Schema depends on command: kept as-is, raised by Parser#get_option(schema:)
    store(value, source)
    return
  end
  new_value = coerce(ExtendedValue.instance.evaluate(value, context: "option: #{@option}", allowed: @types))
  check_sub_options_security(new_value) if restricted
  # Set by the one who starts the process: a secret (e.g. in a `Hash`) of the environment can be sent to a URL of the environment, see `Security#check_credentials_in`
  Security.instance.add_configured(new_value) if source.eql?(:env)
  new_value = {@shorthand => new_value} if @shorthand && new_value.is_a?(String)
  declare_secrets(new_value)
  Log.log.trace1 { "#{source}: #{@option} <- (#{new_value.class})#{Secret::Hider.instance.hidden_copy(new_value)}" }
  Aspera.assert_type(new_value, *@types, type: BadArgument) { "Option #{@option}" } if @types
  new_value = @convert.call(new_value) if @convert && new_value.is_a?(Hash)
  if merge && (new_value.is_a?(Hash) || new_value.is_a?(Array))
    current_value = @value
    # An `Array` default value is replaced, not appended to
    mergeable = current_value.is_a?(new_value.is_a?(Hash) ? Hash : Array) && !current_value.empty? &&
      !(new_value.is_a?(Array) && [source, @source].include?(:default))
    if lower
      # Current value has priority: merge new value under it, unless explicitly emptied
      return unless mergeable
      new_value, current_value = current_value, new_value
      source = @source
    end
    new_value = new_value.is_a?(Hash) ? current_value.deep_merge(new_value) : current_value + new_value if mergeable
  end
  # A value set by the program is not checked, e.g. `--out.img` sets option `image` with a value checked as part of option `out`
  validate_schema(new_value, partial: true) unless source.eql?(:code)
  store(new_value, source)
  @final = true if final
  nil
end

#check_security(value) ⇒ Object

Check that the option, and the sub-options present in a Hash value (x-security in schema), are available at current security level. Used for a value that is not assigned yet, e.g. to store in a preset.

Parameters:

  • value (Object) —

    Value of the option

Raises:



207
208
209
210
# File 'lib/aspera/cli/option/value.rb', line 207

def check_security(value)
  Security.instance.check_level(@security, "option #{@option}")
  check_sub_options_security(value)
end

#clear ⇒ nil

Reset stored value to nil

Returns:

  • (nil)


120
121
122
# File 'lib/aspera/cli/option/value.rb', line 120

def clear
  store(nil, nil)
end

#description ⇒ String

Returns description of the option: explicit one, or first line of schema description.

Returns:

  • (String) —

    description of the option: explicit one, or first line of schema description



95
96
97
98
99
100
101
# File 'lib/aspera/cli/option/value.rb', line 95

def description
  return @description unless @description.nil?
  return if @schema.nil?
  schema_node = Schema::Registry.instance.reader(@schema).current
  first_line = (schema_node['title'] || schema_node['description'].to_s).lines.first.to_s.strip
  first_line.end_with?('.') ? first_line[0..-2] : first_line
end

#flag? ⇒ Boolean

Returns true if option takes no value.

Returns:

  • (Boolean) —

    true if option takes no value



112
# File 'lib/aspera/cli/option/value.rb', line 112

def flag? = @kind.eql?(:flag)

#schema_request?(value) ⇒ Boolean

Returns true if value asks for the schema of option.

Parameters:

  • value (Object) —

    value given to option

Returns:

  • (Boolean) —

    true if value asks for the schema of option



116
# File 'lib/aspera/cli/option/value.rb', line 116

def schema_request?(value) = value.eql?(SchemaRequest::KEYWORD) && @types&.include?(Hash)

#validate_complete(value) ⇒ Object

Validate the value of the option once all sources are applied: required of the schema is enforced.

Parameters:

  • value (Object) —

    Value of the option

Raises:



199
200
201
# File 'lib/aspera/cli/option/value.rb', line 199

def validate_complete(value)
  validate_schema(value, partial: false)
end

#value(log: true) ⇒ Object

Get current option value

Parameters:

  • log (Boolean) (defaults to: true) —

    whether to log the value retrieval

Returns:

  • (Object) —

    current value



127
128
129
130
# File 'lib/aspera/cli/option/value.rb', line 127

def value(log: true)
  Log.log.trace1 { "#{@option} -> (#{@value.class})#{Secret::Hider.instance.hidden_copy(@value)}" } if log
  @value
end