Skip to content

Commit

Permalink
fix(figaro_yml-losing-stage-local): Update configs helper to retain o…
Browse files Browse the repository at this point in the history
…ther stages locally

- Update configs helper to return the rest of `application.yml` as the third element.
- Implement changes to ensure that the other stages are kept locally.
- Bump Capistrano::Ops version to 1.0.4 to mark the introduction of the above enhancements.

This set of changes aims to maintain the integrity of other stages in the local `application.yml` configuration.

This commit message was written by VSCode Copilot.
  • Loading branch information
floriancrusius committed Aug 13, 2024
1 parent 7f8b38d commit 9de08ce
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
27 changes: 13 additions & 14 deletions lib/capistrano/ops/figaro_yml/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,10 @@ def cleanup_remote_backups
end
end

def local_figaro_yml(env)
def local_figaro_yml(_env)
@local_figaro_yml ||= YAML.load(ERB.new(File.read(figaro_yml_local_path)).result)
local_figaro = {}
deployment_env = fetch(:rails_env, env).to_s

@local_figaro_yml.each do |key, value|
if key == env
local_figaro[deployment_env] = @local_figaro_yml[key]
elsif !value.is_a?(Hash)
local_figaro[key] = @local_figaro_yml[key]
end
end

local_figaro
@local_figaro_yml || {}
end

def local_yaml
Expand All @@ -85,9 +75,18 @@ def figaro_yml_content
end

def configs(yaml, env)
stage_yml = yaml[env.to_s]&.sort.to_h
env_str = env.to_s
stage_yml = yaml[env_str]&.sort.to_h
global_yml = remove_nested(yaml)&.sort.to_h
[global_yml, stage_yml]

other_stages_yml = stages.each_with_object({}) do |f, hash|
f_str = f.to_s
next if f_str == env_str

hash[f_str] = yaml[f_str]&.sort.to_h
end.compact

[global_yml, stage_yml, other_stages_yml]
end

def remove_nested(hash)
Expand Down
4 changes: 2 additions & 2 deletions lib/capistrano/ops/figaro_yml/tasks/compare.rake
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace :figaro_yml do
local = local_figaro_yml(figaro_yml_env)

# Split into stage-specific and global configurations
local_global_env, local_stage_env = configs(local, figaro_yml_env)
local_global_env, local_stage_env, _local_rest = configs(local, figaro_yml_env)

on release_roles :all do
# Read and parse remote application.yml
remote = YAML.safe_load(capture("cat #{figaro_yml_remote_path}"))
remote_global_env, remote_stage_env = configs(remote, figaro_yml_env)
remote_global_env, remote_stage_env, _remote_rest = configs(remote, figaro_yml_env)

# Compare hashes and handle nil results with empty hashes
differences_global = compare_hashes(local_global_env, remote_global_env)
Expand Down
8 changes: 4 additions & 4 deletions lib/capistrano/ops/figaro_yml/tasks/get.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace :figaro_yml do
invoke 'figaro_yml:create_local'
else
local_yml = local_figaro_yml(figaro_yml_env)
local_global, local_stage = configs(local_yml, figaro_yml_env)
local_global, local_stage, local_rest = configs(local_yml, figaro_yml_env)
on release_roles :all do
remote = capture "cat #{figaro_yml_remote_path}"
remote_yml = YAML.safe_load(remote).sort.to_h
remote_global, remote_stage = configs(remote_yml, figaro_yml_env)
remote_global, remote_stage, _remote_rest = configs(remote_yml, figaro_yml_env)
differences_global = compare_hashes(remote_global, local_global || {})
differences_stage = compare_hashes(remote_stage, local_stage || {})

Expand All @@ -29,10 +29,10 @@ namespace :figaro_yml do
global_overwrite = ask_to_overwrite('Overwrite local application.yml globals') if differences_global
puts 'Nothing written to local application.yml' unless stage_overwrite || global_overwrite
exit unless stage_overwrite || global_overwrite

# compose new yml

composed_yml = {}
composed_yml.merge!(local_yml) # local yml is always included to avoid losing any data
composed_yml.merge!(local_rest) # local yml is always included to avoid losing any data
composed_yml.merge!(local_global) unless global_overwrite
composed_yml.merge!(remote_global) if global_overwrite
composed_yml[figaro_yml_env.to_s] = stage_overwrite ? remote_stage : local_stage
Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/ops/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Capistrano
module Ops
VERSION = '1.0.3'
VERSION = '1.0.4'
end
end

0 comments on commit 9de08ce

Please sign in to comment.