Class: MiGA::Cli::Action::Db

Inherits:
MiGA::Cli::Action show all
Defined in:
lib/miga/cli/action/db.rb

Constant Summary

Constants included from MiGA

MiGA::CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute Summary

Attributes inherited from MiGA::Cli::Action

#cli

Attributes included from MiGA::Common::Net

#remote_connection_uri

Instance Method Summary collapse

Methods inherited from MiGA::Cli::Action

#initialize, #launch, load, #name

Methods inherited from MiGA

CITATION, CITATION_ARRAY, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, #advance, debug?, debug_trace?, initialized?, #like_io?, #num_suffix, rc_path, #result_files_exist?, #say

Methods included from MiGA::Common::Path

#root_path, #script_path

Methods included from MiGA::Common::Format

#clean_fasta_file, #seqs_length, #tabulate

Methods included from MiGA::Common::Net

#download_file_ftp, #http_request, #known_hosts, #main_server, #net_method, #normalize_encoding

Methods included from MiGA::Common::SystemCall

#run_cmd, #run_cmd_opts

Constructor Details

This class inherits a constructor from MiGA::Cli::Action

Instance Method Details

#completeObject



99
100
101
102
# File 'lib/miga/cli/action/db.rb', line 99

def complete
  @ftp.close unless @ftp.nil?
  super
end

#empty_actionObject



95
96
97
# File 'lib/miga/cli/action/db.rb', line 95

def empty_action
  cli.puts 'Downloading latest version of the default database'
end

#parse_cliObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/miga/cli/action/db.rb', line 8

def parse_cli
  cli.defaults = {
    database: :recommended,
    version: :latest,
    local: File.join(ENV['MIGA_HOME'], '.miga_db'),
    host: MiGA::MiGA.known_hosts(:miga_db),
    pb: true,
    reuse_archive: false,
    overwrite: true
  }
  cli.parse do |opt|
    opt.on(
      '-n', '--database STRING',
      "Name of the database to download. By default: #{cli[:database]}"
    ) { |v| cli[:database] = v.to_sym }
    opt.on(
      '--db-version STRING',
      "Database version to download. By default: #{cli[:version]}"
    ) { |v| cli[:version] = v.to_sym }
    opt.on(
      '-l', '--local-dir PATH',
      "Local directory to store the database. By default: #{cli[:local]}"
    ) { |v| cli[:local] = v }
    opt.on(
      '--host STRING',
      "Remote host of the database. By default: #{cli[:host]}"
    ) { |v| cli[:host] = v }
    opt.on(
      '--list',
      'List available databases and exit'
    ) { |v| cli[:list_databases] = v }
    opt.on(
      '--list-versions',
      'List available versions of the database and exit'
    ) { |v| cli[:list_versions] = v }
    opt.on(
      '--list-local',
      'List only the versions of the local databases (if any) and exit'
    ) { |v| cli[:list_local] = v }
    opt.on(
      '--reuse-archive',
      'Reuse a previously downloaded archive if available'
    ) { |v| cli[:reuse_archive] = v }
    opt.on(
      '--no-overwrite',
      'Exit without downloading if the target database already exists'
    ) { |v| cli[:overwrite] = v }
    opt.on(
      '--tab',
      'Return a tab-delimited table'
    ) { |v| cli[:tabular] = v }
    opt.on('--no-progress', 'Supress progress bars') { |v| cli[:pb] = v }
  end
end

#performObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/miga/cli/action/db.rb', line 63

def perform
  # Quick check when the database is not an alias
  dir = File.join(cli[:local], cli[:database].to_s)
  if !cli[:overwrite] && Dir.exist?(dir)
    cli.puts "Database exists: #{dir}"
    return
  end

  # If dealing with local checks only
  if cli[:list_local]
    list_local
    return
  end

  # Remote manifest
  @ftp = remote_connection
  manif = remote_manifest(@ftp)
  cli.puts "# Host: #{manif[:host]}"
  cli.puts "# Manifest last update: #{manif[:last_update]}"
  list_databases(manif) and return
  db = db_requested(manif)
  list_versions(db) and return
  ver = version_requested(db)
  check_target and return

  # Download and expand
  file = download_file(@ftp, ver[:path], cli[:reuse_archive])
  check_digest(ver, file)
  unarchive(file)
  register_database(manif, db, ver)
end