Module: Specify::CLI
- Defined in:
- lib/specify/cli.rb,
lib/specify/cli/stubs.rb,
lib/specify/cli/viewset.rb,
lib/specify/cli/database_setup.rb
Overview
The CLI module contains methods used by the command line interface.
Class Method Summary collapse
-
.arg_to_hash(arg) ⇒ Object
Transforms
arg
(a String passed as a command line argument) containing hierarchical information (such as geographic or taxonomic) into a structured Hash that can be used to set the Specify::Service::StubGenerator#collecting_data= or Specify::Service::StubGenerator#determination=. -
.configure_database(config) ⇒ Object
Asks the user to configure a database.
-
.configure_host(config) ⇒ Object
Asks the user to configure a host.
-
.db_config!(file, global_options) ⇒ Object
Creates a new database configuratin YAML file.
-
.level(options) ⇒ Object
Parses the level for the Specify::Service::ViewLoader to upload .vioews.xml files to from the command
options
. -
.make_stubs(generator, count) ⇒ Object
Creates stub records with
generator
(a Specify::Service::StubGenerator). -
.proceed?(message) ⇒ Boolean
Asks the user to proceed.
-
.require_input(message) ⇒ Object
Prompts the user for input with
message
. -
.stub_parameters(options) ⇒ Object
Parses the parameters for stub records to created by a Specify::Service::StubGenerator from the command
options
. -
.wrap_args(global_options, args, options) ⇒ Object
Returns a Hash for intialization of a Specify::Service::StubGenerator from
global_options
,args
, and commandoptions
.
Class Method Details
.arg_to_hash(arg) ⇒ Object
Transforms arg
(a String passed as a command line argument) containing hierarchical information (such as geographic or taxonomic) into a structured Hash that can be used to set the Specify::Service::StubGenerator#collecting_data= or Specify::Service::StubGenerator#determination=.
10 11 12 13 14 15 16 |
# File 'lib/specify/cli/stubs.rb', line 10 def self.arg_to_hash(arg) return unless arg arg.split(';') .map { |pair| pair.split(':').map(&:strip) } .to_h .transform_keys { |key| key == 'locality' ? key.to_sym : key } end |
.configure_database(config) ⇒ Object
Asks the user to configure a database.
6 7 8 9 10 11 12 |
# File 'lib/specify/cli/database_setup.rb', line 6 def self.configure_database(config) STDERR.puts "Configuring new database: #{config.database}" config.user_name = require_input 'MySQL user name' config.user_password = require_input 'password (blank for prompt)' config.session_user = require_input 'Specify user (leave blank to skip)' config.save end |
.configure_host(config) ⇒ Object
Asks the user to configure a host.
15 16 17 18 19 |
# File 'lib/specify/cli/database_setup.rb', line 15 def self.configure_host(config) return unless proceed? "host #{config.host} not known" config.port = require_input 'port number (leave blank for default)' config.save end |
.db_config!(file, global_options) ⇒ Object
Creates a new database configuratin YAML file.
22 23 24 25 26 27 28 |
# File 'lib/specify/cli/database_setup.rb', line 22 def self.db_config!(file, ) return if File.exist?([:db_config]) STDERR.puts "Creating new config file #{file}" Specify::Configuration::Config.empty file do |config| config.add_host [:host], [:port] end end |
.level(options) ⇒ Object
Parses the level for the Specify::Service::ViewLoader to upload .vioews.xml files to from the command options
.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/specify/cli/viewset.rb', line 7 def self.level() if [:d] :discipline elsif [:c] :collection elsif [:t] { user_type: [:t] } elsif [:u] { user: [:u] } else raise 'level required (use -d, -c, -t, or -u option)' end end |
.make_stubs(generator, count) ⇒ Object
Creates stub records with generator
(a Specify::Service::StubGenerator).
count
: the number of stub records to be created.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/specify/cli/stubs.rb', line 21 def self.make_stubs(generator, count) STDERR.puts "started creating #{count} records" STDERR.puts "cataloger: #{generator.cataloger}" generator.database.transaction do generator.create count STDERR.puts "creating: #{generator.generated.last.catalog_number}" end STDERR.puts 'done' puts "generated #{generator.generated.count} catalog numbers:" puts '--------------------------' generator.generated.each { |co| puts co.CatalogNumber } end |
.proceed?(message) ⇒ Boolean
Asks the user to proceed. Returns true
if the user answers answers with ‘Yes’.
32 33 34 35 36 |
# File 'lib/specify/cli/database_setup.rb', line 32 def self.proceed?() STDERR.puts STDERR.print "Configure? (Y/n)" return true if /^[Yy](es)?/.match Readline.readline(': ') end |
.require_input(message) ⇒ Object
Prompts the user for input with message
.
39 40 41 42 43 44 |
# File 'lib/specify/cli/database_setup.rb', line 39 def self.require_input() STDERR.print answer = Readline.readline(': ') return if answer.empty? answer end |
.stub_parameters(options) ⇒ Object
Parses the parameters for stub records to created by a Specify::Service::StubGenerator from the command options
.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/specify/cli/stubs.rb', line 50 def self.stub_parameters() params = { 'dataset_name' => [:dataset], 'cataloger' => [:cataloger], 'accession' => [:accession], 'collecting_data' => arg_to_hash([:geography]), 'default_locality_name' => [:locality], 'determination' => arg_to_hash([:taxon]) } return params unless [:preptype] params['preparation'] = { type: [:preptype], count: [:prepcount] } params.compact end |
.wrap_args(global_options, args, options) ⇒ Object
Returns a Hash for intialization of a Specify::Service::StubGenerator from global_options
, args
, and command options
.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/specify/cli/stubs.rb', line 36 def self.wrap_args(, args, ) params = {} stub_generator = {} stub_generator[:host] = [:host] stub_generator[:database] = [:database] stub_generator[:collection] = args.shift stub_generator[:specify_user] = [:specify_user] stub_generator[:config] = [:db_config] params[:stub_generator] = stub_generator params.merge stub_parameters() end |