Created
May 18, 2017 06:52
-
-
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
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 | |
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