class MiGA::Cli::Action::TaxIndex

Public Instance Methods

parse_cli() click to toggle source
# File lib/miga/cli/action/tax_index.rb, line 8
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 25
def perform
  cli.ensure_par(index: '-i')
  ds = cli.load_and_filter_datasets

  cli.say 'Indexing taxonomy'
  tax_index = MiGA::TaxIndex.new
  ds.each_with_index do |d, i|
    cli.advance('Datasets:', i, ds.size, false)
    tax_index << d unless d.metadata[:tax].nil?
  end
  cli.say ''

  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