From f5d1620ca3d92dd5181e72caa07145fce2749d90 Mon Sep 17 00:00:00 2001 From: Fran Zekan Date: Thu, 22 Jun 2023 18:31:33 +0200 Subject: [PATCH 1/4] Add -t argument to envify --- lib/mrsk/cli/main.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index bf0d8917..898c3dfd 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -165,15 +165,17 @@ def init end desc "envify", "Create .env by evaluating .env.erb (or .env.staging.erb -> .env.staging when using -d staging)" + option :template, aliases: "-t", type: :string, default: ".env.erb", desc: "Template to use" def envify if destination = options[:destination] - env_template_path = ".env.#{destination}.erb" + env_template_path = options[:template] || ".env.#{destination}.erb" env_path = ".env.#{destination}" else - env_template_path = ".env.erb" + env_template_path = options[:template] || ".env.erb" env_path = ".env" end + ENV["MRSK_DESTINATION"] = destination.to_s if destination File.write(env_path, ERB.new(File.read(env_template_path)).result, perm: 0600) end From 24320323c8773e2bc913f7fd4403b7045aef5924 Mon Sep 17 00:00:00 2001 From: Fran Zekan Date: Thu, 22 Jun 2023 18:39:24 +0200 Subject: [PATCH 2/4] Fix default value and add tests --- lib/mrsk/cli/main.rb | 2 +- test/cli/main_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index 898c3dfd..18e3c724 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -165,7 +165,7 @@ def init end desc "envify", "Create .env by evaluating .env.erb (or .env.staging.erb -> .env.staging when using -d staging)" - option :template, aliases: "-t", type: :string, default: ".env.erb", desc: "Template to use" + option :template, aliases: "-t", type: :string, desc: "Template to use" def envify if destination = options[:destination] env_template_path = options[:template] || ".env.#{destination}.erb" diff --git a/test/cli/main_test.rb b/test/cli/main_test.rb index 728ce24e..d5569192 100644 --- a/test/cli/main_test.rb +++ b/test/cli/main_test.rb @@ -339,6 +339,20 @@ class CliMainTest < CliTestCase run_command("envify", "-d", "staging") end + test "envify with custom template file" do + File.expects(:read).with(".env.template.erb").returns("HELLO=<%= 'world' %>") + File.expects(:write).with(".env.staging", "HELLO=world", perm: 0600) + + run_command("envify", "-t", ".env.template.erb", "-d", "staging") + end + + test "envify with custom template file and destination" do + File.expects(:read).with(".env.template.erb").returns("HELLO=<%= ENV['MRSK_DESTINATION'] %>") + File.expects(:write).with(".env.staging", "HELLO=staging", perm: 0600) + + run_command("envify", "-t", ".env.template.erb", "-d", "staging") + end + test "remove with confirmation" do run_command("remove", "-y", config_file: "deploy_with_accessories").tap do |output| assert_match /docker container stop traefik/, output From 78be2a8e010ba660f6d95c7360a6ec2a9a9ad61e Mon Sep 17 00:00:00 2001 From: Fran Zekan Date: Mon, 26 Jun 2023 08:33:10 +0200 Subject: [PATCH 3/4] Fix wrong logical assertion --- test/cli/main_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/cli/main_test.rb b/test/cli/main_test.rb index d5569192..2d8ae0b9 100644 --- a/test/cli/main_test.rb +++ b/test/cli/main_test.rb @@ -341,9 +341,9 @@ class CliMainTest < CliTestCase test "envify with custom template file" do File.expects(:read).with(".env.template.erb").returns("HELLO=<%= 'world' %>") - File.expects(:write).with(".env.staging", "HELLO=world", perm: 0600) + File.expects(:write).with(".env", "HELLO=world", perm: 0600) - run_command("envify", "-t", ".env.template.erb", "-d", "staging") + run_command("envify", "-t", ".env.template.erb") end test "envify with custom template file and destination" do From 33eb19c361703b83e4a3fae7101265172d12944e Mon Sep 17 00:00:00 2001 From: Fran Zekan Date: Thu, 13 Jul 2023 13:29:15 +0200 Subject: [PATCH 4/4] Expose `MRSK_DESTINATION` when loading deploy.yml --- .gitignore | 1 + lib/mrsk/configuration.rb | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 6100dc73..9c0078dd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ coverage/* .DS_Store gemfiles/*.lock +.idea/ diff --git a/lib/mrsk/configuration.rb b/lib/mrsk/configuration.rb index ee6dce05..cd36f71e 100644 --- a/lib/mrsk/configuration.rb +++ b/lib/mrsk/configuration.rb @@ -14,18 +14,19 @@ class Mrsk::Configuration class << self def create_from(config_file:, destination: nil, version: nil) - raw_config = load_config_files(config_file, *destination_config_file(config_file, destination)) + raw_config = load_config_files(config_file, *destination_config_file(config_file, destination), destination: destination) new raw_config, destination: destination, version: version end private - def load_config_files(*files) - files.inject({}) { |config, file| config.deep_merge! load_config_file(file) } + def load_config_files(*files, destination: nil) + files.inject({}) { |config, file| config.deep_merge! load_config_file(file, destination) } end - def load_config_file(file) + def load_config_file(file, destination) if file.exist? + ENV["MRSK_DESTINATION"] = destination.to_s if destination YAML.load(ERB.new(IO.read(file)).result).symbolize_keys else raise "Configuration file not found in #{file}"