From 3e9f3eff6f66d82dab07f31a85c52660340dfca8 Mon Sep 17 00:00:00 2001 From: wh0 Date: Fri, 20 Sep 2013 18:26:13 -0700 Subject: [PATCH] Create shell.rb --- lib/heroku/command/shell.rb | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/heroku/command/shell.rb diff --git a/lib/heroku/command/shell.rb b/lib/heroku/command/shell.rb new file mode 100644 index 000000000..d1887635a --- /dev/null +++ b/lib/heroku/command/shell.rb @@ -0,0 +1,53 @@ +require "heroku/command/base" + +# interactive shell for sequential commands +# +class Heroku::Command::Shell < Heroku::Command::Base + + # shell + # + # Read multiple commands + # + def index + validate_arguments! + + begin + app_or_nil = app + rescue Heroku::Command::CommandFailed + app_or_nil = nil + end + @cmd_tag = app_or_nil ? " (#{app_or_nil})" : "" + + require "readline" + require "shellwords" + + sorted_commands = (Heroku::Command.commands.keys + Heroku::Command.command_aliases.keys).sort + Readline.completion_append_character = " " + Readline.completion_proc = proc { |s| sorted_commands.grep(/^#{Regexp.escape(s)}/) } + + while line = readline + args = line.shellsplit + next if args.empty? + command = args.shift.strip + return if command == "exit" + args.unshift("--app", app_or_nil) if app_or_nil + begin + Heroku::Command.run(command, args) + rescue SystemExit + end + end + puts + end + + private + + def readline + begin + return Readline.readline("heroku#{@cmd_tag}> ", true) + rescue Interrupt + puts + retry + end + end + +end