Class: PuppetDebugger::InputResponders::Functions

Inherits:
PuppetDebugger::InputResponderPlugin show all
Defined in:
lib/plugins/puppet-debugger/input_responders/functions.rb

Constant Summary collapse

COMMAND_WORDS =
%w(functions)
SUMMARY =
"List all the functions available in the environment."
COMMAND_GROUP =
:environment
FUNC_NATIVE_NAME_REGEX =
%r{\Afunction\s([\w\:]+)}
FUNC_V4_NAME_REGEX =
%r{Puppet\:\:Functions.create_function\s?\(?\:?\'?([\w\:]+)}

Instance Attribute Summary

Attributes inherited from PuppetDebugger::InputResponderPlugin

#debugger

Instance Method Summary collapse

Methods inherited from PuppetDebugger::InputResponderPlugin

command_completion, command_group, command_words, details, execute, #modules_paths, #puppet_debugger_lib_dir, summary

Instance Method Details

#current_module_dirString

Returns - the current module directory or directory that contains a gemfile.

Returns:

  • (String)
    • the current module directory or directory that contains a gemfile



45
46
47
48
49
50
51
# File 'lib/plugins/puppet-debugger/input_responders/functions.rb', line 45

def current_module_dir
  @current_module_dir ||= begin
    File.dirname(::Bundler.default_gemfile)
  rescue ::Bundler::GemfileNotFound
    Dir.pwd
  end
end

#func_listObject

append a () to functions so we know they are functions



28
29
30
31
# File 'lib/plugins/puppet-debugger/input_responders/functions.rb', line 28

def func_list
  # ideally we should get a list of function names via the puppet loader
  function_map.map { |name, | "#{[:full_name]}()" }
end

#function_mapHash

Returns - a map of all the functions.

Returns:

  • (Hash)
    • a map of all the functions



34
35
36
37
38
39
40
41
42
# File 'lib/plugins/puppet-debugger/input_responders/functions.rb', line 34

def function_map
  functions = {}
  function_files.each do |file|
    obj = function_obj(file)
    # return the last matched in cases where rbenv might be involved
    functions[obj[:full_name]] = obj
  end
  functions
end

#function_obj(file) ⇒ Array

Returns - returns a array of the parentname and function name.

Returns:

  • (Array)
    • returns a array of the parentname and function name



61
62
63
64
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/plugins/puppet-debugger/input_responders/functions.rb', line 61

def function_obj(file)
  namespace = nil
  name = nil
  if file =~ /\.pp/
    File.readlines(file, :encoding => "UTF-8").find do |line|
      # TODO: not getting namespace for functio
      if line.match(FUNC_NATIVE_NAME_REGEX)
        namespace, name = $1.split("::", 2)
        name = namespace if name.nil?
        namespace = "" if namespace == name
      end
    end
  elsif file.include?("lib/puppet/functions")
    File.readlines(file, :encoding => "UTF-8").find do |line|
      if line.match(FUNC_V4_NAME_REGEX)
        namespace, name = $1.split("::", 2)
        name = namespace if name.nil?
        namespace = "" if namespace == name
      end
    end
  end
  name ||= File.basename(file, File.extname(file))
  match = file.match('\/(?<mod>[\w\-\.]+)\/(lib|functions|manifests)')
  summary_match = File.read(file, :encoding => "UTF-8").match(/@summary\s(.*)/)
  summary = summary_match[1] if summary_match
  # fetch the puppet version if this is a function from puppet gem
  captures = file.match(/(puppet-[\d\.]+)/)
  file_namespace = captures[1] if captures
  mod_name = file_namespace || match[:mod]
  full_name = namespace.nil? || namespace.empty? ? name : name.prepend("#{namespace}::")
  { namespace: namespace, summary: summary, mod_name: mod_name, name: name, full_name: full_name, file: file }
end

#lib_dirs(module_dirs = modules_paths) ⇒ Object



53
54
55
56
57
58
# File 'lib/plugins/puppet-debugger/input_responders/functions.rb', line 53

def lib_dirs(module_dirs = modules_paths)
  dirs = module_dirs.map do |mod_dir|
    Dir["#{mod_dir}/*/lib"].entries
  end.flatten
  dirs + [puppet_debugger_lib_dir, File.join(current_module_dir, 'lib')]
end

#run(args = []) ⇒ Object



15
16
17
18
# File 'lib/plugins/puppet-debugger/input_responders/functions.rb', line 15

def run(args = [])
  filter = args.first || ""
  TablePrint::Printer.table_print(sorted_list(filter), [:full_name, :mod_name])
end

#sorted_list(filter = "") ⇒ Object



20
21
22
23
24
25
# File 'lib/plugins/puppet-debugger/input_responders/functions.rb', line 20

def sorted_list(filter = "")
  search = /#{Regexp.escape(filter)}/
  function_map.values.find_all do |v|
    "#{v[:mod_name]}_#{v[:full_name]}" =~ search
  end.sort { |a, b| a[:full_name] <=> b[:full_name] }
end