]>
gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
3 # mailshears, to mangle your mail garden
6 # Load all of our lib/ code.
9 # Define a usage string using the program name.
10 program_name
= File
.basename($PROGRAM_NAME)
16 # Before doing anything else, check for "-h" and "--help" in the args,
17 # because those should cause us to dump usage info and bail out.
18 if ARGV.include?('-h') or ARGV.include?('--help') then
19 puts
"Usage: #{UserInterface.usage(program_name)}"
20 Kernel
.exit(ExitCodes
::SUCCESS)
24 # If a mode was supplied, it should be in ARGV[0].
26 mode_names
= ['prune', 'rm', 'mv']
27 if mode_names
.include?(ARGV.first().downcase()) then
28 # Peel the mode name off the head of the list.
29 mode_name
= ARGV.shift()
33 # Determine the mode from its name.
34 if mode_name
== 'rm' then
36 elsif mode_name
== 'mv' then
40 # Since we removed the mode name (if it existed) from ARGV, what
41 # remains should be the required arguments. Figure out if we have the
42 # wrong number of arguments, and store the associated error message in
43 # args_error_message if necessary.
44 args_error_message
= nil
46 if mode
== :prune and ARGV.length() !
= 0 then
47 args_error_message
= "ERROR: prune mode takes no additional arguments."
48 elsif mode
== :rm and ARGV.length() < 1 then
49 args_error_message
= "ERROR: rm mode takes two or more user arguments."
50 elsif mode
== :mv and ARGV.length() !
= 2 then
51 args_error_message
= "ERROR: mv mode takes exactly two user arguments."
54 # If we got the wrong number of arguments, we'll have an error message
55 # here. Report it and exit with a failure code.
56 if not args_error_message
.nil? then
57 STDERR.puts args_error_message
58 puts
"Usage: #{UserInterface.usage(program_name)}"
59 Kernel
.exit(ExitCodes
::BAD_COMMAND_LINE)
63 # Load each of the plugins that we'll need.
64 cfg
= Configuration
.new()
66 cfg
.plugins
.each
do |plugin_file
|
67 require "#{mode_name}/plugins/#{plugin_file}"
71 require "#{mode_name}/#{mode_name}_runner"
72 require "#{mode_name}/#{mode_name}_dummy_runner"
74 # Now we figure out which plugin module to use based on our mode.
78 plugin_module
= RmPlugin
79 elsif mode
== :mv then
80 plugin_module
= MvPlugin
82 # Safe, catch-all default
83 plugin_module
= PrunePlugin
86 # Parse the remaining arguments as User/Domain objects. If we get some
87 # other argument that isn't one of those, it's an error.
94 rescue InvalidUserError
98 rescue InvalidDomainError
99 STDERR.puts
"ERROR: invalid user/domain argument #{arg}"
100 Kernel
.exit(ExitCodes
::BAD_COMMAND_LINE)
106 # Buffer the output so that we can avoid printing the informational
107 # header when no plugins produce output.
109 output_buffer
= StringIO
.new()
110 $stdout = output_buffer
113 plugin_module
.run(cfg
, *parsed_args
)
115 # Now restore stdout, and print the header plus whatever the plugins
116 # produced (if they produced anything). If they didn't produce any
117 # output, then we avoid printing the header.
119 # This gets wrapped in an "ensure" block because otherwise, if
120 # plugin_module.run() crashes, the traceback will get stored in
121 # output_buffer and never get printed.
124 if output_buffer
.size
> 0 then
125 puts UserInterface
.make_header(program_name
, plugin_module
.to_s())
126 puts output_buffer
.string()
130 # If we made it here without crashing, well that sounds pretty
132 Kernel
.exit(ExitCodes
::SUCCESS)