-
-
Save failshell/92593fd622c94d595fd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'colorize' | |
require 'mixlib/shellout' | |
require_relative 'cli' | |
module Farm | |
class Migrator | |
def initialize(envs) | |
@envs = envs | |
run | |
end | |
def run | |
fail "The 'migrate' option requires 2 arguments.".red if ARGV[1].nil? || ARGV[2].nil? | |
fail 'One of the provided environments is invalid.'.red unless @envs.include?(ARGV[1]) && @envs.include?(ARGV[2]) | |
new_files?(ARGV[1], ARGV[2]) | |
compare_files(ARGV[1], ARGV[2]) | |
end | |
def compare_files(src, dst) | |
Dir.glob("env.#{src}/*.tf").each do |file| | |
file = file.split('/')[1] | |
next unless File.exist?("env.#{dst}/#{file}") | |
next if file == 'env.tf' | |
f1 = parse("env.#{src}/#{file}") | |
f2 = parse("env.#{dst}/#{file}") | |
# returns true if there's no differences (diff returns 0) | |
next if system("diff env.#{src}/#{file} env.#{dst}/#{file} > /dev/null") | |
File.open("env.#{dst}/#{file}", "w+") do |f| | |
f1.concat(f2).uniq.each { |e| f.puts(e) } | |
end | |
puts "The content of 'env.#{src}/#{file}' and 'env.#{dst}/#{file}' has been merged.".light_blue | |
end | |
end | |
def copy_files(files, src, dst) | |
files.each do |file| | |
FileUtils.cp("env.#{src}/#{file}", "env.#{dst}/#{file}", :verbose => true) | |
end | |
end | |
def new_files?(src, dst) # rubocop:disable Metrics/MethodLength | |
src_files = [] | |
Dir.glob("env.#{src}/*").each do |e| | |
src_files << e.split('/')[1] | |
end | |
dst_files = [] | |
Dir.glob("env.#{dst}/*").each do |e| | |
dst_files << e.split('/')[1] | |
end | |
diff = src_files - dst_files | |
return false if diff.empty? | |
puts "The following files are present in 'env.#{src}' and missing in 'env.#{dst}': #{diff}".yellow | |
print 'Do you want to copy the missing files? (yes/no): '.light_blue | |
a = $stdin.gets.chomp | |
a == 'yes' ? copy_files(diff, src, dst) : fail('Aborting.') | |
end | |
def parse(file) | |
data = [] | |
File.foreach(file).slice_after(/^}$/).each do |slice| | |
data << slice | |
end | |
data | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment