<commit_before># encoding: utf-8

require 'thor'
require 'sonar/cli/rcfile'
require 'awesome_print'

module Sonar
  class CLI < Thor
    class_option 'profile', aliases: '-P', type: :string, default: File.join(File.expand_path('~'), Sonar::RCFile::FILENAME),
                            desc: 'Path to Sonar RC file', banner: 'FILE'
    class_option 'format', type: :string, desc: 'Flat JSON, JSON lines, or pretty printed [flat/lines/pretty]'

    def initialize(*)
      @config = Sonar::RCFile.instance.load_file
      @client = Sonar::Client.new(email: @config["email"], access_token: @config["access_token"], api_url: @config["api_url"])
      super
    end

    desc 'profile', 'Display the current profile from sonar.rc'
    def profile
      ap @config
    end

    desc 'usage', 'Display API usage for current user'
    def usage
      ap @client.usage
    end

    desc 'search [QUERY TYPE] [QUERY TERM]', 'Search anything from Sonars'
    method_option 'record_limit', type: :numeric, aliases: '-n', desc: 'Maximum number of records to fetch'
    method_option 'exact', type: :boolean, aliases: '-e', desc: 'Search for the query string exactly, do not including partial string matches'

    def search(type, term)
      @query = {}
      @query[type.to_sym] = term
      @query[:limit] = options['record_limit']
      @query[:exact] = options['exact']
      resp = @client.search(@query)

      if resp.is_a?(Sonar::Request::RequestIterator)
        resp.each do |data|
          print_json(cleanup_data(data), options['format'])
        end
      else
        print_json(cleanup_data(resp), options['format'])
      end
    end

    desc 'types', 'List all Sonar query types'
    def types
      ap Search::QUERY_TYPES_MAP<commit_msg>Fix type in task description<commit_after># encoding: utf-8

require 'thor'
require 'sonar/cli/rcfile'
require 'awesome_print'

module Sonar
  class CLI < Thor
    class_option 'profile', aliases: '-P', type: :string, default: File.join(File.expand_path('~'), Sonar::RCFile::FILENAME),
                            desc: 'Path to Sonar RC file', banner: 'FILE'
    class_option 'format', type: :string, desc: 'Flat JSON, JSON lines, or pretty printed [flat/lines/pretty]'

    def initialize(*)
      @config = Sonar::RCFile.instance.load_file
      @client = Sonar::Client.new(email: @config["email"], access_token: @config["access_token"], api_url: @config["api_url"])
      super
    end

    desc 'profile', 'Display the current profile from sonar.rc'
    def profile
      ap @config
    end

    desc 'usage', 'Display API usage for current user'
    def usage
      ap @client.usage
    end

    desc 'search [QUERY TYPE] [QUERY TERM]', 'Search anything from Sonars'
    method_option 'record_limit', type: :numeric, aliases: '-n', desc: 'Maximum number of records to fetch'
    method_option 'exact', type: :boolean, aliases: '-e', desc: 'Search for the query string exactly, do not include partial string matches'

    def search(type, term)
      @query = {}
      @query[type.to_sym] = term
      @query[:limit] = options['record_limit']
      @query[:exact] = options['exact']
      resp = @client.search(@query)

      if resp.is_a?(Sonar::Request::RequestIterator)
        resp.each do |data|
          print_json(cleanup_data(data), options['format'])
        end
      else
        print_json(cleanup_data(resp), options['format'])
      end
    end

    desc 'types', 'List all Sonar query types'
    def types
      ap Search::QUERY_TYPES_MAP
{'fraction_non_alphanumeric': 0.12781740370898717, 'fraction_numerical': 0.0005706134094151213, 'mean_word_length': 3.2190132370637787, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '1637993', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1058, 'n_words': 325}