Skip to content

Commit

Permalink
New option miga ls --fields
Browse files Browse the repository at this point in the history
- Refactored MiGA::Cli::Action::Ls
- Minor changes to help message
- Added advance for metadata collection
- Version bump 0.5.10.0
  • Loading branch information
lmrodriguezr committed Feb 25, 2020
1 parent 7e3f10b commit 582a803
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
53 changes: 36 additions & 17 deletions lib/miga/cli/action/ls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ def parse_cli
opt.on(
'-m', '--metadata STRING',
'Print name and metadata field only',
'If set, ignores -i and assumes --tab'
'If set, ignores --info and forces --tab (without header)'
) { |v| cli[:datum] = v }
opt.on(
'-f', '--fields STR1,STR2,STR3', Array,
'Comma-delimited metadata fields to print'
) { |v| cli[:fields] = v }
opt.on(
'--tab',
'Return a tab-delimited table'
Expand All @@ -45,30 +49,45 @@ def parse_cli
def perform
ds = cli.load_and_filter_datasets(cli[:silent])
exit(ds.empty? ? 1 : 0) if cli[:silent]
io = cli[:output].nil? ? $stdout : File.open(cli[:output], 'w')
if !cli[:datum].nil?
ds.each do |d|
v = d.metadata[cli[:datum]]
cli.puts(io, "#{d.name}\t#{v.nil? ? '?' : v}")
cli[:tabular] = true
format_table(ds, [nil,nil]) { |d| [d.name, d.metadata[cli[:datum]]] }
elsif !cli[:fields].nil?
format_table(ds, [:name] + cli[:fields]) do |d|
[d.name] + cli[:fields].map { |f| d.metadata[f] }
end
elsif cli[:info]
cli.table(Dataset.INFO_FIELDS, ds.map { |d| d.info }, io)
format_table(ds, Dataset.INFO_FIELDS) { |d| d.info }
elsif cli[:processing]
comp = %w[- done queued]
cli.table(
[:name] + MiGA::Dataset.PREPROCESSING_TASKS,
ds.map { |d| [d.name] + d.profile_advance.map { |i| comp[i] } },
io
)
format_table(ds, [:name] + MiGA::Dataset.PREPROCESSING_TASKS) do |d|
[d.name] + d.profile_advance.map { |i| comp[i] }
end
elsif cli[:taskstatus]
cli.table(
[:name] + MiGA::Dataset.PREPROCESSING_TASKS,
ds.map { |d| [d.name] + d.results_status.values },
io
)
format_table(ds, [:name] + MiGA::Dataset.PREPROCESSING_TASKS) do |d|
[d.name] + d.results_status.values
end
else
ds.each { |d| cli.puts(io, d.name) }
cli[:tabular] = true
format_table(ds, [nil]) { |d| [d.name] }
end
end

private

def format_table(ds, header, &blk)
io = cli[:output].nil? ? $stdout : File.open(cli[:output], 'w')
cli.say 'Collecting metadata'
k = 0
cli.table(
header,
ds.map do |d|
cli.advance('Datasets:', k += 1, ds.size, false)
blk[d]
end,
io
)
cli.say ''
io.close unless cli[:output].nil?
end
end
3 changes: 2 additions & 1 deletion lib/miga/common/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module MiGA::Common::Format
# Tabulates an +values+, and Array of Arrays, all with the same number of
# entries as +header+. Returns an Array of String, one per line.
def tabulate(header, values, tabular = false)
fields = [header.map(&:to_s)]
fields = []
fields << header.map(&:to_s) unless tabular && header.all?(&:nil?)
fields << fields.first.map { |h| h.gsub(/\S/, '-') } unless tabular
fields += values.map { |r| r.map { |cell| cell.nil? ? '?' : cell.to_s } }
clen = tabular ? Array.new(header.size, 0) :
Expand Down
4 changes: 2 additions & 2 deletions lib/miga/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ module MiGA
# - Float representing the major.minor version.
# - Integer representing gem releases of the current version.
# - Integer representing minor changes that require new version number.
VERSION = [0.5, 9, 0]
VERSION = [0.5, 10, 0]

##
# Nickname for the current major.minor version.
VERSION_NAME = 'collotype'

##
# Date of the current gem release.
VERSION_DATE = Date.new(2020, 2, 21)
VERSION_DATE = Date.new(2020, 2, 25)

##
# Reference of MiGA.
Expand Down

0 comments on commit 582a803

Please sign in to comment.