-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrt-tind-harvester
executable file
·74 lines (58 loc) · 2.1 KB
/
mrt-tind-harvester
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
Dir.chdir(__dir__) { require 'bundler/setup' }
require 'optparse'
require 'optparse/time'
require 'ostruct'
require 'merritt/tind'
USAGE = OpenStruct.new(
CONFIG: 'path to configuration file (required)'.freeze,
DRY_RUN: 'dry run (harvest, but do not submit or update last_harvest)'.freeze,
FROM: 'start date/time (inclusive) for selective harvesting'.freeze,
UNTIL: 'end date/time (inclusive) for selective harvesting'.freeze,
HELP: 'print help and exit'
).freeze
class TINDHarvesterApp
attr_reader :options
def initialize
@options = OpenStruct.new(dry_run: false, help: false)
end
def option_parser
@option_parser = OptionParser.new do |opts|
opts.on('-c CONFIG', '--config CONFIG', USAGE.CONFIG) { |config_file| options.config_file = config_file }
opts.on('-f', '--from DATETIME', Time, USAGE.FROM) { |from_time| options.from_time = to_utc_time(from_time) }
opts.on('-u', '--until DATETIME', Time, USAGE.UNTIL) { |until_time| options.until_time = to_utc_time(until_time) }
opts.on('-n', '--dry-run', USAGE.DRY_RUN) { options.dry_run = true }
opts.on('-h', '--help', USAGE.HELP) { options.help = true }
end
end
def do_harvest!
option_parser.parse!
print_help_and_exit(0) if options.help
harvester = Merritt::TIND::Harvester.from_file(config_file, dry_run: options.dry_run)
warn("Starting harvester; logging to #{harvester.log_path}")
harvester.process_feed!(from_time: options.from_time, until_time: options.until_time)
rescue StandardError => e
warn(e)
print_help_and_exit(1)
end
private
def config_file
config_file = options.config_file
return config_file if config_file && File.exist?(config_file)
exit_with_error('No configuration file specified') unless config_file
exit_with_error('The specified configuration file does not exist: ' + config_file)
end
def help
option_parser.to_s
end
def exit_with_error(msg)
warn(msg)
print_help_and_exit(1)
end
def print_help_and_exit(status)
warn(help)
exit(status)
end
end
app = TINDHarvesterApp.new
app.do_harvest!