Skip to content

Instantly share code, notes, and snippets.

@caseylang
Created February 19, 2015 01:18
Show Gist options
  • Save caseylang/b57eee59f3d7f1fd429c to your computer and use it in GitHub Desktop.
Save caseylang/b57eee59f3d7f1fd429c to your computer and use it in GitHub Desktop.
Copy Config Files
#!/usr/bin/env ruby
require 'find'
require 'fileutils'
def main
example_files = gather_example_files(ARGV[1])
case ARGV[0]
when 'copy' then copy_and_rename(example_files)
when 'validate' then validate_config_files_presence(example_files)
else
raise "Unknown option\n
Usage: copy_config_files <command> <path>\n
Commands:
copy: finds all files in the given path ending in '.example', copies them, and renames them, omitting the '.example'
validate: finds all files in the given path ending in '.example' and ensures that a copy exists with a name that lacks '.example'"
end
end
def gather_example_files(dir)
config_files = []
Find.find("#{dir}") do |path|
config_files << path if path =~ /.*\.example$/
end
config_files
end
def copy_and_rename(files)
files.each do |src_file|
dest_file = strip_example(src_file)
FileUtils.copy(src_file, dest_file) unless File.exists?(dest_file)
end
end
def validate_config_files_presence(files)
valid = true
files.each do |org_file|
new_file = strip_example(org_file)
valid = false unless File.exists?(new_file)
end
exit false unless valid
end
def strip_example(file)
file.sub(/\.example$/, '')
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment