Created
September 15, 2015 23:59
-
-
Save parabuzzle/28dea88cd5a23f6de315 to your computer and use it in GitHub Desktop.
I built this... I'm not proud... but yea, it works....
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
#!/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 |
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
to use in bash... just do
eval $(parse_yaml_for_bash <file>)