]>
gitweb.michael.orlitzky.com - mailshears.git/blob - mailshears
4d69f8fb67ab2df8a7615e4601b01e8228617a84
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
35 if (mode
== :prune and (ARGV.length() !
= 0)) or
36 (mode
== :rm and (ARGV.length() < 1)) or
37 (mode
== :mv and (ARGV.length() !
= 2)) then
38 puts
"ERROR: missing (or extra) command-line arguments."
39 puts
"Usage: #{usage}"
40 Kernel
.exit(ExitCodes
::BAD_COMMAND_LINE)
43 cfg
= Configuration
.new()
45 # Load each of the plugins that we'll need.
46 cfg
.plugins
.each
do |plugin_file
|
47 require "#{mode_name}/plugins/#{plugin_file}"
51 require "#{mode_name}/#{mode_name}_runner"
52 require "#{mode_name}/#{mode_name}_dummy_runner"
54 def make_header(plugin_name
)
55 # The header that we output before the list of domains/users.
56 # Just the path of this script, the current time, and the plugin name.
57 exe
= File
.basename($PROGRAM_NAME)
60 current_time
= Time
.now()
61 if current_time
.respond_to
?(:iso8601)
62 # Somehow this method is missing on some machines.
63 header +
= current_time
.iso8601
.to_s
65 # Fall back to whatever this looks like.
66 header +
= current_time
.to_s
69 header +
= ' (Plugin: ' + plugin_name +
")\n"
70 header +
= '-' * header
.size
# Underline the header.
79 plugin_module
= RmPlugin
80 elsif mode
== :mv then
81 plugin_module
= MvPlugin
83 # Safe, catch-all default
84 plugin_module
= PrunePlugin
88 # Buffer the output so that we can avoid printing the informational
89 # header when no plugins produce output.
91 output_buffer
= StringIO
.new()
92 $stdout = output_buffer
95 plugin_module
.run(cfg
, *ARGV)
97 # Now restore stdout, and print the header plus whatever the plugins
98 # produced if they produced anything. If they didn't produce any
99 # output, then we avoid printing the header.
101 # This gets wrapped in an "ensure" block because otherwise, if
102 # plugin_module.run() crashes, the traceback will get stored in
103 # output_buffer and never get printed.
105 if output_buffer
.size
> 0 then
106 puts
make_header(plugin_module
.to_s())
107 puts output_buffer
.string()