#!/usr/bin/env ruby require 'rubygems' ; $: << File.dirname(__FILE__)+'/../lib' require 'configliere' require 'configliere/client' require 'configliere/client/commands' Log = Logger.new(STDERR) unless defined?(Log) class ConfigliereScript < Configliere::CommandClient def usage %Q{Client for the configliere gem: manipulate configuration and passwords for automated scripts usage: #{File.basename($0)} command handle [...--option=val...] where command: One of: #{COMMANDS.keys[0..-2].join(', ')} or #{COMMANDS.keys.last} handle: Name of the configliere group (within the configliere_file) or path to a configliere YAML file. Configuration taken from #{configliere_file} by default.} end def process_options! *args super *args self.command = options[:_rest].shift.to_sym rescue nil self.handle = options[:_rest].shift.to_sym rescue nil end # =========================================================================== # # Commands # COMMANDS[:fix] = "encrypt the param" def fix Log.info "Fixing stored info for #{handle}" store.fix!(handle, option_or_ask(:key)) end COMMANDS[:encrypt] = "synonym for fix. Params are stored encrypted by default" def encrypt() fix end COMMANDS[:decrypt] = "Store the param as decrypted back into the file. Can be undone with 'fix'." def decrypt Log.info "Storing info for #{handle} in **DECRYPTED** form." param = get(handle) store.put_decrypted!(handle, param) end COMMANDS[:list] = "Show all params in the configliere file." def list puts "List of param names: #{store.handles.inspect}" end COMMANDS[:delete] = "Permanently deletes the param" def delete Log.info "Permanently deleting stored info for #{handle}. O, I die, Horatio." store.delete! handle, options[:key] end COMMANDS[:set] = "sets values using remaining arguments from the command line. eg #{File.basename($0)} set my_program --username=bob --password=frank" def set param = get(handle) param.merge! external_options store.put handle, param store.save! Log.info "Stored configuration for #{handle}: #{param}" end COMMANDS[:change_key] = "set a new key and/or new key options. Specify the old key as usual with --key='...' and the new one with --new_key='...'" def change_key param = get(handle) new_key = option_or_ask(:new_key) new_hsh = param.to_decrypted new_param = Configliere::Param.new(new_key, new_hsh) store.put! handle, new_param Log.info "Changed param key for #{handle}: #{new_param}" end COMMANDS[:show] = "print the decrypted information" def show param = get(handle) puts "Stored info for #{handle}:\n #{param.to_s}" end end ConfigliereScript.new.run