exe/etcd-env in etcd-env-0.0.1 vs exe/etcd-env in etcd-env-0.0.2

- old
+ new

@@ -3,22 +3,21 @@ require 'json' require 'net/http' def usage - puts "Usage: #{$PROGRAM_NAME} <etcd key> [ <etcd key> ... ] -- <command>" + puts "Usage: #{$PROGRAM_NAME} <etcd key> [ <etcd key> ... --] <command>" end def env_vars(keys) - var = Array.new - keys.each do |key| + result = keys.reduce({}) do |memo, key| response = fetch "http://#{etcd_host}:#{etcd_port}/v2/keys#{key}" - var << (JSON.parse(response)['node']['nodes'] || []) + vars = (JSON.parse(response)['node']['nodes'] || []) .map { |var| [var['key'].split('/').last, var['value']] } .to_h + memo.merge(vars) end - var end def etcd_host host = (ENV['ETCD_HOST'] || `ip route | awk '/default/ { print $3 }'`).strip rescue '' raise 'etcd host not found' if host.empty? @@ -41,14 +40,19 @@ else response.value end end -if __FILE__ == $PROGRAM_NAME - key_str, *commands = ARGV.join(" ").split(" -- ") - keys = key_str.split(" ") - if commands.empty? - usage - exit 1 - end - exec env_vars(keys).reduce({}, :merge), *commands +sep = ARGV.index '--' +if sep + keys = ARGV[0...sep] + commands = ARGV[(sep + 1)..-1] +else + keys = ARGV.take(1) + commands = ARGV.drop(1) end +if commands.empty? + usage + exit 1 +end +exec env_vars(keys), *commands +