Class: Kontena::Plugin::Shell::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/plugin/shell/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#contextObject (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

#caretObject



98
99
100
# File 'lib/kontena/plugin/shell/session.rb', line 98

def caret
  pastel.white('>')
end

#configObject



90
91
92
# File 'lib/kontena/plugin/shell/session.rb', line 90

def config
  Kontena::Cli::Config
end

#grid_nameObject



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_fileObject



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_nameObject



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

#pastelObject



86
87
88
# File 'lib/kontena/plugin/shell/session.rb', line 86

def pastel
  Kontena.pastel
end

#promptObject



94
95
96
# File 'lib/kontena/plugin/shell/session.rb', line 94

def prompt
  "#{master_name}/#{grid_name} #{pastel.yellow(context)} #{caret} "
end

#read_historyObject



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

#runObject



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