9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/rails_agents/credentials_writer.rb', line 9
def write!(api_key:, project_id: nil)
path = Rails.root.join("config/rails_agents_credentials.yml")
payload = {
"api_key" => api_key,
"project_id" => project_id
}.compact
File.write(path, payload.to_yaml)
env_path = Rails.root.join(".env")
lines = {
"RAILS_AGENTS_API_KEY" => api_key,
"RAILS_AGENTS_PROJECT_ID" => project_id
}.compact
if env_path.exist?
existing = File.read(env_path)
lines.each do |key, value|
if existing.match?(/^#{Regexp.escape(key)}=/)
existing = existing.gsub(/^#{Regexp.escape(key)}=.*$/, "#{key}=#{value}")
else
existing = existing.sub(/\n*\z/, "\n") + "#{key}=#{value}\n"
end
end
File.write(env_path, existing)
else
File.write(env_path, lines.map { |k, v| "#{k}=#{v}" }.join("\n") + "\n")
end
end
|