mirror of
https://github.com/inspec/inspec
synced 2024-11-26 14:40:26 +00:00
Add deprecation hook for attribute 'default' option
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
parent
fd0ef925c4
commit
7ac2f6433a
2 changed files with 17 additions and 7 deletions
|
@ -2,5 +2,9 @@
|
||||||
"file_version": "1.0.0",
|
"file_version": "1.0.0",
|
||||||
"unknown_group_action": "ignore",
|
"unknown_group_action": "ignore",
|
||||||
"groups": {
|
"groups": {
|
||||||
|
"attrs_value_replaces_default": {
|
||||||
|
"action": "ignore",
|
||||||
|
"prefix": "The 'default' option for attributes is being replaced by 'value' - please use it instead."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
# encoding:utf-8
|
# encoding:utf-8
|
||||||
|
|
||||||
|
require 'utils/deprecation'
|
||||||
|
|
||||||
module Inspec
|
module Inspec
|
||||||
class Attribute
|
class Attribute
|
||||||
attr_accessor :name
|
attr_accessor :name
|
||||||
|
@ -21,8 +23,8 @@ module Inspec
|
||||||
# output warn message if we are in a exec call
|
# output warn message if we are in a exec call
|
||||||
Inspec::Log.warn(
|
Inspec::Log.warn(
|
||||||
"Attribute '#{@name}' does not have a value. "\
|
"Attribute '#{@name}' does not have a value. "\
|
||||||
"Use --attrs to provide a value for '#{@name}' or specify a default "\
|
"Use --attrs to provide a value for '#{@name}' or specify a "\
|
||||||
"value with `attribute('#{@name}', default: 'somedefault', ...)`.",
|
"value with `attribute('#{@name}', value: 'somevalue', ...)`.",
|
||||||
) if Inspec::BaseCLI.inspec_cli_command == :exec
|
) if Inspec::BaseCLI.inspec_cli_command == :exec
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -42,8 +44,12 @@ module Inspec
|
||||||
def initialize(name, options = {})
|
def initialize(name, options = {})
|
||||||
@name = name
|
@name = name
|
||||||
@opts = options
|
@opts = options
|
||||||
validate_value_type(default) if @opts.key?(:type) && @opts.key?(:default)
|
if @opts.key?(:default)
|
||||||
@value = nil
|
Inspec.deprecate(:attrs_value_replaces_default, "attribute name: '#{name}'")
|
||||||
|
@opts[:value] = @opts.delete(:default)
|
||||||
|
end
|
||||||
|
@value = @opts[:value]
|
||||||
|
validate_value_type(@value) if @opts.key?(:type) && @opts.key?(:value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def value=(new_value)
|
def value=(new_value)
|
||||||
|
@ -54,7 +60,7 @@ module Inspec
|
||||||
def value
|
def value
|
||||||
if @value.nil?
|
if @value.nil?
|
||||||
validate_required(@value) if @opts[:required] == true
|
validate_required(@value) if @opts[:required] == true
|
||||||
@value = default
|
@value = value_or_dummy
|
||||||
else
|
else
|
||||||
@value
|
@value
|
||||||
end
|
end
|
||||||
|
@ -162,8 +168,8 @@ module Inspec
|
||||||
end
|
end
|
||||||
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
||||||
|
|
||||||
def default
|
def value_or_dummy
|
||||||
@opts.key?(:default) ? @opts[:default] : DEFAULT_ATTRIBUTE.new(@name)
|
@opts.key?(:value) ? @opts[:value] : DEFAULT_ATTRIBUTE.new(@name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue