class PuppetDebugger::InputResponders::Playbooks

Constants

COMMAND_GROUP
COMMAND_WORDS
SUMMARY

Public Class Methods

command_completion(buffer_words) click to toggle source
# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 9
def self.command_completion(buffer_words)
  directories.map do |d|
    path = File.join(d, *buffer_words)
    glob_path = File.directory?(path) ? File.join(path, '*') : path + '*'
    files = Dir.glob(glob_path)
    dirs = files.grep(path).map { |f| File.basename(f, File.extname(f)) }
    files.find_all {|d| d.match(path) }.map { |f| File.basename(f, File.extname(f)) } - dirs
  end.flatten.sort
end
directories() click to toggle source
# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 19
def self.directories
  (internal_directories + external_directory).uniq
end
external_directory() click to toggle source

@return [Array] dir of external playbooks dir empty array is returned if no external directory

# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 30
def self.external_directory
  [ENV['PLAYBOOKS_DIR']].compact
end
gemspecs() click to toggle source

Returns an Array of Gem::Specification objects.

# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 45
def self.gemspecs
  @gemspecs ||= Gem::Specification.respond_to?(:latest_specs) ?
      Gem::Specification.latest_specs : Gem.searcher.init_gemspecs
end
internal_directories() click to toggle source

@return [String] - the path to the playbooks directory

# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 24
def self.internal_directories
  playbooks_dir_list
end
playbooks_dir_list() click to toggle source

@return [Array] - a list of puppet debugger playbook directories

# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 35
def self.playbooks_dir_list
  @playbooks_dir_list ||= begin
    gemspecs.collect do |spec|
      lib_path = File.join(spec.full_gem_path,'lib','puppet-debugger','playbooks')
      lib_path if Dir.exist?(lib_path)
    end.compact
  end
end

Public Instance Methods

create_tree(dirs = []) click to toggle source

@param dirs [Array]

# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 77
def create_tree(dirs = [])
  tree = []
  dirs.each do |dir|
    walk(dir) do |path, depth|
      tree << sprintf("%s%s\n", ('  ' * depth), File.basename(path, File.extname(path)) )
    end
  end
  tree.join()
end
playbook_file(words) click to toggle source
# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 50
def playbook_file(words)
  path = File.join(*words)
  file = nil
  self.class.directories.find do |dir|
    search_path = File.join(dir, path) + '*'
    file = Dir.glob(search_path).find {|f| File.file?(f)}
  end
  file
end
run(args = []) click to toggle source
# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 60
def run(args = [])
  if args.count > 0
    file = playbook_file(args)
    return debugger.handle_input("play #{file}") if file && File.exist?(file)
    self.class.directories.each do |dir|
      walk(dir) do |path|
        if args.first == File.basename(path)
          return create_tree([path])
        end
      end
    end
  else
    create_tree(self.class.directories)
  end
end

Private Instance Methods

walk(dir, depth = 0, &block) click to toggle source

@param dir [String] - directory path @param depth [Integer] - where you are in the directory tree @param block [block] - passes path and depth to block @return [nil]

# File lib/plugins/puppet-debugger/input_responders/playbooks.rb, line 93
def walk(dir, depth = 0, &block)
  Dir.foreach(dir).sort.each do |name|
    next if name == '.' || name == '..'
    path = File.join(dir, name)
    block.call(path, depth)
    walk(path, depth + 1, &block) if File.directory?(path)
  end
end