Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@parabuzzle
Created September 15, 2015 23:59
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 parabuzzle/28dea88cd5a23f6de315 to your computer and use it in GitHub Desktop.
Save parabuzzle/28dea88cd5a23f6de315 to your computer and use it in GitHub Desktop.
I built this... I'm not proud... but yea, it works....
#!/usr/bin/env ruby
#
# Parses a given yaml file and returns prefix_key=value
# Usage: parse_yaml_for_bash <yaml_file> <prefix>
require 'yaml'
unless ARGV[0]
puts "Usage: parse_yaml_for_bash <yaml_file> <optional prefix>"
exit 1
end
@prefix = ARGV[1]
data = YAML.load_file(ARGV[0])
def tree(hash, path = [])
hash.each do |k,v|
new_path = path + [k]
if v.is_a?(Hash)
puts "#{@prefix}_#{new_path.join('_').gsub('-', '_')}_keys_count=\"#{v.keys.count}\""
tree(v, new_path)
else
leaf(v, new_path)
end
end
end
def leaf(v, path = [])
if v.is_a?(Array)
puts "#{@prefix}_#{path.join('_').gsub('-', '_')}_keys_count=\"#{v.length}\""
puts "#{@prefix}_#{path.join('_').gsub('-', '_')}=\"#{v.join(' ')}\""
else
puts "#{@prefix}_#{path.join('_').gsub('-', '_')}=\"#{v}\""
end
end
tree data
@parabuzzle
Copy link
Author

to use in bash... just do eval $(parse_yaml_for_bash <file>)

@busbey
Copy link

busbey commented Sep 16, 2015

mind putting a license on this? (personally I'd recommend either MIT or ASLv2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment