Class: Legion::CLI::Dataset

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/dataset_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/legion/cli/dataset_command.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#export(name, path) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/legion/cli/dataset_command.rb', line 100

def export(name, path)
  out = formatter
  with_dataset_client do |client|
    kwargs = { name: name, path: path, format: options[:format] }
    kwargs[:version] = options[:version] if options[:version]
    result = client.export_dataset(**kwargs)
    if options[:json]
      out.json(result)
    else
      out.success("Exported #{result[:row_count]} rows to #{result[:path]}")
    end
  end
end

#import(name, path) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/legion/cli/dataset_command.rb', line 75

def import(name, path)
  out = formatter
  with_dataset_client do |client|
    unless File.exist?(path)
      out.error("File not found: #{path}")
      raise SystemExit, 1
    end

    result = client.import_dataset(
      name:        name,
      path:        path,
      format:      options[:format],
      description: options[:description]
    )
    if options[:json]
      out.json(result)
    else
      out.success("Imported '#{result[:name]}' v#{result[:version]} (#{result[:row_count]} rows)")
    end
  end
end

#listObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/cli/dataset_command.rb', line 18

def list
  out = formatter
  with_dataset_client do |client|
    datasets = client.list_datasets
    if options[:json]
      out.json(datasets)
    elsif datasets.empty?
      out.warn('No datasets found')
    else
      rows = datasets.map do |d|
        [d[:name].to_s, (d[:description] || '').to_s,
         (d[:latest_version] || '-').to_s, (d[:row_count] || 0).to_s]
      end
      out.table(%w[name description version row_count], rows)
    end
  end
end

#show(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/legion/cli/dataset_command.rb', line 39

def show(name)
  out = formatter
  with_dataset_client do |client|
    kwargs = { name: name }
    kwargs[:version] = options[:version] if options[:version]
    result = client.get_dataset(**kwargs)
    if result[:error]
      out.error("Dataset '#{name}': #{result[:error]}")
      raise SystemExit, 1
    end

    if options[:json]
      out.json(result)
    else
      out.header("Dataset: #{result[:name]}")
      out.spacer
      out.detail({ version: result[:version], row_count: result[:row_count] })
      out.spacer
      preview = (result[:rows] || []).first(10)
      if preview.empty?
        out.warn('No rows in this dataset version')
      else
        rows = preview.map do |r|
          [r[:row_index].to_s, r[:input].to_s.slice(0, 60), (r[:expected_output] || '').to_s.slice(0, 60)]
        end
        out.table(%w[index input expected_output], rows)
        remaining = result[:row_count].to_i - preview.size
        out.warn("... #{remaining} more rows not shown") if remaining.positive?
      end
    end
  end
end