Skip to content

Instantly share code, notes, and snippets.

@tongueroo
Created October 19, 2023 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tongueroo/95a8e3c6d6d2e21c441cf7668e2b9117 to your computer and use it in GitHub Desktop.
Save tongueroo/95a8e3c6d6d2e21c441cf7668e2b9117 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "fileutils"
class Mover
def initialize
@expr = "app/stacks/*/tfvars"
end
def run
if empty?
puts "No existing tfvars require moving in #{@expr}. Exiting."
exit 0
end
puts <<~EOL
Will move tfvars to new recommended location.
Will move app/stacks/*/tfvars to config/stacks/*/.tfvars
Any existing config/stacks/*/tfvars folders will be removed and replaced.
It's recommended that you commit and save your code before running this script.
EOL
move_all(preview: true)
sure?
move_all(preview: false)
end
def empty?
Dir.glob(@expr).empty?
end
def sure?
print "\nAre you sure you want to move all tfvars files? [y/N] "
answer = gets.chomp
puts
unless answer =~ /^y/i
puts "Exiting. Did not move files."
exit 1
end
end
def move_all(preview:)
Dir.glob(@expr).each do |path|
move path, preview: preview
end
end
def move(src, preview:)
dest = src.sub("app/stacks", "config/stacks")
header_message(preview)
puts " #{src} => #{dest}"
unless preview
FileUtils.rm_rf(dest)
FileUtils.mkdir_p(File.dirname(dest))
FileUtils.mv(src, dest)
end
end
@@preview_shown = false
@@moving_shown = false
def header_message(preview)
if preview
unless @@preview_shown
puts "Previewing move of tfvars files:\n\n"
@@preview_shown = true
end
else
unless @@moving_shown
puts "Moving tfvars files:\n\n"
@@moving_shown = true
end
end
end
end
if __FILE__ == $0
Mover.new.run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment