Class: Gzr::Commands::Folder::Export

Inherits:
Gzr::Command show all
Includes:
Dashboard, FileHelper, Folder, Look, Plan
Defined in:
lib/gzr/commands/folder/export.rb

Instance Method Summary collapse

Methods included from FileHelper

#read_file, #write_file

Methods included from Plan

#create_scheduled_plan, #delete_scheduled_plan, #query_all_scheduled_plans, #query_scheduled_plan, #query_scheduled_plans_for_dashboard, #query_scheduled_plans_for_look, #run_scheduled_plan, #update_scheduled_plan, #upsert_plan_for_dashboard, #upsert_plan_for_look, #upsert_plan_for_obj, #upsert_plans_for_dashboard, #upsert_plans_for_look, #upsert_plans_for_obj

Methods included from Dashboard

#cat_dashboard, #create_dashboard, #create_dashboard_element, #create_dashboard_filter, #create_dashboard_layout, #delete_dashboard, #delete_dashboard_element, #delete_dashboard_filter, #delete_dashboard_layout, #get_all_dashboard_layout_components, #get_dashboard_layout, #import_lookml_dashboard, #query_dashboard, #search_dashboards_by_slug, #search_dashboards_by_title, #sync_lookml_dashboard, #trim_dashboard, #update_dashboard, #update_dashboard_element, #update_dashboard_filter, #update_dashboard_layout, #update_dashboard_layout_component

Methods included from Alert

#alert_notifications, #create_alert, #delete_alert, #follow_alert, #get_alert, #read_alert_notification, #search_alerts, #unfollow_alert, #update_alert, #update_alert_field

Methods included from Look

#cat_look, #create_fetch_query, #create_look, #create_merge_result, #delete_look, #query_look, #search_looks_by_slug, #search_looks_by_title, #trim_look, #update_look, #upsert_look

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(folder_id, options) ⇒ Export

Returns a new instance of Export.



43
44
45
46
47
# File 'lib/gzr/commands/folder/export.rb', line 43

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

Instance Method Details

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



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
# File 'lib/gzr/commands/folder/export.rb', line 49

def execute(input: $stdin, output: $stdout)
  say_warning("options: #{@options.inspect}") if @options[:debug]
  with_session do
    if @options[:tar] || @options[:tgz] || @options[:zip] then
      arc_path = Pathname.new(@options[:tgz] || @options[:tar] || @options[:zip])
      arc_path = Pathname.new(File.expand_path(@options[:dir])) + arc_path unless arc_path.absolute?
      if @options[:tar] || @options[:tgz]
        f = File.open(arc_path.to_path, "wb")
        tarfile = StringIO.new(String.new,"w") unless @options[:zip]
        begin
          tw = Gem::Package::TarWriter.new(tarfile)
          process_folder(@folder_id, tw)
          tw.flush
          tarfile.rewind
          if @options[:tgz]
            gzw = Zlib::GzipWriter.new(f)
            gzw.write tarfile.string
            gzw.close
          else
            f.write tarfile.string
          end
        ensure
          f.close
          tarfile.close
        end
      else
        z = Zip::File.new(arc_path.to_path, Zip::File::CREATE, false, continue_on_exists_proc: true)
        begin
          process_folder(@folder_id, z)
        ensure
          z.close
        end
      end
    else
      process_folder(@folder_id, @options[:dir])
    end
  end
end

#process_folder(folder_id, base, rel_path = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gzr/commands/folder/export.rb', line 88

def process_folder(folder_id, base, rel_path = nil)
  folder = query_folder(folder_id)
  name = folder[:name]
  name = "nil (#{folder_id})" if name.nil?
  path = Pathname.new(name.gsub('/',"\u{2215}"))
  path = rel_path + path if rel_path

  write_file("Folder_#{folder[:id]}_#{name}.json", base, path) do |f|
    f.write JSON.pretty_generate(folder.reject do |k,v|
      [:looks, :dashboards].include?(k)
    end)
  end
  folder[:looks].each do |l|
    look = cat_look(l[:id])
    look = trim_look(look) if @options[:trim]
    write_file("Look_#{look[:id]}_#{look[:title]}.json", base, path) do |f|
      f.write JSON.pretty_generate(look)
    end
  end
  folder[:dashboards].each do |d|
    data = cat_dashboard(d[:id])
    data = trim_dashboard(data) if @options[:trim]
    write_file("Dashboard_#{data[:id]}_#{data[:title]}.json", base, path) do |f|
      f.write JSON.pretty_generate(data)
    end
  end
  folder_children = query_folder_children(folder_id)
  folder_children.each do |child_folder|
    process_folder(child_folder[:id], base, path)
  end
end