class Birdwatcher::Console
Constants
- DB_MIGRATIONS_PATH
- DEFAULT_AUTO_COMPLETION_STRINGS
- HISTORY_FILE_LOCATION
- HISTORY_FILE_NAME
- LINE_SEPARATOR
Attributes
current_module[RW]
current_workspace[RW]
database[R]
spool[RW]
Public Class Methods
new()
click to toggle source
# File lib/birdwatcher/console.rb, line 14 def initialize @output_mutex = Mutex.new @spool_mutex = Mutex.new end
Public Instance Methods
auto_completion_strings()
click to toggle source
# File lib/birdwatcher/console.rb, line 50 def auto_completion_strings if !@auto_completion_strings @auto_completion_strings = DEFAULT_AUTO_COMPLETION_STRINGS commands.each { |c| @auto_completion_strings += c.auto_completion_strings } @auto_completion_strings += Birdwatcher::Module.module_paths end @auto_completion_strings end
confirm(question)
click to toggle source
# File lib/birdwatcher/console.rb, line 104 def confirm(question) question = "#{question} (y/n) " save_to_spool(question) if HighLine.agree("#{question}") save_to_spool("y\n") true else save_to_spool("n\n") false end end
error(message)
click to toggle source
# File lib/birdwatcher/console.rb, line 92 def error(message) output "[-] ".bold.light_red + message end
fatal(message)
click to toggle source
# File lib/birdwatcher/console.rb, line 100 def fatal(message) output "[-]".white.bold.on_red + " #{message}" end
handle_input(input)
click to toggle source
# File lib/birdwatcher/console.rb, line 35 def handle_input(input) input.strip! save_command_to_history(input) save_to_spool("#{input}\n") command_name, argument_line = input.split(" ", 2).map(&:strip) command_name.downcase commands.each do |command| next unless command.has_name?(command_name) command.new.execute(argument_line) return true end error("Unknown command: #{command_name.bold}") false end
info(message)
click to toggle source
# File lib/birdwatcher/console.rb, line 77 def info(message) output "[+] ".bold.light_blue + message end
klout_client()
click to toggle source
# File lib/birdwatcher/console.rb, line 129 def klout_client if !@klout_clients @klout_clients = create_klout_clients! end @klout_clients.sample end
line_separator()
click to toggle source
# File lib/birdwatcher/console.rb, line 73 def line_separator output LINE_SEPARATOR end
newline()
click to toggle source
# File lib/birdwatcher/console.rb, line 69 def newline output "" end
output(data, newline = true)
click to toggle source
# File lib/birdwatcher/console.rb, line 59 def output(data, newline = true) data = "#{data}\n" if newline with_output_mutex { print data } save_to_spool(data) end
output_formatted(*args)
click to toggle source
# File lib/birdwatcher/console.rb, line 65 def output_formatted(*args) output(sprintf(*args), false) end
page_text(text)
click to toggle source
# File lib/birdwatcher/console.rb, line 116 def page_text(text) save_to_spool(text) ::TTY::Pager::SystemPager.new.page(text) rescue Errno::EPIPE end
start!()
click to toggle source
# File lib/birdwatcher/console.rb, line 19 def start! print_banner bootstrap! Readline.completion_proc = proc do |s| expanded_s = File.expand_path(s) Birdwatcher::Console.instance.auto_completion_strings.grep(/\A#{Regexp.escape(s)}/) + Dir["#{expanded_s}*"].grep(/^#{Regexp.escape(expanded_s)}/) end Readline.completion_append_character = "" load_command_history while input = Readline.readline(prompt_line, true) save_to_spool(prompt_line) input = input.to_s.strip handle_input(input) unless input.empty? end end
task(message, fatal = false) { |block| ... }
click to toggle source
# File lib/birdwatcher/console.rb, line 81 def task(message, fatal = false, &block) output("[+] ".bold.light_blue + message, false) yield block output " done".bold.light_green rescue => e output " failed".bold.light_red error "#{e.class}: ".bold + e.message e.backtrace.each { |l| error l } if debugging_enabled? exit(1) if fatal end
twitter_client()
click to toggle source
# File lib/birdwatcher/console.rb, line 122 def twitter_client if !@twitter_clients @twitter_clients = create_twitter_clients! end @twitter_clients.sample end
warn(message)
click to toggle source
# File lib/birdwatcher/console.rb, line 96 def warn(message) output "[!] ".bold.light_yellow + message end
Private Instance Methods
bootstrap!()
click to toggle source
# File lib/birdwatcher/console.rb, line 146 def bootstrap! bootstrap_configuration! bootstrap_database! newline end
bootstrap_configuration!()
click to toggle source
# File lib/birdwatcher/console.rb, line 152 def bootstrap_configuration! if !Birdwatcher::Configuration.configured? Birdwatcher::ConfigurationWizard.new.start! end task "Loading configuration...", true do Birdwatcher::Configuration.load! end end
bootstrap_database!()
click to toggle source
# File lib/birdwatcher/console.rb, line 161 def bootstrap_database! task "Preparing database...", true do Sequel.extension :migration, :core_extensions @database = Sequel.connect(configuration.get!(:database_connection_uri)) Sequel::Migrator.run(@database, DB_MIGRATIONS_PATH) Sequel::Model.db = @database Sequel::Model.plugin :timestamps bootstrap_models! load_default_workspace! end end
bootstrap_models!()
click to toggle source
# File lib/birdwatcher/console.rb, line 173 def bootstrap_models! Dir[File.join(File.dirname(__FILE__), "..", "..", "models", "*.rb")].each do |file| require file end end
commands()
click to toggle source
# File lib/birdwatcher/console.rb, line 197 def commands @commands ||= Birdwatcher::Command.descendants end
configuration()
click to toggle source
# File lib/birdwatcher/console.rb, line 201 def configuration Birdwatcher::Configuration end
create_klout_clients!()
click to toggle source
# File lib/birdwatcher/console.rb, line 224 def create_klout_clients! clients = [] configuration.get(:klout).each do |key| clients << Birdwatcher::KloutClient.new(key) end clients end
create_twitter_clients!()
click to toggle source
# File lib/birdwatcher/console.rb, line 213 def create_twitter_clients! clients = [] configuration.get!(:twitter).each do |keypair| clients << Twitter::REST::Client.new do |config| config.consumer_key = keypair["consumer_key"] config.consumer_secret = keypair["consumer_secret"] end end clients end
debugging_enabled?()
click to toggle source
# File lib/birdwatcher/console.rb, line 264 def debugging_enabled? ENV.key?("BIRDWATCHER_DEBUG") end
load_command_history()
click to toggle source
# File lib/birdwatcher/console.rb, line 232 def load_command_history if File.exist?(HISTORY_FILE_LOCATION) if File.readable?(HISTORY_FILE_LOCATION) File.open(HISTORY_FILE_LOCATION).each_line do |command| Readline::HISTORY << command.strip end else warn("Cannot load command history: #{HISTORY_FILE_LOCATION} is not readable") end end end
load_default_workspace!()
click to toggle source
# File lib/birdwatcher/console.rb, line 189 def load_default_workspace! @current_workspace = Birdwatcher::Models::Workspace.find_or_create( :name => Birdwatcher::Models::Workspace::DEFAULT_WORKSPACE_NAME ) do |w| w.description = Birdwatcher::Models::Workspace::DEFAULT_WORKSPACE_DESCRIPTION end end
prompt_line()
click to toggle source
# File lib/birdwatcher/console.rb, line 179 def prompt_line prompt = "birdwatcher[".bold + "#{current_workspace.name}" + "]".bold if current_module prompt += "[".bold + current_module.path.light_red + "]> ".bold else prompt += "> ".bold end prompt end
save_command_to_history(command)
click to toggle source
# File lib/birdwatcher/console.rb, line 244 def save_command_to_history(command) if File.exist?(HISTORY_FILE_LOCATION) && !File.writable?(HISTORY_FILE_LOCATION) warn("Cannot save command to history: #{HISTORY_FILE_LOCATION} is not writable") return end File.open(HISTORY_FILE_LOCATION, "a") do |file| file.puts(command) end end
save_to_spool(string)
click to toggle source
# File lib/birdwatcher/console.rb, line 254 def save_to_spool(string) return unless spool_enabled? string = string.to_s.uncolorize with_spool_mutex { self.spool.write(string) } end
spool_enabled?()
click to toggle source
# File lib/birdwatcher/console.rb, line 260 def spool_enabled? self.spool && self.spool.is_a?(File) end
with_output_mutex() { || ... }
click to toggle source
# File lib/birdwatcher/console.rb, line 205 def with_output_mutex @output_mutex.synchronize { yield } end
with_spool_mutex() { || ... }
click to toggle source
# File lib/birdwatcher/console.rb, line 209 def with_spool_mutex @spool_mutex.synchronize { yield } end