53 lines
1.3 KiB
Text
53 lines
1.3 KiB
Text
|
#!/usr/bin/env ruby
|
||
|
# frozen_string_literal: true
|
||
|
|
||
|
require 'bundler/setup'
|
||
|
require 'ace_of_base'
|
||
|
require 'getoptlong'
|
||
|
|
||
|
options = GetoptLong.new(['--help', '-h', GetoptLong::NO_ARGUMENT])
|
||
|
|
||
|
def print_help_message_and_exit
|
||
|
puts <<~HELP_MESSAGE
|
||
|
import [OPTION] FILE
|
||
|
|
||
|
-h, --help:
|
||
|
show this help message
|
||
|
|
||
|
FILE: The path to the file you wish to import.
|
||
|
HELP_MESSAGE
|
||
|
|
||
|
exit(0)
|
||
|
end
|
||
|
|
||
|
print_help_message_and_exit if options.each.to_a.any? { |name, _value| name == '--help' }
|
||
|
|
||
|
begin
|
||
|
file_name = ARGV[0].to_s
|
||
|
File.open(file_name, 'r') do |file|
|
||
|
storage = AceOfBase::Storage.new
|
||
|
importer = AceOfBase::FileImporter.import(file)
|
||
|
if importer.valid?
|
||
|
importer.records.each { |record| storage.store(record) }
|
||
|
puts "Imported #{importer.records.size} records."
|
||
|
else
|
||
|
warn "Unable to import file #{file_name.inspect}:"
|
||
|
importer.errors.each do |line_no, error_messages|
|
||
|
error_messages.each do |error_message|
|
||
|
warn format('%5d: %s', line_no, error_message)
|
||
|
end
|
||
|
end
|
||
|
warn "\nPlease correct the above errors, and try again."
|
||
|
exit(1)
|
||
|
end
|
||
|
end
|
||
|
rescue StandardError => e
|
||
|
warn <<~FILE_ERROR_MESSAGE
|
||
|
Unable to import file #{file_name.inspect}: #{e.message}
|
||
|
FILE_ERROR_MESSAGE
|
||
|
|
||
|
e.backtrace.each { |bt| warn(bt) }
|
||
|
|
||
|
exit(1)
|
||
|
end
|