]>
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 # If a mode was supplied, it should be in ARGV[0].
18 mode_names
= ['prune', 'rm', 'mv']
19 if mode_names
.include?(ARGV.first().downcase()) then
20 # Peel the mode name off the head of the list.
21 mode_name
= ARGV.shift()
25 # Determine the mode from its name.
26 if mode_name
== 'rm' then
28 elsif mode_name
== 'mv' then
32 # Since we removed the mode name (if it existed) from ARGV, what
33 # remains should be the required arguments. Figure out if we have the
34 # wrong number of arguments, and store the associated error message in
35 # args_error_message if necessary.
36 args_error_message
= nil
38 if mode
== :prune and ARGV.length() !
= 0 then
39 args_error_message
= "ERROR: prune mode takes no additional arguments."
40 elsif mode
== :rm and ARGV.length() < 1 then
41 args_error_message
= "ERROR: rm mode takes two or more user arguments."
42 elsif mode
== :mv and ARGV.length() !
= 2 then
43 args_error_message
= "ERROR: mv mode takes exactly two user arguments."
46 # If we got the wrong number of arguments, we'll have an error message
47 # here. Report it and exit with a failure code.
48 if not args_error_message
.nil? then
49 STDERR.puts args_error_message
50 puts
"Usage: #{UserInterface.usage(program_name)}"
51 Kernel
.exit(ExitCodes
::BAD_COMMAND_LINE)
55 # Load each of the plugins that we'll need.
56 cfg
= Configuration
.new()
58 cfg
.plugins
.each
do |plugin_file
|
59 require "#{mode_name}/plugins/#{plugin_file}"
63 require "#{mode_name}/#{mode_name}_runner"
64 require "#{mode_name}/#{mode_name}_dummy_runner"
66 # Now we figure out which plugin module to use based on our mode.
70 plugin_module
= RmPlugin
71 elsif mode
== :mv then
72 plugin_module
= MvPlugin
74 # Safe, catch-all default
75 plugin_module
= PrunePlugin
78 # Parse the remaining arguments as User/Domain objects. If we get some
79 # other argument that isn't one of those, it's an error.
86 rescue InvalidUserError
90 rescue InvalidDomainError
91 STDERR.puts
"ERROR: invalid user/domain argument #{arg}"
92 Kernel
.exit(ExitCodes
::BAD_COMMAND_LINE)
98 # Buffer the output so that we can avoid printing the informational
99 # header when no plugins produce output.
101 output_buffer
= StringIO
.new()
102 $stdout = output_buffer
105 plugin_module
.run(cfg
, *parsed_args
)
107 # Now restore stdout, and print the header plus whatever the plugins
108 # produced (if they produced anything). If they didn't produce any
109 # output, then we avoid printing the header.
111 # This gets wrapped in an "ensure" block because otherwise, if
112 # plugin_module.run() crashes, the traceback will get stored in
113 # output_buffer and never get printed.
116 if output_buffer
.size
> 0 then
117 puts UserInterface
.make_header(program_name
, plugin_module
.to_s())
118 puts output_buffer
.string()
122 # If we made it here without crashing, well that sounds pretty
124 Kernel
.exit(ExitCodes
::SUCCESS)