Class: Gzr::Commands::Model::Ls

Inherits:
Gzr::Command show all
Includes:
Model
Defined in:
lib/gzr/commands/model/ls.rb

Instance Method Summary collapse

Methods included from Model

#query_all_lookml_models

Methods inherited from Gzr::Command

#create_merge_query, #create_query, #field_expression, #field_names, #keys_to_keep, #merge_query, #pairs, #query, #render_csv, #run_inline_query

Methods included from Session

#build_connection_hash, #login, #logout_all, #pastel, #say_error, #say_ok, #say_warning, #v3_1_available?, #with_session

Constructor Details

#initialize(options) ⇒ Ls

Returns a new instance of Ls.



33
34
35
36
# File 'lib/gzr/commands/model/ls.rb', line 33

def initialize(options)
  super()
  @options = options
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



38
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
# File 'lib/gzr/commands/model/ls.rb', line 38

def execute(input: $stdin, output: $stdout)
  say_warning(@options) if @options[:debug]
  with_session do
    data = query_all_lookml_models(@options[:fields])
    begin
      say_ok "No users found"
      return nil
    end unless data && data.length > 0

    table_hash = Hash.new
    fields = field_names(@options[:fields])
    table_hash[:header] = fields unless @options[:plain]
    expressions = fields.collect { |fn| field_expression(fn) }
    table_hash[:rows] = data.map do |row|
      expressions.collect do |e|
        eval "row.#{e}"
      end
    end
    table = TTY::Table.new(table_hash)
    alignments = fields.collect do |k|
      (k =~ /id$/) ? :right : :left
    end
    begin
      if @options[:csv] then
        output.puts render_csv(table)
      else
        output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments)
      end
    end if table
  end
end