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

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, global_options)
  return if File.exist?(global_options[:db_config])
  STDERR.puts "Creating new config file #{file}"
  Specify::Configuration::Config.empty file do |config|
    config.add_host global_options[:host], global_options[: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(options)
  if options[:d]
    :discipline
  elsif options[:c]
    :collection
  elsif options[:t]
    { user_type: options[:t] }
  elsif options[:u]
    { user: options[: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’.

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/specify/cli/database_setup.rb', line 32

def self.proceed?(message)
  STDERR.puts message
  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(message)
  STDERR.print message
  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(options)
  params = { 'dataset_name' => options[:dataset],
             'cataloger' => options[:cataloger],
             'accession' => options[:accession],
             'collecting_data' => arg_to_hash(options[:geography]),
             'default_locality_name' => options[:locality],
             'determination' => arg_to_hash(options[:taxon]) }
  return params unless options[:preptype]
  params['preparation'] = { type: options[:preptype],
                            count: options[: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(global_options, args, options)
  params = {}
  stub_generator = {}
  stub_generator[:host] = global_options[:host]
  stub_generator[:database] = global_options[:database]
  stub_generator[:collection] = args.shift
  stub_generator[:specify_user] = global_options[:specify_user]
  stub_generator[:config] = global_options[:db_config]
  params[:stub_generator] = stub_generator
  params.merge stub_parameters(options)
end