Class: Gzr::Commands::Space::Tree

Inherits:
Gzr::Command show all
Includes:
Space
Defined in:
lib/gzr/commands/space/tree.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) ⇒ Tree

Returns a new instance of Tree.



33
34
35
36
37
# File 'lib/gzr/commands/space/tree.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
# File 'lib/gzr/commands/space/tree.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])

    tree_data = Hash.new
    
    space_ids.each do |space_id| 
      s = query_space(space_id, "id,name,parent_id,looks(id,title),dashboards(id,title)")
      space_name = s.name
      space_name = "nil (#{s.id})" unless space_name 
      space_name = "\"#{space_name}\"" if ((space_name != space_name.strip) || (space_name.length == 0))
      space_name += " (#{space_id})" unless space_ids.length == 1
      tree_data[space_name] =
        [ recurse_spaces(s.id) ] +
        s.looks.map { |l| "(l) #{l.title}" } +
          s.dashboards.map { |d| "(d) #{d.title}" }
    end
    tree = TTY::Tree.new(tree_data)
    output.puts tree.render
  end
end

#recurse_spaces(space_id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gzr/commands/space/tree.rb', line 62

def recurse_spaces(space_id)
  data = query_space_children(space_id, "id,name,parent_id,looks(id,title),dashboards(id,title)")
  tree_branch = Hash.new
  data.each do |s|
    space_name = s.name
    space_name = "nil (#{s.id})" unless space_name 
    space_name = "\"#{space_name}\"" if ((space_name != space_name.strip) || (space_name.length == 0))
    tree_branch[space_name] =
      [ recurse_spaces(s.id) ] +
      s.looks.map { |l| "(l) #{l.title}" } +
      s.dashboards.map { |d| "(d) #{d.title}" }
  end
  tree_branch
end