class MiGA::Cli::Action::Db

Public Instance Methods

complete() click to toggle source
Calls superclass method MiGA::Cli::Action#complete
# File lib/miga/cli/action/db.rb, line 99
def complete
  @ftp.close unless @ftp.nil?
  super
end
empty_action() click to toggle source
# File lib/miga/cli/action/db.rb, line 95
def empty_action
  cli.puts 'Downloading latest version of the default database'
end
parse_cli() click to toggle source
# 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
perform() click to toggle source
# 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