Class: Shell
- Inherits:
-
Object
- Object
- Shell
- Defined in:
- lib/shell.rb
Overview
Shell
Constant Summary collapse
- BANNER =
<<~ENDBANNER ___________________________ OverTheWire wargame Natas ___________________________ ENDBANNER
- CONFIG_FILE =
"#{Dir.home}/.natas.yml"
- PROMPT =
'Natas> '
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#console ⇒ Object
readonly
Returns the value of attribute console.
-
#natas ⇒ Object
readonly
Returns the value of attribute natas.
Instance Method Summary collapse
-
#initialize ⇒ Shell
constructor
A new instance of Shell.
- #run ⇒ Object
Constructor Details
#initialize ⇒ Shell
Returns a new instance of Shell.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/shell.rb', line 20 def initialize @console = Console.new @natas = Natas.new(self) @commands = [] ObjectSpace.each_object(Class).select { |c| c < CmdBase }.each { |c| @commands << c.new(self) } @commands.sort! { |a, b| a.name <=> b.name } return unless File.exist?(CONFIG_FILE) config = YAML.safe_load(File.read(CONFIG_FILE)) @natas.levels.each do |level| level.password = config.fetch(level.level, nil) end end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
18 19 20 |
# File 'lib/shell.rb', line 18 def commands @commands end |
#console ⇒ Object (readonly)
Returns the value of attribute console.
18 19 20 |
# File 'lib/shell.rb', line 18 def console @console end |
#natas ⇒ Object (readonly)
Returns the value of attribute natas.
18 19 20 |
# File 'lib/shell.rb', line 18 def natas @natas end |
Instance Method Details
#run ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/shell.rb', line 35 def run Signal.trap('INT') { exit } puts @console.cyan.bold(BANNER) cmd_help = @commands.detect { |c| c.instance_of?(CmdHelp) } puts @console.yellow("Type #{cmd_help.name} or #{cmd_help.aliases.first} for list of commands") loop do line = Readline.readline(@console.green.bold(PROMPT), true) exit if line.nil? line.strip! next if line.empty? Readline::HISTORY.pop if Readline::HISTORY.length > 1 && Readline::HISTORY[-2] == line words = line.split(/\s+/) cmd = words.first.downcase command = @commands.detect { |c| c.name == cmd || c.aliases.include?(cmd) } if command.nil? puts @console.red('Unrecognized command ') + @console.magenta.bold(cmd) next end command.exec(words[1..-1]) end end |