class OctocatalogDiff::CatalogUtil::ENC

Support a generic ENC. It must use one of the supported backends found in the 'enc' subdirectory.

Attributes

builder[R]

Public Class Methods

new(options = {}) click to toggle source

Constructor @param :backend [Symbol] If set, this will force a backend @param :enc [String] Path to ENC script (node_terminus = exec) @param # FIXME: Add support for PE's ENC endpoint API

# File lib/octocatalog-diff/catalog-util/enc.rb, line 21
def initialize(options = {})
  @options = options

  # Determine appropriate backend based on options supplied
  @enc_obj = backend

  # Initialize instance variables for content and error message.
  @builder = @enc_obj.class.to_s

  # Set the executed flag to false, so that it can be executed when something is retrieved.
  @executed = false
end

Public Instance Methods

content(logger = nil) click to toggle source

Retrieve content @return [String] ENC content, or nil if there was an error

# File lib/octocatalog-diff/catalog-util/enc.rb, line 36
def content(logger = nil)
  execute(logger)
  @content ||= @enc_obj.content
end
error_message(logger = nil) click to toggle source

Retrieve error message @return [String] Error message, or nil if there was no error

# File lib/octocatalog-diff/catalog-util/enc.rb, line 43
def error_message(logger = nil)
  execute(logger)
  @error_message ||= @enc_obj.error_message
end
execute(logger = nil) click to toggle source

Execute the 'execute' method of the object, but only once @param [Logger] Logger (optional) - if not supplied any logger messages will be discarded

# File lib/octocatalog-diff/catalog-util/enc.rb, line 50
def execute(logger = nil)
  return if @executed
  logger ||= @options[:logger]
  logger ||= Logger.new(StringIO.new)
  @enc_obj.execute(logger) if @enc_obj.respond_to?(:execute)
  @executed = true
  override_enc_parameters(logger)
end

Private Instance Methods

backend() click to toggle source

Backend - given options, choose an appropriate backend and construct the corresponding object. @return [?] Backend object

# File lib/octocatalog-diff/catalog-util/enc.rb, line 99
def backend
  # Hard-coded backend
  if @options[:backend]
    return OctocatalogDiff::CatalogUtil::ENC::Noop.new(@options) if @options[:backend] == :noop
    return OctocatalogDiff::CatalogUtil::ENC::PE.new(@options) if @options[:backend] == :pe
    return OctocatalogDiff::CatalogUtil::ENC::Script.new(@options) if @options[:backend] == :script
    raise ArgumentError, "Unknown backend :#{@options[:backend]}"
  end

  # Determine backend based on arguments
  return OctocatalogDiff::CatalogUtil::ENC::PE.new(@options) if @options[:pe_enc_url]
  return OctocatalogDiff::CatalogUtil::ENC::Script.new(@options) if @options[:enc]

  # At this point we do not know what backend to use for the ENC
  raise ArgumentError, 'Unable to determine ENC backend to use'
end
merge_enc_param(pointer, key, value) click to toggle source

Merging behavior for ENC overrides @param pointer [Hash] Portion of the content structure to modify @param key [String] String representing structure, delimited by '::' @param value [?] Value to insert at structure point

# File lib/octocatalog-diff/catalog-util/enc.rb, line 81
def merge_enc_param(pointer, key, value)
  if key =~ /::/
    first_key, the_rest = key.split(/::/, 2)
    if pointer[first_key].nil?
      pointer[first_key] = {}
    elsif !pointer[first_key].is_a?(Hash)
      raise ArgumentError, "Attempt to override #{pointer[first_key].class} with hash for #{key}"
    end
    merge_enc_param(pointer[first_key], the_rest, value)
  elsif value.nil?
    pointer.delete(key)
  else
    pointer[key] = value
  end
end
override_enc_parameters(logger) click to toggle source

Override of ENC parameters with parameters specified on the command line. Modifies structures in @enc_obj. @param logger [Logger] Logger object

# File lib/octocatalog-diff/catalog-util/enc.rb, line 64
def override_enc_parameters(logger)
  return unless @options[:enc_override].is_a?(Array) && @options[:enc_override].any?
  content_structure = YAML.load(content)
  @options[:enc_override].each do |x|
    keys = x.key.is_a?(Regexp) ? content_structure.keys.select { |y| x.key.match(y) } : [x.key]
    keys.each do |key|
      merge_enc_param(content_structure, key, x.value)
      logger.debug "ENC override: #{key} #{x.value.nil? ? 'DELETED' : '= ' + x.value.inspect}"
    end
  end
  @content = content_structure.to_yaml
end