Class: Gzr::Commands::Folder::Tree

Inherits:
Gzr::Command show all
Includes:
Folder
Defined in:
lib/gzr/commands/folder/tree.rb

Instance Method Summary collapse

Methods included from Folder

#all_folders, #create_folder, #delete_folder, included, #process_args, #query_folder, #query_folder_children, #search_folders

Methods inherited from Gzr::Command

#all_color_collections, #color_collection, #color_palette_lookup!, #create_merge_query, #create_query, #default_color_collection, #field_expressions_eval, #field_names, #find_color_palette_reference, #find_vis_config_reference, #get_auth, #get_user_by_id, #keys_to_keep, #keys_to_keep_internal, #merge_query, #pairs, #query, #render_csv, #rewrite_color_palette!, #run_inline_query, #update_color_palette!

Methods included from Session

#build_connection_hash, #login, #logout_all, #pastel, #read_token_data, #say_error, #say_ok, #say_warning, #sufficient_version?, #token_file, #update_auth, #with_session, #write_token_data

Constructor Details

#initialize(filter_spec, options) ⇒ Tree

Returns a new instance of Tree.



57
58
59
60
61
# File 'lib/gzr/commands/folder/tree.rb', line 57

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

Instance Method Details

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gzr/commands/folder/tree.rb', line 63

def execute(input: $stdin, output: $stdout)
  say_warning("options: #{@options.inspect}") if @options[:debug]
  with_session do
    folder_ids = process_args([@filter_spec])

    tree_data = Hash.new

    folder_ids.each do |folder_id|
      s = query_folder(folder_id, "id,name,parent_id,looks(id,title),dashboards(id,title)")
      folder_name = s[:name]
      folder_name = "nil (#{s[:id]})" unless folder_name
      folder_name = "\"#{folder_name}\"" if ((folder_name != folder_name.strip) || (folder_name.length == 0))
      folder_name += " (#{folder_id})" unless folder_ids.length == 1
      tree_data[folder_name] =
        [ recurse_folders(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_folders(folder_id) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gzr/commands/folder/tree.rb', line 86

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