#!/usr/bin/env ruby require "rubygems" require "bundler/setup" $:.unshift(File.dirname(__FILE__) + '/../lib/') require 'optparse' require 'irb' require 'irb/init' require 'irb/completion' require 'irb/ext/history' require 'pp' require 'pry' require 'fixie/config' require 'fixie' # IRB tries to overwrite our custom settings when it loads, so we have to hax0r # it to work. module IRB class << self alias :run_original_config :run_config def run_config run_original_config ::Fixie::Console.configure_irb end end end module Fixie module Context def describe_orgs OrgMetrics.org_stats(orgs) end def orgs Fixie::Organizations.new end def jobs Fixie::Jobs.new end def users Fixie::Users.new end def global_groups Fixie::GlobalGroups.new end def help puts(<<-HALP) ** ORGS ** * access with ORGS or ORGS * access a specific org: ORGS['orgname'] ** USERS ** * users.find('clownco-org-admin') * users.grep :clownco * users.usernames ** RAW SQL ACCESS** * sql[:users].select(:column, :column).where(:column => "condition").all ** irb Help ** irb_help HALP :COOL_STORY_BRO end def sql Opscode::Mappers.default_connection end def associate_user(username, orgname) unless user = users.find(username) raise ArgumentError, "No users matched '#{username}'" end unless org = ORGS[orgname] raise ArgumentError, "No orgs matched '#{orgname}'" end Fixie::Associator.associate_user(org, user) end def dissociate_user(username, orgname) unless user = users.find(username) raise ArgumentError, "No users matched '#{username}'" end unless org = ORGS[orgname] raise ArgumentError, "No orgs matched '#{orgname}'" end Fixie::Dissociator.dissociate_user(org, user) end end module Console extend self def irb_conf IRB.conf end def start # FUGLY HACK: irb gives us no other choice. irb_help = [:help, :irb_help, IRB::ExtendCommandBundle::NO_OVERRIDE] IRB::ExtendCommandBundle.instance_variable_get(:@ALIASES).delete(irb_help) # This has to come before IRB.setup b/c IRB.setup eats ARGV. configure # HACK: this duplicates the functions of IRB.start, but we have to do it # to get access to the main object before irb starts. ::IRB.setup(nil) irb = IRB::Irb.new setup(irb.context.main) irb_conf[:IRB_RC].call(irb.context) if irb_conf[:IRB_RC] irb_conf[:MAIN_CONTEXT] = irb.context trap("SIGINT") do irb.signal_handle end catch(:IRB_EXIT) do irb.eval_input end end def configure if ARGV.first && ARGV[0].chars.first != "-" && config_file = ARGV.shift config_file = File.expand_path(config_file) load_config_file = true end options = {} OptionParser.new do |opt| opt.banner = "Usage: orgmapper [config] [options]" opt.on('--couchdb-uri COUCH_URI', 'The URI of the couchdb server to connect to') { |v| options[:couchdb_uri] = v } opt.on("--database ACCOUNT_DATABASE", 'The name of the opscode account database') { |v| options[:database] = v } opt.on('--auth-uri AUTH_URI', "The URI of the opscode authz service") { |v| options[:auth_uri] =v } opt.on_tail('-h', '--help', 'Show this message') do puts opt puts "\nExample configuration file:\n\n" puts Fixie::Config.instance.example_config puts "\n" exit(1) end opt.parse!(ARGV) end pp :cli_opts => options if ENV["DEBUG"] if load_config_file puts "loading config: #{config_file}..." Kernel.load(config_file) end Fixie::Config.instance.merge_opts(options) puts Fixie::Config.instance.to_text end def setup(context) Object.send(:include, Mixlib::Authorization::AuthHelper) Object.const_set(:PLATFORM_BACKEND, Fixie::Backend.new(*Config.instance.to_ary)) Object.const_set(:ORGS, Fixie::Organizations.new) Object.const_set(:JOBS, Fixie::Jobs.new) Object.const_set(:USERS, Fixie::Users.new) Object.const_set(:GLOBAL_GROUPS, Fixie::GlobalGroups.new) context.extend(Context) end # Configure IRB how we like it. This needs to be hooked into IRB.run_config Object.send(:include, Mixlib::Authorization::AuthHelper) Object.const_set(:PLATFORM_BACKEND, Fixie::Backend.new(*Config.instance.to_ary)) Object.const_set(:ORGS, Fixie::Organizations.new) Object.const_set(:JOBS, Fixie::Jobs.new) Object.const_set(:USERS, Fixie::Users.new) Object.const_set(:GLOBAL_GROUPS, Fixie::GlobalGroups.new) context.extend(Context) end # Configure IRB how we like it. This needs to be hooked into IRB.run_config # because much of IRB's code is anachronistic def configure_irb IRB.init_config(__FILE__) IRB.conf[:HISTORY_FILE] = "~/.orgmapper_history" IRB.conf[:SAVE_HISTORY]=1000 IRB.conf[:USE_READLINE]=true IRB.conf[:PROMPT][:ORGMAPPER] = { # name of prompt mode :PROMPT_I => "orgmapper:%i > ", # normal prompt :PROMPT_S => "..%l ", # prompt for continuing strings :PROMPT_C => "... ", # prompt for continuing statement :RETURN => "%s\n" # format to return value } IRB.conf[:PROMPT_MODE] = :ORGMAPPER begin require 'wirble' Wirble.init Wirble.colorize rescue LoadError end end end end Fixie::Console.start