Class: PuppetRepl::Cli

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support

#compiler, #do_initialize, #known_resource_types, #lib_dirs, #load_lib_dirs, #manifests_dir, #mod_finder, #module_dirs, #node, #parser, #puppet_lib_dir, #scope

Methods included from Support::Node

#create_node

Methods included from Support::Functions

#function_files, #function_map

Methods included from Support::Scope

#create_scope, #scope_vars

Methods included from Support::Facts

#facterdb_filter, #facts

Methods included from Support::Environment

#environment_loaders, #puppet_env_name, #puppet_environment

Methods included from Support::Compilier

#create_compiler

Constructor Details

#initializeCli

Returns a new instance of Cli.



12
13
14
# File 'lib/puppet-repl/cli.rb', line 12

def initialize
  @log_level = 'notice'
end

Instance Attribute Details

#log_levelObject

Returns the value of attribute log_level.



10
11
12
# File 'lib/puppet-repl/cli.rb', line 10

def log_level
  @log_level
end

#settingsObject

Returns the value of attribute settings.



10
11
12
# File 'lib/puppet-repl/cli.rb', line 10

def settings
  @settings
end

Class Method Details



131
132
133
134
135
136
137
138
139
140
# File 'lib/puppet-repl/cli.rb', line 131

def self.print_repl_desc
  puts("Ruby Version: \#{RUBY_VERSION}\nPuppet Version: \#{Puppet.version}\nPuppet Repl Version: \#{PuppetRepl::VERSION}\nCreated by: NWOps <[email protected]>\nType \"exit\", \"functions\", \"vars\", \"krt\", \"facts\", \"reset\", \"help\" for more information.\n\n  EOT\nend\n")

.startObject



142
143
144
145
146
147
148
# File 'lib/puppet-repl/cli.rb', line 142

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

Instance Method Details

#ap_formatterObject



16
17
18
19
20
21
22
# File 'lib/puppet-repl/cli.rb', line 16

def ap_formatter
  unless @ap_formatter
    inspector = AwesomePrint::Inspector.new({:sort_keys => true, :indent => 2})
    @ap_formatter = AwesomePrint::Formatter.new(inspector)
  end
  @ap_formatter
end

#expand_resource_type(types) ⇒ Object

ruturns a formatted array



34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet-repl/cli.rb', line 34

def expand_resource_type(types)
  output = [types].flatten.map do |t|
    if t.class.to_s == 'Puppet::Pops::Types::PResourceType'
      to_resource_declaration(t)
    else
      t
    end
  end
  output
end

#handle_input(input) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/puppet-repl/cli.rb', line 74

def handle_input(input)
  case input
  when 'help'
    PuppetRepl::Cli.print_repl_desc
  when 'functions'
    puts function_map.keys.sort
  when /^:set/
    handle_set(input)
  when 'facts'
    vars = Hash[ node.facts.map { |k, v| [k.to_s, v] } ]
    ap(vars, {:sort_keys => true, :indent => -1})
  when '_'
    puts(" => #{@last_item}")
  when 'vars'
    vars = scope.to_hash.delete_if {| key, value | node.facts.key?(key.to_sym) }
    vars['facts'] = 'removed by the puppet-repl'
    ap 'Facts were removed for easier viewing'
    ap(vars, {:sort_keys => true, :indent => -1})
  when 'environment'
    puts "Puppet Environment: #{puppet_env_name}"
  when 'vars'
    ap(scope.to_hash, {:sort_keys => true, :indent => 0})
  when 'exit'
    exit 0
  when 'reset'
    @scope = nil
    # initilize scope again
    scope
    set_log_level(log_level)
  when 'krt'
    ap(known_resource_types, {:sort_keys => true, :indent => -1})
  else
    begin
      result = puppet_eval(input)
      @last_item = result
      print " => "
      output = normalize_output(result)
      if output.nil?
        puts ""
      else
        ap(output)
      end
    rescue ArgumentError => e
      print " => "
      puts e.message.fatal
    rescue Puppet::ResourceError => e
      print " => "
      puts e.message.fatal
    rescue Puppet::ParseErrorWithIssue => e
      print " => "
      puts e.message.fatal
    rescue Exception => e
      puts e.message.fatal
    end
  end
end

#handle_set(input) ⇒ Object



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

def handle_set(input)
  args = input.split(' ')
  args.shift # throw away the set
  case args.shift
  when /loglevel/
    if level = args.shift
      @log_level = level
      set_log_level(level)
      puts "loglevel #{Puppet::Util::Log.level} is set"
    end
  end
end

#normalize_output(result) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet-repl/cli.rb', line 45

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

#puppet_eval(input) ⇒ Object



24
25
26
# File 'lib/puppet-repl/cli.rb', line 24

def puppet_eval(input)
  parser.evaluate_string(scope, input)
end

#set_log_level(level) ⇒ Object



56
57
58
59
# File 'lib/puppet-repl/cli.rb', line 56

def set_log_level(level)
  Puppet::Util::Log.level = level.to_sym
  Puppet::Util::Log.newdestination(:console)
end

#to_resource_declaration(type) ⇒ Object



28
29
30
31
# File 'lib/puppet-repl/cli.rb', line 28

def to_resource_declaration(type)
  res = scope.catalog.resource(type.type_name, type.title)
  res.to_ral
end