module Mongrel2::CLI::CommandUtilities
Functions for common command tasks
Public Instance Methods
edit( filename )
click to toggle source
Invoke the user's editor on the given filename
and return the exit code from doing so.
# File lib/mongrel2/cli.rb, line 444 def edit( filename ) editor = ENV['EDITOR'] || ENV['VISUAL'] || DEFAULT_EDITOR system editor, filename.to_s unless $?.success? || editor =~ /vim/i raise "Editor exited with an error status (%d)" % [ $?.exitstatus ] end end
find_mongrel2()
click to toggle source
Search the PATH for a mongrel2 binary, returning the absolute Pathname to it if found, and outputting a warning and describing how to set ENV if not.
# File lib/mongrel2/cli.rb, line 455 def find_mongrel2 if ENV['MONGREL2'] m2 = Pathname( ENV['MONGREL2'] ) error = nil if !m2.file? error = "but it isn't a plain file." elsif !m2.executable? error = "but it isn't executable." end raise "MONGREL2 was set to #{m2}, #{error}" if error return m2 else m2 = ENV['PATH'].split( File::PATH_SEPARATOR ). map {|dir| Pathname(dir) + 'mongrel2' }. find {|path| path.executable? } return m2 if m2 raise "The 'mongrel2' binary doesn't seem to be in your PATH. Either " + "add the appropriate directory to your PATH or set the MONGREL2 " + "environment variable to the full path." end end
find_server( serverspec=nil )
click to toggle source
Search the current mongrel2 config for a server matching serverspec
and return it as a Mongrel2::Config::Server
object.
# File lib/mongrel2/cli.rb, line 384 def find_server( serverspec=nil ) server = nil servers = Mongrel2::Config.servers raise "No servers are configured." if servers.empty? # If there's only one configured server, just make sure if a serverspec was given # that it would have matched. if servers.length == 1 server = servers.first if !serverspec || servers.first.values.values_at( :uuid, :default_host, :name ).include?( serverspec ) # Otherwise, require an argument and search for the desired server if there is one else raise "You must specify a server uuid/hostname/name when more " + "than one server is configured." if servers.length > 1 && !serverspec server = servers.find {|s| s.uuid == serverspec } || servers.find {|s| s.default_host == serverspec } || servers.find {|s| s.name == serverspec } end raise "No servers match '#{serverspec}'" unless server return server end
read_history()
click to toggle source
Read command line history from HISTORY_FILE
# File lib/mongrel2/cli.rb, line 413 def read_history histfile = HISTORY_FILE.expand_path if histfile.exist? lines = histfile.readlines.collect {|line| line.chomp } self.log.debug "Read %d saved history commands from %s." % [ lines.length, histfile ] Readline::HISTORY.push( *lines ) else self.log.debug "History file '%s' was empty or non-existant." % [ histfile ] end end
save_history()
click to toggle source
Save command line history to HISTORY_FILE
# File lib/mongrel2/cli.rb, line 427 def save_history histfile = HISTORY_FILE.expand_path lines = Readline::HISTORY.to_a.reverse.uniq.reverse lines = lines[ -DEFAULT_HISTORY_SIZE, DEFAULT_HISTORY_SIZE ] if lines.length > DEFAULT_HISTORY_SIZE self.log.debug "Saving %d history lines to %s." % [ lines.length, histfile ] histfile.open( File::WRONLY|File::CREAT|File::TRUNC ) do |ofh| ofh.puts( *lines ) end end