module ChefVault::Mixin::Helper

Public Instance Methods

merge_values(json, file) click to toggle source
# File lib/chef/knife/mixin/helper.rb, line 27
def merge_values(json, file)
  values = {}
  values.merge!(values_from_file(file)) if file
  values.merge!(values_from_json(json)) if json

  values
end
printable?(string) click to toggle source

I/P: String O/P: true/false returns true if string is free of non-printable characters (escape sequences) this returns false for whitespace escape sequences as well, e.g. nt

# File lib/chef/knife/mixin/helper.rb, line 71
def printable?(string)
  /[^[:print:]]|[[:space:]]/.match(string)
end
set_mode(mode) click to toggle source
# File lib/chef/knife/mixin/helper.rb, line 19
def set_mode(mode)
  if mode == "client"
    Chef::Config[:solo_legacy_mode] = false
  else
    Chef::Config[:solo_legacy_mode] = true
  end
end
validate_json(json) click to toggle source

I/P: json string Raises ‘InvalidValue` if any of the json’s values contain non-printable characters.

# File lib/chef/knife/mixin/helper.rb, line 50
def validate_json(json)
  begin
    evaled_json = eval(json) # rubocop: disable Security/Eval
  rescue SyntaxError
    raise ChefVault::Exceptions::InvalidValue, "#{json} is not valid JSON!"
  end

  if evaled_json.is_a?(Hash)
    evaled_json.each do |key, value|
      next unless printable?(value.to_s)

      msg = "Value '#{value}' of key '#{key}' contains non-printable characters. Check that backslashes are escaped with another backslash (e.g. C:\\\\Windows) in double-quoted strings."
      ChefVault::Log.warn(msg)
    end
  end
end
values_from_file(file) click to toggle source
# File lib/chef/knife/mixin/helper.rb, line 35
def values_from_file(file)
  json = File.open(file, &:read)

  values_from_json(json)
end
values_from_json(json) click to toggle source
# File lib/chef/knife/mixin/helper.rb, line 41
def values_from_json(json)
  validate_json(json)
  JSON.parse(json)
rescue JSON::ParserError
  raise JSON::ParserError, "#{json} is not valid JSON!"
end