Tip: Click lines to highlight, hold ctrl/cmd to multi-select

http://codedumper.com/iloqe (18-Jan @ 09:10)

ripienaar.devco.net

Syntax Highlighted Code

  1. class MCollective::Application::Facts<MCollective::Application
  2.     description "Reports on usage for a specific fact"
  3.  
  4.     # this will be available in configuration[:fact]
  5.     option :script,
  6.         :description    => "Fact to report on",
  7.         :arguments      => ["--fact FACT", "-f FACT"]
  8.  
  9.     def post_option_parser(configuration)
  10.         configuration[:fact] = ARGV.shift if ARGV.size > 0
  11.     end
  12.  
  13.     def validate_configuration(configuration)
  14.         raise "Please specify a fact to report for" unless configuration.include?(:fact)
  15.     end
  16.  
  17.     def show_single_fact_report(fact, facts, verbose=false)
  18.         puts("Report for fact: #{fact}\n\n")
  19.  
  20.         facts.keys.sort.each do |k|
  21.             printf("        %-40sfound %d times\n", k, facts[k].size)
  22.  
  23.             if verbose
  24.                 puts
  25.  
  26.                 facts[k].sort.each do |f|
  27.                     puts("            #{f}")
  28.                 end
  29.  
  30.                 puts
  31.             end
  32.         end
  33.     end
  34.  
  35.     def main
  36.         rpcutil = rpcclient("rpcutil", :options => options)
  37.         rpcutil.progress = false
  38.  
  39.         facts = {}
  40.  
  41.         rpcutil.get_fact(:fact => configuration[:fact]) do |resp|
  42.             begin
  43.                 value = resp[:body][:data][:value]
  44.                 if value
  45.                     facts.include?(value) ? facts[value] << resp[:senderid] : facts[value] = [ resp[:senderid] ]
  46.                 end
  47.             rescue Exception => e
  48.                 STDERR.puts "Could not parse facts for #{resp[:senderid]}: #{e.class}: #{e}"
  49.             end
  50.         end
  51.  
  52.         show_single_fact_report(configuration[:fact], facts, options[:verbose])
  53.  
  54.         printrpcstats
  55.     end
  56. end

Plain Code

class MCollective::Application::Facts<MCollective::Application
    description "Reports on usage for a specific fact"

    # this will be available in configuration[:fact]
    option :script,
        :description    => "Fact to report on",
        :arguments      => ["--fact FACT", "-f FACT"]

    def post_option_parser(configuration)
        configuration[:fact] = ARGV.shift if ARGV.size > 0
    end

    def validate_configuration(configuration)
        raise "Please specify a fact to report for" unless configuration.include?(:fact)
    end

    def show_single_fact_report(fact, facts, verbose=false)
        puts("Report for fact: #{fact}\n\n")

        facts.keys.sort.each do |k|
            printf("        %-40sfound %d times\n", k, facts[k].size)

            if verbose
                puts

                facts[k].sort.each do |f|
                    puts("            #{f}")
                end

                puts
            end
        end
    end

    def main
        rpcutil = rpcclient("rpcutil", :options => options)
        rpcutil.progress = false

        facts = {}

        rpcutil.get_fact(:fact => configuration[:fact]) do |resp|
            begin
                value = resp[:body][:data][:value]
                if value
                    facts.include?(value) ? facts[value] << resp[:senderid] : facts[value] = [ resp[:senderid] ]
                end
            rescue Exception => e
                STDERR.puts "Could not parse facts for #{resp[:senderid]}: #{e.class}: #{e}"
            end
        end

        show_single_fact_report(configuration[:fact], facts, options[:verbose])

        printrpcstats
    end
end

Permalink: http://codedumper.com/iloqe