Merge branch 'no-exit' [closes #92]

This commit is contained in:
Samuel Cochran 2013-11-18 10:59:55 +11:00
commit f93df88021
4 changed files with 40 additions and 11 deletions

View File

@ -40,6 +40,10 @@ OptionParser.new do |parser|
parser.on('-q', 'Ignored option -q') do |ignored|
end
parser.on('-x', '--no-exit', 'Can\'t exit from the application') do
options[:no_exit] = true
end
parser.on('-h', '--help', 'Display this help information') do
puts parser
exit!

View File

@ -46,7 +46,7 @@ module MailCatcher extend self
end
end
@defaults = {
@@defaults = {
:smtp_ip => '127.0.0.1',
:smtp_port => '1025',
:http_ip => '127.0.0.1',
@ -55,10 +55,19 @@ module MailCatcher extend self
:daemon => !windows?,
:growl => growlnotify?,
:browse => false,
:quit => true,
}
def options
@@options
end
def quittable?
options[:quit]
end
def parse! arguments=ARGV, defaults=@defaults
@defaults.dup.tap do |options|
@@defaults.dup.tap do |options|
OptionParser.new do |parser|
parser.banner = "Usage: mailcatcher [options]"
parser.version = VERSION
@ -83,13 +92,17 @@ module MailCatcher extend self
options[:http_port] = port
end
parser.on("--no-quit", "Don't allow quitting the process") do
options[:quit] = false
end
if mac?
parser.on("--[no-]growl", "Growl to the local machine when a message arrives") do |growl|
if growl and not growlnotify?
puts "You'll need to install growlnotify from the Growl installer."
puts
puts "See: http://growl.info/extras.php#growlnotify"
exit!
exit -2
end
options[:growl] = growl
@ -114,7 +127,7 @@ module MailCatcher extend self
parser.on('-h', '--help', 'Display this help information') do
puts parser
exit!
exit
end
end.parse!
end
@ -122,10 +135,13 @@ module MailCatcher extend self
def run! options=nil
# If we are passed options, fill in the blanks
options &&= @defaults.merge options
options &&= options.reverse_merge @@defaults
# Otherwise, parse them from ARGV
options ||= parse!
# Stash them away for later
@@options = options
puts "Starting MailCatcher"
Thin::Logging.silent = true
@ -161,7 +177,11 @@ module MailCatcher extend self
# Daemonize, if we should, but only after the servers have started.
if options[:daemon]
EventMachine.next_tick do
puts "*** MailCatcher runs as a daemon by default. Go to the web interface to quit."
if quittable?
puts "*** MailCatcher runs as a daemon by default. Go to the web interface to quit."
else
puts "*** MailCatcher is now running as a daemon that cannot be quit."
end
Process.daemon
end
end
@ -182,7 +202,7 @@ protected
rescue RuntimeError
if $!.to_s =~ /\bno acceptor\b/
puts "~~> ERROR: Something's using port #{port}. Are you already running MailCatcher?"
exit(-1)
exit -1
else
raise
end

View File

@ -18,8 +18,12 @@ class MailCatcher::Web < Sinatra::Base
end
delete '/' do
MailCatcher.quit!
status 204
if MailCatcher.quittable?
MailCatcher.quit!
status 204
else
status 403
end
end
get '/messages' do

View File

@ -21,8 +21,9 @@
%input{:type => 'search', :name => 'search', :placeholder => 'Search messages...', :incremental => true}
%li.clear
%a{:href => '#', :title => 'Clear all messages'} Clear
%li.quit
%a{:href => '#', :title => 'Quit MailCatcher'} Quit
- unless MailCatcher.options[:no_exit]
%li.quit
%a{:href => '#', :title => 'Quit MailCatcher'} Quit
%nav#messages
%table
%thead