Class: Gzr::Commands::Space::Ls

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

Instance Method Summary collapse

Methods included from Space

#all_spaces, #create_space, #delete_space, included, #process_args, #query_space, #query_space_children, #search_spaces

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(filter_spec, options) ⇒ Ls

Returns a new instance of Ls.



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

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

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gzr/commands/space/ls.rb', line 39

def execute(input: $stdin, output: $stdout)
  say_warning("options: #{@options.inspect}") if @options[:debug]
  with_session do
    space_ids = process_args([@filter_spec])
    begin
      puts "No spaces match #{@filter_spec}"
      return nil
    end unless space_ids && space_ids.length > 0

    data = space_ids.map do |space_id|
      query_space(space_id, @options[:fields])
    end.compact

    begin
      puts "No data returned for spaces #{space_ids.inspect}"
      return nil
    end unless data && data.length > 0

    @options[:fields] = 'dashboards(id,title)' if @filter_spec == 'lookml'
    table_hash = Hash.new
    fields = field_names(@options[:fields])
    table_hash[:header] = field_names(@options[:fields]) unless @options[:plain]
    rows = []
    data.each do |r|
      h = r.to_attrs
      if @filter_spec != 'lookml' then
        rows << [h[:parent_id],h[:id],h[:name], nil, nil, nil, nil]
        subspaces = query_space_children(h[:id], "id,name,parent_id")
        rows += subspaces.map do |r|
          h1 = r.to_attrs
          [h1[:parent_id], h1[:id], h1[:name], nil, nil, nil, nil]
        end
      end
      h[:looks].each do |r|
        rows << [h[:parent_id],h[:id],h[:name], r[:id], r[:title], nil, nil]
      end if h[:looks]
      h[:dashboards].each do |r|
        rows << [h[:parent_id],h[:id],h[:name], nil, nil, r[:id], r[:title]] unless @filter_spec == 'lookml'
        rows << [r[:id], r[:title]] if @filter_spec == 'lookml'
      end if h[:dashboards]
    end
    table_hash[:rows] = rows
    table = TTY::Table.new(table_hash) if data[0]
    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