class DTK::Client::CLI::Context

Object that provides the context for interpreting commands

Constants

ERROR_MSG_MAPPING
FILE_TYPES

Attributes

base_dsl_file_obj[R]
context_attributes[R]

Public Class Methods

determine_context() click to toggle source
# File lib/cli/context.rb, line 25
def self.determine_context
  base_dsl_file_obj = base_dsl_file_obj()
  Type.create_context!(base_dsl_file_obj)
end
new(base_dsl_file_obj) click to toggle source
# File lib/cli/context.rb, line 30
def initialize(base_dsl_file_obj)
  @base_dsl_file_obj   = base_dsl_file_obj
  @command_processor   = Processor.default
  @context_attributes  = Attributes.new(self)
  @options             = {}
  add_command_defs_defaults_and_hooks!
end

Private Class Methods

base_dsl_file_obj(opts = {}) click to toggle source

opts can have keys

:dir_path
# File lib/cli/context.rb, line 161
def  self.base_dsl_file_obj(opts = {}) 
  opts[:flag] = false if opts[:flag].nil?
  DirectoryParser.matching_file_obj?(FILE_TYPES, opts)
end

Public Instance Methods

method_missing(method, *args, &body) click to toggle source
Calls superclass method
# File lib/cli/context.rb, line 44
def method_missing(method, *args, &body)
  command_processor_object_methods.include?(method) ? @command_processor.send(method, *args, &body) : super
end
module_ref_object_from_options_or_context(options, module_refs_opts = {}) click to toggle source
# File lib/cli/context.rb, line 61
def module_ref_object_from_options_or_context(options, module_refs_opts = {})
  module_ref_object_from_options_or_context?(options, module_refs_opts) || raise_error_when_missing_context(:module_ref, options)
end
respond_to?(method) click to toggle source
Calls superclass method
# File lib/cli/context.rb, line 48
def respond_to?(method)
  command_processor_object_methods.include?(method) or super
end
run_and_return_response_object(argv) click to toggle source
# File lib/cli/context.rb, line 39
def run_and_return_response_object(argv)
  @flag = true if argv.include?('-d')
  @command_processor.run_and_return_response_object(argv)
end
value_from_base_dsl_file?(key) click to toggle source
# File lib/cli/context.rb, line 52
def value_from_base_dsl_file?(key)
  case key
  when :module_ref 
    module_ref_from_base_dsl_file?
  when :service_instance 
    service_instance_from_base_dsl_file?
  end
end

Private Instance Methods

add_command_defs!() click to toggle source

Methods related to adding cli command definitions

# File lib/cli/context.rb, line 228
def add_command_defs!
  raise Error::NoMethodForConcreteClass.new(self.class)
end
add_command_defs_defaults_and_hooks!() click to toggle source
# File lib/cli/context.rb, line 232
def add_command_defs_defaults_and_hooks!
  add_command_defaults!
  add_command_defs!
  add_command_hooks!
  self
end
command_processor_object_methods() click to toggle source
# File lib/cli/context.rb, line 239
def command_processor_object_methods
  @@command_processor_object_methods ||= Processor::Methods.all
end
module_ref_from_base_dsl_file?() click to toggle source
# File lib/cli/context.rb, line 71
def module_ref_from_base_dsl_file?
  parsed_module_hash = parse_module_content_and_create_hash
  namespace          = parsed_module_hash[:namespace]
  module_name        = parsed_module_hash[:module_name]
  version            = parsed_module_hash[:version] || 'master'

  if namespace and module_name
    client_dir_path = base_dsl_file_obj.parent_dir?
    ModuleRef.new(:namespace => namespace, :module_name => module_name, :version => version, :client_dir_path => client_dir_path)
  end
end
module_ref_object_from_options_or_context?(options, module_refs_opts = {}) click to toggle source
# File lib/cli/context.rb, line 172
def module_ref_object_from_options_or_context?(options, module_refs_opts = {})
  # using :ignore_parsing_errors to ret namespace, name and version from .yaml file even if there are parsing errors
  @options.merge!(:ignore_parsing_errors => module_refs_opts[:ignore_parsing_errors])

  if options[:module_ref]
    opts = {:namespace_module_name => options[:module_ref]}
    opts.merge!(:version => options[:version]) if options[:version]
    ModuleRef.new(opts)
  else
    if module_dir_path = options[:directory_path]
      set_base_dsl_file_obj!(:dir_path => module_dir_path)
    end
    context_attributes[:module_ref]
  end
end
parse_conent_and_ret_service_name() click to toggle source
# File lib/cli/context.rb, line 132
def parse_conent_and_ret_service_name
  begin
    base_dsl_file_obj.parse_content(:service_module_summary).val(:Name)
  rescue Error::Usage => error
    if content = @options[:ignore_parsing_errors] && base_dsl_file_obj.content
      ret_service_name_from_raw_content(content)
    else
      raise error
    end
  end
end
parse_module_content_and_create_hash() click to toggle source
# File lib/cli/context.rb, line 83
def parse_module_content_and_create_hash
  parsed_hash = {}

  begin
    parsed_module = base_dsl_file_obj.parse_content(:common_module_summary)
    parsed_hash = {
      :namespace   => parsed_module.val(:Namespace),
      :module_name => parsed_module.val(:ModuleName),
      :version     => parsed_module.val(:ModuleVersion)
    }
  rescue Error::Usage => error
    # if there is syntax error in dsl, we still want to get namespace, name, and version
    # will be used in commands like 'dtk module uninstall', ... where we want to uninstall module even if parsing errors in yaml
    if content = @options[:ignore_parsing_errors] && base_dsl_file_obj.content
      ret_module_info_from_raw_content(parsed_hash, content)
    else
      raise error
    end
  end

  parsed_hash
end
raise_error_when_missing_context(type, options = {}) click to toggle source
# File lib/cli/context.rb, line 211
def raise_error_when_missing_context(type, options = {})
  if options["d"].nil?
    @base_dsl_file_obj.raise_error_if_no_content
  else
    @base_dsl_file_obj.raise_error_if_no_content_flag(type)
  end
  # TODO: not sure if below can be reached
  error_msg = 
    if options[:directory_path]
      "Bad #{ERROR_MSG_MAPPING[type]} directory path '#{options[:directory_path]}'"
    else
      "This command must be executed from within a #{ERROR_MSG_MAPPING[type]} directory or a directory path must be given using option '#{option_ref(:directory_path)}'"
    end
  raise Error::Usage, error_msg
end
ret_module_info_from_raw_content(parsed_hash, content) click to toggle source

get namespace, name and version from raw file content and return as parsed_hash

# File lib/cli/context.rb, line 107
def ret_module_info_from_raw_content(parsed_hash, content)
  info_found = lambda {|input_hash| (input_hash[:namespace] && input_hash[:module_name] && input_hash[:version]) }

  content.each_line do |line|
    if line_match = line.match(/(^module:)(.*)/)
      name_found      = true
      full_name       = line_match[2].strip
      namespace, name = full_name.split('/')
      parsed_hash.merge!(:namespace => namespace, :module_name => name)
    elsif line_match = line.match(/(^version:)(.*)/)
      version_found = true
      parsed_hash.merge!(:version => line_match[2].strip)
    end

    break if info_found.call(parsed_hash)
  end
end
ret_service_name_from_raw_content(content) click to toggle source
# File lib/cli/context.rb, line 144
def ret_service_name_from_raw_content(content)
  content.each_line do |line|
    if line_match = line.match(/(^name:)(.*)/)
      return line_match[2].strip
    end
  end
end
service_instance_from_base_dsl_file?() click to toggle source
# File lib/cli/context.rb, line 125
def service_instance_from_base_dsl_file?
  #raise_error_when_missing_context(:service_instance) unless base_dsl_file_obj.file_type == DTK::DSL::FileType::ServiceInstance::DSLFile::Top
  # base_dsl_file_obj.file_type == DTK::DSL::FileType::ServiceInstance::DSLFile::Top
  base_dsl_file_obj.file_type == DTK::DSL::FileType::ServiceInstance::DSLFile::Top::Hidden
  parse_conent_and_ret_service_name
end
service_instance_in_options_or_context(options, service_refs_opts = {}) click to toggle source
# File lib/cli/context.rb, line 188
def service_instance_in_options_or_context(options, service_refs_opts = {})
  service_instance_in_options_or_context?(options, service_refs_opts) || raise_error_when_missing_context(:service_instance, options)
end
service_instance_in_options_or_context?(options, service_refs_opts = {}) click to toggle source
# File lib/cli/context.rb, line 192
def service_instance_in_options_or_context?(options, service_refs_opts = {})
  # using :ignore_parsing_errors to ret namespace, name and version from .yaml file even if there are parsing errors
  @options.merge!(:ignore_parsing_errors => service_refs_opts[:ignore_parsing_errors])

  if ret = options[:service_instance]
    ret
  else
    if module_dir_path = options[:directory_path]
      set_base_dsl_file_obj!(:dir_path => module_dir_path)
    end
    context_attributes[:service_instance]
  end
end
set_base_dsl_file_obj!(opts = {}) click to toggle source

opts can have keys

:dir_path
# File lib/cli/context.rb, line 154
def set_base_dsl_file_obj!(opts = {})
  opts[:flag] = @flag
  @base_dsl_file_obj = self.class.base_dsl_file_obj(opts)
end