Class: Saltr::Repl
- Inherits:
-
Object
- Object
- Saltr::Repl
- Defined in:
- lib/saltr.rb
Instance Attribute Summary collapse
-
#last_result ⇒ Object
Returns the value of attribute last_result.
-
#main_minion ⇒ Object
Returns the value of attribute main_minion.
-
#minions ⇒ Object
Returns the value of attribute minions.
-
#out ⇒ Object
Returns the value of attribute out.
-
#static ⇒ Object
Returns the value of attribute static.
Instance Method Summary collapse
- #help(cmd) ⇒ Object
- #history_file ⇒ Object
-
#initialize ⇒ Repl
constructor
A new instance of Repl.
- #initialize_readline ⇒ Object
- #load_history ⇒ Object
- #minion_str ⇒ Object
- #parse(cmd) ⇒ Object
- #parse_cmd_run(cmd) ⇒ Object
- #parse_list(cmd) ⇒ Object
- #parse_salt(cmd, minions = minion_str, out_type = out) ⇒ Object
- #print_result(result) ⇒ Object
- #readline_proc ⇒ Object
- #readline_with_hist_management(prompt = '> ') ⇒ Object
- #repl ⇒ Object
- #run(cmd, debug = false) ⇒ Object
- #run_yaml(cmd, debug = false) ⇒ Object
- #set_minion(cmd) ⇒ Object
- #set_out(cmd) ⇒ Object
- #store_history ⇒ Object
Constructor Details
#initialize ⇒ Repl
Returns a new instance of Repl.
15 16 17 18 19 20 21 22 |
# File 'lib/saltr.rb', line 15 def initialize @minions = '*' @last_result = nil @out = :json @static = true @main_minion = 'gru' initialize_readline end |
Instance Attribute Details
#last_result ⇒ Object
Returns the value of attribute last_result.
13 14 15 |
# File 'lib/saltr.rb', line 13 def last_result @last_result end |
#main_minion ⇒ Object
Returns the value of attribute main_minion.
13 14 15 |
# File 'lib/saltr.rb', line 13 def main_minion @main_minion end |
#minions ⇒ Object
Returns the value of attribute minions.
13 14 15 |
# File 'lib/saltr.rb', line 13 def minions @minions end |
#out ⇒ Object
Returns the value of attribute out.
13 14 15 |
# File 'lib/saltr.rb', line 13 def out @out end |
#static ⇒ Object
Returns the value of attribute static.
13 14 15 |
# File 'lib/saltr.rb', line 13 def static @static end |
Instance Method Details
#help(cmd) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/saltr.rb', line 144 def help(cmd) cmd = 'top' if cmd=='' or cmd.nil? msg = "Help topic '#{cmd}' doesn't exist." begin msg = Help.new.send(cmd.to_sym) rescue end "HELP #{cmd}\n\n#{msg}" end |
#history_file ⇒ Object
196 197 198 |
# File 'lib/saltr.rb', line 196 def history_file Pathname(__FILE__).dirname + '.history' end |
#initialize_readline ⇒ Object
164 165 166 167 168 |
# File 'lib/saltr.rb', line 164 def initialize_readline load_history Readline.completion_append_character = ' ' Readline.completion_proc = readline_proc end |
#load_history ⇒ Object
180 181 182 183 184 185 |
# File 'lib/saltr.rb', line 180 def load_history history_file.open('r').readlines.each do |line| Readline::HISTORY.push line.chomp end rescue end |
#minion_str ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/saltr.rb', line 131 def minion_str if minions.match(/ and /) "-C '#{minions}'" else "'#{minions}'" end end |
#parse(cmd) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/saltr.rb', line 61 def parse(cmd) tokens = cmd.split(/[= ]/, 2) case tokens[0] when 'help' [:result, help(tokens[1])] when 'r' parse_cmd_run(tokens[1]) when 'm' [:result, set_minion(tokens[1])] when 'o' [:result, set_out(tokens[1])] when 'l' parse_list(tokens[1]) when /^salt-key/,/^salt-cloud/ cmd when 'quit' exit(0) else parse_salt(cmd) end end |
#parse_cmd_run(cmd) ⇒ Object
92 93 94 |
# File 'lib/saltr.rb', line 92 def parse_cmd_run(cmd) "salt #{minion_str} cmd.run_all --static '#{cmd}'" end |
#parse_list(cmd) ⇒ Object
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 |
# File 'lib/saltr.rb', line 96 def parse_list(cmd) puts cmd.inspect type = 'sys.list_modules' rest = '' out_type = 'raw' unless cmd.nil? args = cmd.split(/\s+/, 2) case args.first when 'functions' type = 'sys.list_functions' rest = args[1] when 'doc' type = 'sys.list_functions' rest = args[1] out_type = 'txt' else if args[0].match(/\./) type = 'sys.doc' out_type = 'txt' else type = 'sys.list_functions' end rest = args[0] end end cmd = type cmd = "#{cmd} #{rest}" parse_salt(cmd, main_minion, out_type) end |
#parse_salt(cmd, minions = minion_str, out_type = out) ⇒ Object
88 89 90 |
# File 'lib/saltr.rb', line 88 def parse_salt(cmd, minions=minion_str, out_type=out) "salt #{minions} --out=#{out_type} #{cmd}" end |
#print_result(result) ⇒ Object
83 84 85 86 |
# File 'lib/saltr.rb', line 83 def print_result(result) puts result unless result.nil? #YAML.load(result) end |
#readline_proc ⇒ Object
170 171 172 173 174 175 176 177 178 |
# File 'lib/saltr.rb', line 170 def readline_proc cmd_list = [ 'cmd.run', 'cmd.run_all', #'sys.list_modules', 'sys.list_functions', 'sys.doc', 'l', 'l modules', 'l functions', 'l doc', 'rbenv', 'rbenv.install' ] proc { |s| cmd_list.grep(/^#{Regexp.escape(s)}/) } end |
#readline_with_hist_management(prompt = '> ') ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/saltr.rb', line 154 def readline_with_hist_management(prompt='> ') line = Readline.readline(prompt, true) return nil if line.nil? if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line Readline::HISTORY.pop end # TODO: limit the history size line end |
#repl ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/saltr.rb', line 24 def repl stty_save = `stty -g`.chomp while (1) do print "\n#{'-'*50}\n" prompt = "salt '#{@minions}' --out=#{out}: " # cmd = Readline.readline(prompt, true) cmd = readline_with_hist_management(prompt) break if cmd.nil? result = run(cmd) print_result(result) end rescue Interrupt $stderr.puts "\n\nCtrl-c pressed." ensure store_history system('stty', stty_save) end |
#run(cmd, debug = false) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/saltr.rb', line 47 def run(cmd, debug=false) parsed_cmd = parse(cmd) puts parsed_cmd result = if parsed_cmd.class==Array and parsed_cmd.first==:result parsed_cmd[1] else parsed_cmd = "#{parsed_cmd} --out=#{@out}" unless parsed_cmd.match(/--out/) # parsed_cmd += " --static" if static `#{parsed_cmd}` end puts "result:#{result}:" if debug result end |
#run_yaml(cmd, debug = false) ⇒ Object
43 44 45 |
# File 'lib/saltr.rb', line 43 def run_yaml(cmd, debug=false) YAML.load(run(cmd, debug)) end |
#set_minion(cmd) ⇒ Object
126 127 128 129 |
# File 'lib/saltr.rb', line 126 def set_minion(cmd) @minions = cmd cmd end |
#set_out(cmd) ⇒ Object
139 140 141 142 |
# File 'lib/saltr.rb', line 139 def set_out(cmd) @out = cmd cmd end |
#store_history ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/saltr.rb', line 187 def store_history history_file.open('w') do |out| Readline::HISTORY.each do |line| out.puts line end end rescue end |