Module: Capistrano::Ops::FigaroYml::Helpers

Defined in:
lib/capistrano/ops/figaro_yml/helpers.rb

Instance Method Summary collapse

Instance Method Details

#ask_to_overwrite(question) ⇒ Object

selection helpers



115
116
117
118
119
120
121
122
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 115

def ask_to_overwrite(question)
  answer = ''
  until %w[y n].include?(answer)
    print "#{question}? (y/N): "
    answer = $stdin.gets.strip.downcase
  end
  answer == 'y'
end

#backup_regexObject



30
31
32
33
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 30

def backup_regex
  # filename we are looking for "#{figaro_yml_remote_path.basename}-yyyy-mm-dd-HH-MM-SS.bak"
  "#{figaro_yml_remote_path.basename}-[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}.bak"
end

#check_config_present_errorObject



150
151
152
153
154
155
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 150

def check_config_present_error
  puts
  puts "Error - '#{figaro_yml_env}' config not present in '#{fetch(:figaro_yml_local_path)}'."
  puts 'Please populate it.'
  puts
end

#check_figaro_file_exists_errorObject



157
158
159
160
161
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 157

def check_figaro_file_exists_error
  puts
  puts "Error - '#{fetch(:figaro_yml_local_path)}' file does not exists, and it's required."
  puts
end

#check_git_tracking_errorObject

error helpers



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 138

def check_git_tracking_error
  puts
  puts "Error - please remove '#{fetch(:figaro_yml_local_path)}' from git:"
  puts
  puts "    $ git rm --cached #{fetch(:figaro_yml_local_path)}"
  puts
  puts 'and gitignore it:'
  puts
  puts "    $ echo '#{fetch(:figaro_yml_local_path)}' >> .gitignore"
  puts
end

#cleanup_remote_backupsObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 46

def cleanup_remote_backups
  number_of_backups = count_remote_files
  return unless number_of_backups > 5

  diff = number_of_backups - 5
  # remove older backups and keep the latest 5
  command = "ls -1t #{figaro_yml_remote_path.parent} | grep -E '#{backup_regex}' | tail -n #{diff}"

  capture(command).split("\n").each do |file|
    execute "rm #{figaro_yml_remote_path.parent.join(file)}"
  end
end

#compare_hashes(hash1, hash2) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 104

def compare_hashes(hash1, hash2)
  all_keys = hash1.keys | hash2.keys # Union of all keys from both hashes
  all_keys.each_with_object({}) do |key, changes_hash|
    old_value = hash2[key].nil? ? 'nil' : hash2[key].to_s
    new_value = hash1[key].nil? ? 'nil' : hash1[key].to_s

    changes_hash[key] = { old: old_value, new: new_value } if old_value != new_value
  end.tap { |changes| return changes.empty? ? nil : changes }
end

#configs(yaml, env) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 77

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

  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

#count_remote_filesObject



35
36
37
38
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 35

def count_remote_files
  command = "ls -1 #{figaro_yml_remote_path.parent} | grep -E '#{backup_regex}' | wc -l"
  capture(command).to_i
end

#create_remote_backupObject



40
41
42
43
44
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 40

def create_remote_backup
  backup_file = "#{figaro_yml_remote_path.basename}-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.bak"
  # puts "cp #{figaro_yml_remote_path} #{figaro_yml_remote_path.parent.join(backup_file)}"
  execute :cp, figaro_yml_remote_path, figaro_yml_remote_path.parent.join(backup_file)
end

#figaro_yml_contentObject



73
74
75
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 73

def figaro_yml_content
  local_figaro_yml(figaro_yml_env).to_yaml
end

#figaro_yml_envObject



69
70
71
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 69

def figaro_yml_env
  fetch(:figaro_yml_env).to_s
end

#latest_remote_backupObject



25
26
27
28
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 25

def latest_remote_backup
  command = "ls -1t #{figaro_yml_remote_path.parent} | grep -E '#{backup_regex}' | head -n 1"
  capture(command).strip
end

#local_figaro_yml(_env) ⇒ Object



59
60
61
62
63
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 59

def local_figaro_yml(_env)
  @local_figaro_yml ||= YAML.load(ERB.new(File.read(figaro_yml_local_path)).result)

  @local_figaro_yml || {}
end

#local_yamlObject



65
66
67
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 65

def local_yaml
  YAML.safe_load(File.read(figaro_yml_local_path)) || {}
end

info helpers



126
127
128
129
130
131
132
133
134
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 126

def print_changes(changes, message)
  return unless changes

  puts "#{message}:\n\n"
  changes.each do |key, diff|
    puts "#{key}: #{diff[:old]} => #{diff[:new]}"
  end
  puts "\n"
end

#remote_backup_exists?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 14

def remote_backup_exists?
  count_remote_files.positive?
end

#remote_file_exists?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 10

def remote_file_exists?
  test("[ -f #{figaro_yml_remote_path} ]")
end

#remove_nested(hash) ⇒ Object



92
93
94
95
96
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 92

def remove_nested(hash)
  hash.each_with_object({}) do |(key, value), new_hash|
    new_hash[key] = value unless value.is_a?(Hash)
  end
end

#rollback_remote_backupObject



18
19
20
21
22
23
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 18

def rollback_remote_backup
  latest_backup = latest_remote_backup
  puts "mv #{figaro_yml_remote_path.parent.join(latest_backup)} #{figaro_yml_remote_path}"
  execute :mv, figaro_yml_remote_path.parent.join(latest_backup), figaro_yml_remote_path
  execute :ls, '-l', figaro_yml_remote_path.parent
end

#sort_with_nested(hash) ⇒ Object



98
99
100
101
102
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 98

def sort_with_nested(hash)
  hash.each_with_object({}) do |(key, value), new_hash|
    new_hash[key] = value.is_a?(Hash) ? sort_with_nested(value) : value
  end.sort.to_h
end

#write_combined_yaml(yamls_combined) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 170

def write_combined_yaml(yamls_combined)
  if yamls_combined.empty?
    info 'No data to write.'
  else
    # write to new file
    info 'writing to config/application.yml'
    write_to_file(figaro_yml_local_path, yamls_combined.to_yaml)
  end
end

#write_to_file(file, content) ⇒ Object

file helpers



164
165
166
167
168
# File 'lib/capistrano/ops/figaro_yml/helpers.rb', line 164

def write_to_file(file, content)
  File.open(file, 'w') do |f|
    f.write(content)
  end
end