Class: Kontena::Plugin::Shell::Session
- Inherits:
-
Object
- Object
- Kontena::Plugin::Shell::Session
- Defined in:
- lib/kontena/plugin/shell/session.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #caret ⇒ Object
- #config ⇒ Object
- #grid_name ⇒ Object
- #history_file ⇒ Object
-
#initialize(context) ⇒ Session
constructor
A new instance of Session.
- #master_name ⇒ Object
- #pastel ⇒ Object
- #prompt ⇒ Object
- #read_history ⇒ Object
- #run ⇒ Object
- #run_command(buf) ⇒ Object
Constructor Details
#initialize(context) ⇒ Session
Returns a new instance of Session.
13 14 15 16 |
# File 'lib/kontena/plugin/shell/session.rb', line 13 def initialize(context) @context = Context.new(context) read_history end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
11 12 13 |
# File 'lib/kontena/plugin/shell/session.rb', line 11 def context @context end |
Instance Method Details
#caret ⇒ Object
98 99 100 |
# File 'lib/kontena/plugin/shell/session.rb', line 98 def caret pastel.white('>') end |
#config ⇒ Object
90 91 92 |
# File 'lib/kontena/plugin/shell/session.rb', line 90 def config Kontena::Cli::Config end |
#grid_name ⇒ Object
106 107 108 |
# File 'lib/kontena/plugin/shell/session.rb', line 106 def grid_name config.current_grid ? pastel.blue(config.current_grid) : pastel.red('<no grid>') end |
#history_file ⇒ Object
18 19 20 |
# File 'lib/kontena/plugin/shell/session.rb', line 18 def history_file ENV['KOSH_HISTORY'] || File.join(Dir.home, '.kosh_history') end |
#master_name ⇒ Object
102 103 104 |
# File 'lib/kontena/plugin/shell/session.rb', line 102 def master_name config.current_master ? pastel.blue(config.current_master.name) : pastel.red('<no master>') end |
#pastel ⇒ Object
86 87 88 |
# File 'lib/kontena/plugin/shell/session.rb', line 86 def pastel Kontena.pastel end |
#prompt ⇒ Object
94 95 96 |
# File 'lib/kontena/plugin/shell/session.rb', line 94 def prompt "#{master_name}/#{grid_name} #{pastel.yellow(context)} #{caret} " end |
#read_history ⇒ Object
22 23 24 |
# File 'lib/kontena/plugin/shell/session.rb', line 22 def read_history File.readlines(history_file).each { |line| Readline::HISTORY.push(line.chomp) } if File.exist?(history_file) end |
#run ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/kontena/plugin/shell/session.rb', line 38 def run puts File.read(__FILE__)[/__END__$(.*)/m, 1] puts "Kontena Shell v#{Kontena::Plugin::Shell::VERSION} (c) 2017 Kontena" puts pastel.green("Enter 'help' to see a list of commands or 'help <command>' to get help on a specific command.") stty_save = `stty -g`.chomp rescue nil at_exit do File.write(history_file, Readline::HISTORY.to_a.uniq.last(100).join("\n")) system('stty', stty_save) if stty_save end # Hook stack command kontena.yml content prompting require 'kontena/plugin/shell/callbacks/stack_file' Readline.completion_proc = Proc.new do |word| line = Readline.line_buffer tokens = line.shellsplit tokens.pop unless word.empty? if context.empty? && tokens.empty? completions = Kontena::MainCommand.recognised_subcommands.flat_map(&:names) + Shell.commands.keys else command = Shell.command(context.first || tokens.first || 'kontena') if command if command.completions.first.respond_to?(:call) completions = command.completions.first.call(context, tokens, word) else completions = Array(command.completions) end else completions = [] end end word.empty? ? completions : completions.select { |c| c.start_with?(word) } end while buf = Readline.readline(prompt, true) if buf.strip.empty? Readline::HISTORY.pop else run_command(buf) end end puts puts pastel.green("Bye!") end |
#run_command(buf) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/kontena/plugin/shell/session.rb', line 26 def run_command(buf) tokens = buf.split(/\s(?=(?:[^"]|"[^"]*")*$)/).map(&:strip) runner = Shell.command(tokens.first) || Shell.command(context.first) || Kontena::Plugin::Shell::KontenaCommand command = runner.new(context, tokens, self) old_trap = trap('INT', Proc.new { Thread.main[:command_thread] && Thread.main[:command_thread].kill }) Thread.main[:command_thread] = Thread.new do command.run end Thread.main[:command_thread].join trap('INT', old_trap) end |