]>
gitweb.michael.orlitzky.com - mailshears.git/blob - bin/mailshears
3 # mailshears, to prune unused mail directories.
6 # Define a usage string using the program name.
7 exe
= File
.basename($PROGRAM_NAME)
8 usage
= "#{exe} [prune | rm <target> | mv <src> <dst>]"
14 # Now, if a mode was supplied, it should be in ARGV[0].
16 mode_names
= ['prune', 'rm', 'mv']
17 if mode_names
.include?(ARGV.first().downcase()) then
18 # Peel the mode name off the head of the list.
19 mode_name
= ARGV.shift()
23 if mode_name
== 'rm' then
25 elsif mode_name
== 'mv' then
29 # Need this before referencing ExitCodes.
32 # Since we removed both the executable name and the mode name (if it
33 # existed) from ARGV, what remains should be the required arguments.
34 # Figure out if we have the wrong number of arguments, and store the
35 # associated error message in args_error_message if we do.
36 args_error_message
= nil
37 if mode
== :prune and ARGV.length() !
= 0 then
38 args_error_message
= "ERROR: prune mode takes no additional arguments."
39 elsif mode
== :rm and ARGV.length() < 1 then
40 args_error_message
= "ERROR: rm mode takes two or more user arguments."
41 elsif mode
== :mv and ARGV.length() !
= 2 then
42 args_error_message
= "ERROR: mv mode takes exactly two user arguments."
45 # If we got the wrong number of arguments, we'll have some error
46 # message. Report it and quit.
47 if not args_error_message
.nil? then
48 STDERR.puts args_error_message
49 puts
"Usage: #{usage}"
50 Kernel
.exit(ExitCodes
::BAD_COMMAND_LINE)
54 cfg
= Configuration
.new()
56 # Load each of the plugins that we'll need.
57 cfg
.plugins
.each
do |plugin_file
|
58 require "#{mode_name}/plugins/#{plugin_file}"
62 require "#{mode_name}/#{mode_name}_runner"
63 require "#{mode_name}/#{mode_name}_dummy_runner"
65 def make_header(plugin_name
)
66 # The header that we output before the list of domains/users.
67 # Just the path of this script, the current time, and the plugin name.
68 exe
= File
.basename($PROGRAM_NAME)
71 current_time
= Time
.now()
72 if current_time
.respond_to
?(:iso8601)
73 # Somehow this method is missing on some machines.
74 header +
= current_time
.iso8601
.to_s
76 # Fall back to whatever this looks like.
77 header +
= current_time
.to_s
80 header +
= ' (Plugin: ' + plugin_name +
")\n"
81 header +
= '-' * header
.size
# Underline the header.
90 plugin_module
= RmPlugin
91 elsif mode
== :mv then
92 plugin_module
= MvPlugin
94 # Safe, catch-all default
95 plugin_module
= PrunePlugin
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
104 # Parse the remaining arguments as User/Domain objects. If we get some
105 # other argument that isn't one of those, it's an error.
111 rescue InvalidUserError
115 rescue InvalidDomainError
116 STDERR.puts
"ERROR: invalid user/domain argument #{arg}"
117 Kernel
.exit(ExitCodes
::BAD_COMMAND_LINE)
123 plugin_module
.run(cfg
, *parsed_args
)
125 # Now restore stdout, and print the header plus whatever the plugins
126 # produced if they produced anything. If they didn't produce any
127 # output, then we avoid printing the header.
129 # This gets wrapped in an "ensure" block because otherwise, if
130 # plugin_module.run() crashes, the traceback will get stored in
131 # output_buffer and never get printed.
133 if output_buffer
.size
> 0 then
134 puts
make_header(plugin_module
.to_s())
135 puts output_buffer
.string()