This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
ace-of-base/exe/import
James Harton 97341bf6b2 Fini.
2019-07-15 19:03:58 +12:00

59 lines
1.5 KiB
Ruby
Executable file

#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
require 'ace_of_base'
require 'optionparser'
options = {}
option_parser = OptionParser.new do |opts|
opts.banner = 'Usage: import [OPTIONS]'
opts.on('-h', '--help', 'Show this help message') do
puts opts
exit(0)
end
opts.on('-fFILE', '--file FILE', 'Path to the file you wish to import') do |path|
options[:file] = path
end
opts.on('-tDIR', '--target DIR', 'Path to the directory where you want to store the imported data') do |path|
options[:target] = path
end
end
option_parser.parse!
unless options[:file]
puts option_parser
exit(0)
end
begin
file_name = options[:file]
File.open(file_name, 'r') do |file|
storage = AceOfBase::Storage.new(options.fetch(:target, AceOfBase::Storage::STORAGE_DIR))
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