Skip to content

Instantly share code, notes, and snippets.

@s-shin
Created May 18, 2017 06:52
Show Gist options
  • Save s-shin/30595f765afb47240a3ea3739b7f6ee8 to your computer and use it in GitHub Desktop.
Save s-shin/30595f765afb47240a3ea3739b7f6ee8 to your computer and use it in GitHub Desktop.
Example: `echo "$(eval "$(echo "{ foo: { bar: [fizz, buzz] } }" | yml2sh)"; echo "$(foo.bar)")"` => fizz\tbuzz
#!/usr/bin/env ruby
require 'yaml'
def puts_sh_kv(key, value)
escaped_value = value.gsub(/"/, '\\"')
puts %(function #{key}() { echo "#{escaped_value}"; })
end
def print_sh(data, key_prefix: '')
data.each do |key, value|
key_path = "#{key_prefix}#{key}"
case value
when Hash
print_sh value, key_prefix: key_path + '.'
when Array
puts_sh_kv key_path, value.join("\t")
else
puts_sh_kv key_path, value
end
end
end
print_sh YAML.load(ARGF.read)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment