Class: BerkeleyLibrary::TIND::Export::ExportCommand

Inherits:
Object
  • Object
show all
Extended by:
Util::SysExits
Includes:
Util::SysExits
Defined in:
lib/berkeley_library/tind/export/export_command.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

DEFAULT_FORMAT =
ExportFormat::CSV
FORMATS =
ExportFormat.to_a.map(&:value).join(', ')
OPTS =
{
  f: ['--format FORMAT', "Format (#{FORMATS}; defaults to output file extension, or else to #{DEFAULT_FORMAT})"],
  o: ['--output-file FILE', 'Output file or directory'],
  l: ['--list-collections', 'List collection sizes and names'],
  u: ['--tind-base-url URL', "TIND base URL (default $#{BerkeleyLibrary::TIND::Config::ENV_TIND_BASE_URL})"],
  k: ['--api-key KEY', "TIND API key (default $#{BerkeleyLibrary::TIND::Config::ENV_TIND_API_KEY})"],
  e: ['--env-file [ENV]', 'Read environment variables from <ENV> (default: ./.env)'],
  v: ['--verbose', 'Verbose error logging'],
  h: ['--help', 'Show help and exit']
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, out: $stdout) ⇒ ExportCommand

Returns a new instance of ExportCommand.



20
21
22
23
# File 'lib/berkeley_library/tind/export/export_command.rb', line 20

def initialize(*args, out: $stdout)
  @options = ExportCommand.parse_options(args)
  @out = out
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/berkeley_library/tind/export/export_command.rb', line 17

def options
  @options
end

#outObject (readonly)

Returns the value of attribute out.



18
19
20
# File 'lib/berkeley_library/tind/export/export_command.rb', line 18

def out
  @out
end

Class Method Details

.parse_options(argv) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/berkeley_library/tind/export/export_command.rb', line 66

def parse_options(argv)
  {}.tap do |opts|
    option_parser(opts).parse!(argv)
    opts[:collection] = argv.pop
    opts[:format] = ensure_format(opts)
    validate!(opts)
    configure!(opts)
  end
rescue StandardError => e
  print_usage_and_exit!($stderr, EX_USAGE, e.message)
end

Instance Method Details

#execute!Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/berkeley_library/tind/export/export_command.rb', line 25

def execute!
  return list_collections if options[:list]

  export_collection
rescue StandardError => e
  warn(e)
  warn(e.backtrace.join("\n")) if e.backtrace && options[:verbose]

  exit(EX_SOFTWARE)
end