#!/usr/bin/env ruby require 'yaml' require 'slop' require 'gitdis' opts = Slop.parse { |o| o.banner = "USAGE: gitdis path/to/config.yaml [options]" o.separator ' Environment selection' o.string '-e', '--environment', 'select within YAML[environments]' o.separator ' Redis overrides' o.string '-H', '--redis-host', 'string' o.integer '-p', '--redis-port', 'number' o.integer '-d', '--redis-db', 'number' o.separator ' Git repo overrides' o.string '-r', '--git-repo', 'path/to/repo_dir' o.string '-b', '--git-branch', 'e.g. master' o.separator ' Other options' o.bool '-D', '--dump', 'Just dump Redis contents per YAML keymap' o.on '-h', '--help' do puts o exit end } unless(yaml_file = opts.arguments.shift) puts opts puts "path/to/config.yaml is required" exit 1 end # load the default (required) config yaml = YAML.load_file(yaml_file) config = yaml.fetch('default') # apply any env-specific settings if opts[:environment] env = opts[:environment].downcase unless yaml.key?(env) warn "no environment #{env} found in #{yaml_file}" exit 1 end config.merge!(yaml[env]) end # make sure we have a keymap keymap = config.fetch('keymap') raise "keymap should be a hash/map" unless keymap.is_a?(Hash) # update config with any settings from opts # create redis_options redis_options = {} %w[git-repo git-branch redis-host redis-port redis-db].each { |item| opt_value = opts[item.to_sym] config[item] = opt_value if opt_value if item.match /^redis-/ # set :host, :port, :db cfg_value = config[item] if cfg_value # "redis-foo-bar" => :foo_bar ropt = item.split('-').drop(1).join('_').to_sym redis_options[ropt] = cfg_value end end puts [item, config[item]].join(': ') if config[item] } puts "Redis options: #{redis_options}" # do things'n'stuff if opts.dump? GitDis.dump(keymap.keys, redis_options) else gd = GitDis.new config.fetch('git-repo') gd.git_pull config.fetch('git-branch') keymap.each { |key, fileglob| result = gd.update(key, fileglob) case result when nil puts "#{fileglob} not found" when false puts "#{fileglob} unchanged" else ver, md5 = *result puts "#{fileglob} updated:" puts "\tVersion: #{ver} (#{md5})" end } end