Class: Gitlab::Database::Sos::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/sos/output.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode:) ⇒ Output

Returns a new instance of Output.



14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/database/sos/output.rb', line 14

def initialize(path, mode:)
  @mode = mode
  if mode == :zip
    @zip = Zip::File.open(path, create: true) # rubocop:disable Performance/Rubyzip -- opening a file so no performance issues
  elsif mode == :directory # Used in testing, it's a lot easier to inspect a directory than a zip file
    @dir = path
  else
    raise "mode must be one of :zip, :directory"
  end
end

Class Method Details

.writing(path, mode:) ⇒ Object



7
8
9
10
11
12
# File 'lib/gitlab/database/sos/output.rb', line 7

def self.writing(path, mode:)
  output = new(path, mode: mode)
  yield output
ensure
  output.finish
end

Instance Method Details

#finishObject



25
26
27
# File 'lib/gitlab/database/sos/output.rb', line 25

def finish
  @zip.close if @mode == :zip
end

#write_file(relative_path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/database/sos/output.rb', line 29

def write_file(relative_path)
  if @mode == :zip
    @zip.get_output_stream(relative_path) do |f|
      yield f
    end
  else
    abs_path = File.join(@dir, relative_path)
    FileUtils.mkdir_p(File.dirname(abs_path))
    File.open(abs_path, 'w+') do |f|
      yield f
    end
  end
end