From 8fadaad817409ef6576bb360717d04d29a03aef1 Mon Sep 17 00:00:00 2001 From: Yuri Smirnov Date: Wed, 18 Dec 2024 16:16:24 +0300 Subject: [PATCH] use temp dir for generating templates --- lib/helmsnap/generate.rb | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/helmsnap/generate.rb b/lib/helmsnap/generate.rb index 1fa59e4..3878f89 100644 --- a/lib/helmsnap/generate.rb +++ b/lib/helmsnap/generate.rb @@ -10,8 +10,17 @@ def initialize(config, snapshots_path: nil) def call Helmsnap::SetupDependencies.call(config) - FileUtils.rmtree(snapshots_path) + Dir.mktmpdir do |tmpdir| + tmp_path = Pathname.new(tmpdir) + generate!(tmp_path) + end + end + + private + + attr_accessor :config, :snapshots_path + def generate!(tmp_path) config.envs.each do |env| run_cmd( "helmfile", @@ -19,21 +28,20 @@ def call env.name, "template", "--output-dir-template", - snapshots_path.join(env.name).join("{{ .Release.Name }}"), + tmp_path.join(env.name, "{{ .Release.Name }}"), "--skip-deps", ) end - snapshots_path.glob(["**/*yaml", "**/*.yml"]).each do |path| + tmp_path.glob(["**/*yaml", "**/*.yml"]).each do |path| content = path.read content.gsub!(/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d+/, "2022-01-01 00:00:00.000") content.gsub!(/\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d/, "2022-01-01-00-00-00") content.gsub!(/\d\d\d\d-\d\d-\d\d-\d\d-\d\d/, "2022-01-01-00-00") path.write(content) end - end - - private - attr_accessor :config, :snapshots_path + FileUtils.rmtree(snapshots_path) + FileUtils.move(tmp_path, snapshots_path) + end end