class MiGA::Cli::Action::TaxIndex

Public Instance Methods

parse_cli() click to toggle source
# File lib/miga/cli/action/tax_index.rb, line 9
def parse_cli
  cli.defaults = {format: :json}
  cli.parse do |opt|
    cli.opt_object(opt, [:project])
    opt.on(
      '-i', '--index PATH',
      '(Mandatory) File to create with the index'
      ){ |v| cli[:index] = v }
    opt.on(
      '-f', '--format STRING',
      "Format of the index file, by default: #{cli[:format]}",
      'Supported: json, tab.'
      ){ |v| cli[:format] = v.downcase.to_sym }
    cli.opt_filter_datasets(opt)
  end
end
perform() click to toggle source
# File lib/miga/cli/action/tax_index.rb, line 26
def perform
  cli.ensure_par(index: '-i')
  ds = cli.load_and_filter_datasets
  ds.keep_if {|d| !d.metadata[:tax].nil? }

  cli.say 'Indexing taxonomy'
  tax_index = MiGA::TaxIndex.new
  ds.each { |d| tax_index << d }

  cli.say 'Saving index'
  File.open(cli[:index], 'w') do |fh|
    case cli[:format]
    when :json
      fh.print tax_index.to_json
    when :tab
      fh.print tax_index.to_tab
    else
      raise "Unsupported output format: #{cli[:format]}"
    end
  end
end