<commit_before>require 'puppet/indirector/face'
Puppet::Indirector::Face.define(:query, '0.0.1') do

  copyright "<NAME>", 2011
  license   "Apache 2 license; see COPYING"


  require 'puppetdb'
  require 'puppet/util/puppetdb'
  require 'puppetX/puppetdb_query'

  PuppetX::Puppetdb_query.add_puppetdb_host_option(self)
  PuppetX::Puppetdb_query.add_puppetdb_port_option(self)
  # TODO get rid of the query and make it an argument
  PuppetX::Puppetdb_query.add_query_option(self)
  PuppetX::Puppetdb_query.add_only_active_option(self)

  action :fact do


    summary 'Serves as an interface to puppetdb allowing a user to query for a list of nodes'

    description <<-EOT
      Here is a ton of more useful information :)
    EOT

    option '--filter FACTS' do
      summary 'facts to return that represent each host'
      description <<-EOT
        Filter for the fact subcommand can be used to specify the facts to filter for.
        It accepts either a string or a comma delimited list of facts.
      EOT
      default_to { '' }
      before_action do |action, args, options|
        options[:filter] = options[:filter].split(',')
      end
    end

    when_invoked do |options|
      p = PuppetDB.new
      nodes = p.find_nodes_matching(options[:puppetdb_host], options[:puppetdb_port], options[:query], options[:only_active])
      node_hash = {}
      nodes.each do |node_name|
        node_hash[node_name] = p.find_node_facts(options[:puppetdb_host], options[:puppetdb_port], node_name, options[:filter])
      end
      node_hash
    end

  end

  action :node do

    summary 'Perform complex queries for nodes from PuppetDB'
    description <<-EOT
      Here is a ton of more useful information :)
    EOT

    option '--filter FACTS' do
      summary 'values to return that represent each host'
      description <<-EOT
        Filter is used to specify a single fact value that should be returned for each host.
      EOT
      default_to { nil }
    end

    option '--fail-on-dups' do
      summary 'fail if all returned fact values are not unique'
      default_to { false }
    end

    option '--unique', '--uniq' do
      summary 'only show unique fact values'
      default_to { false }
    end

#  not sure if this is supported
#    option '--only-inactive' do
#      summary 'only match inactive nodes'
#    end

    when_invoked do |options|
      p = PuppetDB.new
      nodes = p.find_nodes_matching(options[:puppetdb_host], options[:puppetdb_port], options[:query], options[:only_active])
      if options[:return_fact]
        nodes.map! do |node_name|
          p.find_node_facts(options[:puppetdb_host], options[:puppetdb_port], node_name, options[:filter]).values[0]
        end
        raise(Puppet::Error, "Duplicate facts found and fail on dups specified") if options[:fail_on_dups] and nodes.uniq.size != nodes.size
        if options[:unique]
          nodes.uniq
        else
          nodes
        end<commit_msg>Fix typo<commit_after>require 'puppet/indirector/face'
Puppet::Indirector::Face.define(:query, '0.0.1') do

  copyright "Puppet Labs", 2011
  license   "Apache 2 license; see COPYING"


  require 'puppetdb'
  require 'puppet/util/puppetdb'
  require 'puppetX/puppetdb_query'

  PuppetX::Puppetdb_query.add_puppetdb_host_option(self)
  PuppetX::Puppetdb_query.add_puppetdb_port_option(self)
  # TODO get rid of the query and make it an argument
  PuppetX::Puppetdb_query.add_query_option(self)
  PuppetX::Puppetdb_query.add_only_active_option(self)

  action :fact do


    summary 'Serves as an interface to puppetdb allowing a user to query for a list of nodes'

    description <<-EOT
      Here is a ton of more useful information :)
    EOT

    option '--filter FACTS' do
      summary 'facts to return that represent each host'
      description <<-EOT
        Filter for the fact subcommand can be used to specify the facts to filter for.
        It accepts either a string or a comma delimited list of facts.
      EOT
      default_to { '' }
      before_action do |action, args, options|
        options[:filter] = options[:filter].split(',')
      end
    end

    when_invoked do |options|
      p = PuppetDB.new
      nodes = p.find_nodes_matching(options[:puppetdb_host], options[:puppetdb_port], options[:query], options[:only_active])
      node_hash = {}
      nodes.each do |node_name|
        node_hash[node_name] = p.find_node_facts(options[:puppetdb_host], options[:puppetdb_port], node_name, options[:filter])
      end
      node_hash
    end

  end

  action :node do

    summary 'Perform complex queries for nodes from PuppetDB'
    description <<-EOT
      Here is a ton of more useful information :)
    EOT

    option '--filter FACTS' do
      summary 'values to return that represent each host'
      description <<-EOT
        Filter is used to specify a single fact value that should be returned for each host.
      EOT
      default_to { nil }
    end

    option '--fail-on-dups' do
      summary 'fail if all returned fact values are not unique'
      default_to { false }
    end

    option '--unique', '--uniq' do
      summary 'only show unique fact values'
      default_to { false }
    end

#  not sure if this is supported
#    option '--only-inactive' do
#      summary 'only match inactive nodes'
#    end

    when_invoked do |options|
      p = PuppetDB.new
      nodes = p.find_nodes_matching(options[:puppetdb_host], options[:puppetdb_port], options[:query], options[:only_active])
      if options[:filter]
        nodes.map! do |node_name|
          p.find_node_facts(options[:puppetdb_host], options[:puppetdb_port], node_name, options[:filter]).values[0]
        end
        raise(Puppet::Error, "Duplicate facts found and fail on dups specified") if options[:fail_on_dups] and nodes.uniq.size != nodes.size
        if options[:unique]
          nodes.uniq
        else
          nodes
        end
{'fraction_non_alphanumeric': 0.08969368759519378, 'fraction_numerical': 0.003046200710780166, 'mean_word_length': 2.9478957915831665, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1647830', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1839, 'n_words': 563}