Class: AnalyticsOps::Configuration::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/analytics_ops/configuration/writer.rb,
sig/analytics_ops.rbs

Overview

Safely creates the smallest valid configuration after interactive setup.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#write_minimal(path, profile:, property_id:) ⇒ Result

Parameters:

  • path (String)
  • profile: (String, Symbol)
  • property_id: (String)

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/analytics_ops/configuration/writer.rb', line 44

def write_minimal(path, profile:, property_id:)
  state = validated_state(profile, property_id)
  expanded = File.expand_path(path)
  return existing_result(File.realpath(expanded), state) if File.exist?(expanded)

  FileUtils.mkdir_p(File.dirname(expanded))
  temporary = temporary_path(expanded)
  File.open(temporary, File::WRONLY | File::CREAT | File::EXCL, 0o644) do |file|
    file.write(document(state))
    file.flush
    file.fsync
  end
  begin
    File.link(temporary, expanded)
  rescue Errno::EEXIST
    raise ConfigurationError, "Configuration #{Redaction.message(path)} was created by another process"
  end
  Result.new(path: expanded, created: true)
rescue AnalyticsOps::Error
  raise
rescue SystemCallError => error
  raise ConfigurationError,
        "Cannot write configuration #{Redaction.message(path)}: #{Redaction.message(error.message)}"
ensure
  File.unlink(temporary) if temporary && File.exist?(temporary)
end