Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add template argument to envify #352

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
coverage/*
.DS_Store
gemfiles/*.lock
.idea/
6 changes: 4 additions & 2 deletions lib/mrsk/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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

Expand Down
9 changes: 5 additions & 4 deletions lib/mrsk/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
14 changes: 14 additions & 0 deletions test/cli/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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", "HELLO=world", perm: 0600)

run_command("envify", "-t", ".env.template.erb")
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
Expand Down