Class: PuppetRepl::Cli

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/puppet-repl/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support

#compiler, #create_compiler, #create_node, #create_scope, #do_initialize, #environment_loaders, #facterdb_filter, #facts, #function_files, #function_map, #lib_dirs, #load_lib_dirs, #manifests_dir, #mod_finder, #module_dirs, #new_parser, #node, #parser, #puppet_env_name, #puppet_environment, #puppet_evaluate, #puppet_lib_dir, #scope

Class Method Details



55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet-repl/cli.rb', line 55

def self.print_repl_desc
  puts(<<-EOT)
Ruby Version: #{RUBY_VERSION}
Puppet Version: #{Puppet.version}
Puppet Repl Version: #{PuppetRepl::VERSION}
Created by: NWOps <[email protected]>
Type "exit", "functions", "types", "reset", "help" for more information.

  EOT
end

.startObject



66
67
68
69
70
71
72
# File 'lib/puppet-repl/cli.rb', line 66

def self.start
  print_repl_desc
  repl_obj = new
  while buf = Readline.readline(">> ", true)
    repl_obj.handle_input(buf)
  end
end

Instance Method Details

#handle_input(input) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet-repl/cli.rb', line 30

def handle_input(input)
  case input
  when 'help'
    PuppetRepl::Cli.print_repl_desc
  when 'functions'
    puts function_map.keys.sort
  when 'facts'
    puts JSON.pretty_generate(node.facts)
  when '_'
    puts(" => #{@last_item}")
  when 'environment'
    puts "Puppet Environment: #{puppet_env_name}"
  when 'exit'
    exit 0
  when 'reset'
    @scope = nil
  when 'current_resources'
    compiler.known_resource_types
  else
    result = puppet_eval(input)
    @last_item = result
    puts(" => #{normalize_output(result)}")
  end
end

#normalize_output(result) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/puppet-repl/cli.rb', line 21

def normalize_output(result)
  if result.instance_of?(Array)
    if result.count == 1
      return result.first
    end
  end
  result
end

#puppet_eval(input) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/puppet-repl/cli.rb', line 9

def puppet_eval(input)
  begin
    parser.evaluate_string(scope, input)
  rescue ArgumentError => e
    e.message
  rescue Puppet::ParseErrorWithIssue => e
    e.message
  rescue Exception => e
    e.message
  end
end