class Gzr::Commands::Space::Export

Public Class Methods

new(space_id, options) click to toggle source
Calls superclass method Gzr::Command::new
# File lib/gzr/commands/space/export.rb, line 41
def initialize(space_id, options)
  super()
  @space_id = space_id
  @options = options
end

Public Instance Methods

execute(input: $stdin, output: $stdout) click to toggle source
# File lib/gzr/commands/space/export.rb, line 47
def execute(input: $stdin, output: $stdout)
  say_warning("options: #{@options.inspect}") if @options[:debug]
  with_session("3.1") 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_space(@space_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_space(@space_id, z)
        ensure
          z.close
        end
      end
    else
      process_space(@space_id, @options[:dir])
    end
  end
end
process_space(space_id, base, rel_path = nil) click to toggle source
# File lib/gzr/commands/space/export.rb, line 86
def process_space(space_id, base, rel_path = nil)
  space = query_space(space_id).to_attrs
  name = space[:name]
  name = "nil (#{space_id})" if name.nil?
  path = Pathname.new(name.gsub('/',"\u{2215}"))
  path = rel_path + path if rel_path

  write_file("Space_#{space[:id]}_#{name}.json", base, path) do |f|
    f.write JSON.pretty_generate(space.reject do |k,v|
      [:looks, :dashboards].include?(k)
    end)
  end
  space[:looks].each do |l|
    look = query_look(l[:id]).to_attrs
    find_vis_config_reference(look) do |vis_config|
      find_color_palette_reference(vis_config) do |o,default_colors|
        rewrite_color_palette!(o,default_colors)
      end
    end
    write_file("Look_#{look[:id]}_#{look[:title]}.json", base, path) do |f|
      f.write JSON.pretty_generate(look)
    end
  end
  space[:dashboards].each do |d|
    data = query_dashboard(d[:id]).to_attrs()
    data[:dashboard_elements].each_index do |i|
      element = data[:dashboard_elements][i]
      find_vis_config_reference(element) do |vis_config|
        find_color_palette_reference(vis_config) do |o,default_colors|
          rewrite_color_palette!(o,default_colors)
        end
      end
      merge_result = merge_query(element[:merge_result_id])&.to_attrs() if element[:merge_result_id]
      if merge_result
        merge_result[:source_queries].each_index do |j|
          source_query = merge_result[:source_queries][j]
          merge_result[:source_queries][j][:query] = query(source_query[:query_id]).to_attrs()
        end
        find_vis_config_reference(merge_result) do |vis_config|
          find_color_palette_reference(vis_config) do |o,default_colors|
            rewrite_color_palette!(o,default_colors)
          end
        end
        data[:dashboard_elements][i][:merge_result] = merge_result
      end
    end
    write_file("Dashboard_#{data[:id]}_#{data[:title]}.json", base, path) do |f|
      f.write JSON.pretty_generate(data)
    end
  end
  space_children = query_space_children(space_id)
  space_children.each do |child_space|
    process_space(child_space[:id], base, path)
  end
end