Skip to content

Instantly share code, notes, and snippets.

@ang3lkar
Last active May 23, 2017 09:28
Show Gist options
  • Save ang3lkar/d7f667cab38b572417de6d3b7527c41b to your computer and use it in GitHub Desktop.
Save ang3lkar/d7f667cab38b572417de6d3b7527c41b to your computer and use it in GitHub Desktop.
Adds missing variables from .env_sample to .env when switching branches
#!/usr/bin/env ruby
begin
env = File.read(File.expand_path('~/projects/workable/.env'))
env_sample = File.read(File.expand_path('~/projects/workable/.env_sample'))
env_keys = env
.split("\n")
.reject { |e| e.start_with?('#') }
.map { |e| e.split('=')[0] }
sample_map = env_sample
.split("\n")
.reject { |e| e.start_with?('#') }
.map { |e| arr = e.split('='); [arr[0].strip, (arr[1] || '').strip] }
sample_hash = Hash[*sample_map.flatten]
sample_keys = sample_hash.map { |arr| arr[0] }
missing_keys = sample_keys - env_keys
puts 'Found new keys in env_sample' if missing_keys.any?
File.open(File.expand_path('~/projects/workable/.env'), 'a') do |file|
missing_keys.each do |key|
puts "Adding #{key}"
file.write "#{key}=#{sample_hash[key]}\n"
end
end
`sort -o ~/projects/workable/.env ~/projects/workable/.env`
rescue => e
puts e.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment