<commit_before>require 'clamp'
require 'highline/import'

module Pagerduty
  module CLT
    class CLI < Clamp::Command

      class AbstractCommand < Clamp::Command
        option [ '-c', '--config_file' ], 'CONFIG', 'Config file', default: Pagerduty::CLT::Config::DEFAULT_CONFIG_FILE

        option '--version', :flag, 'show version' do
          puts Pagerduty::CLT::VERSION
          exit(0)
        end
      end

      class ConsoleCommand < AbstractCommand
        def execute
          begin
            require 'pry-byebug'
          rescue LoadError
            $logger.error 'pry-byebug not installed.'
            exit(1)
          end
          pry ::Pagerduty::CLT
        end
      end

      class AcknowledgeCommand < AbstractCommand
        parameter('PATTERN', 'pattern to match (on node)', required: false)
        option('--interactive', :flag, 'Interactively acknowledge', default: false)
        option('--everyone', :flag, 'ALL incidents, not just mine', default: false)
        option('--yes', :flag, "Don't confirm, just do it!", default: false)

        def execute
          options = { status: [ Status::TRIGGERED ], pattern: pattern }
          options[:user_id] = nil if everyone?
          ack_options = { confirm: !yes? }

          if interactive?
            Incidents.new.where(options).acknowledge!
          else
            Incidents.new.where(options).acknowledge_all!(ack_options)
          end
        end
      end

      class ReassignCommand < AbstractCommand
        parameter('USER_PATTERN', 'user to reassign incident(s) to', required: true)
        parameter('PATTERN', 'pattern to match (on node)', required: false)
        option('--yes', :flag, "Don't confirm, just do it!", default: false)

        def execute
          options = { status: Status::UNRESOLVED, pattern: pattern, user_id: nil }
          reassign_options = { confirm: !yes? }

          users = User.search(user_pattern)

          if users.count > 1
            puts Formatters::Users::Table.new(users).render
            user_id = ask("\n%s match(e)s, please enter a User ID: " % users.count)
            users = unless user_id.empty?
                      users.select { |user| user.match?(user_id) }
                    else
                      []
                    end
          end

          if users.count == 1
            user = users.first
          else
            $logger.error "Unable find User using pattern '#{user_pattern}'"
            exit(1)
          end

          Incidents.new.where(options).reassign_all!(user, reassign_options)
        end
      end

      class ResolveCommand < AbstractCommand
        parameter('PATTERN', 'pattern to match (on node)', required: false)
        option('--interactive', :flag, 'Interactively acknowledge', default: false)
        option('--everyone', :flag, 'All incidents, not just mine', default: false)
        option('--yes', :flag, "Don't confirm, just do it!", default: false)

        def execute
          options = { status: Status::UNRESOLVED, pattern: pattern }
          options[:user_id] = nil if everyone?
          resolve_options = { confirm: !yes? }

          if interactive?
            Incidents.new.where(options).resolve!
          else
            Incidents.new.where(options).resolve_all!(resolve_options)
          end
        end
      end

      class ListNeedingAttentionCommand < AbstractCommand
        option('--everyone', :flag, 'All incidents, not just mine', default: false)

        def execute
          options = { status: Status::UNRESOLVED }
          options[:user_id] = nil if everyone?

          incidents = Incidents.new.where(options)
          table = Formatters::Incidents::Table.new(incidents).render
          if table
            puts table
            puts "\n%s match(es)" % incidents.count
          end
        end
      end

      class SchedulesCommand < AbstractCommand
        option [ '-q', '--query' ], 'QUERY', 'Query'

        def execute
          options = { query: query }

          schedules = Schedules.new.where(options)
          table = Formatters::Schedules::Table.new(schedules).render
          puts table if table
        end
      end

      class OncallCommand < AbstractCommand
        option [ '-q', '--query' ], 'QUERY', 'Query'

        def execute
          options = { query: query }<commit_msg>Allow --format json/csv to be defined when listing incidents<commit_after>require 'clamp'
require 'highline/import'

module Pagerduty
  module CLT
    class CLI < Clamp::Command

      class AbstractCommand < Clamp::Command
        option [ '-c', '--config_file' ], 'CONFIG', 'Config file', default: Pagerduty::CLT::Config::DEFAULT_CONFIG_FILE

        option '--version', :flag, 'show version' do
          puts Pagerduty::CLT::VERSION
          exit(0)
        end
      end

      class ConsoleCommand < AbstractCommand
        def execute
          begin
            require 'pry-byebug'
          rescue LoadError
            $logger.error 'pry-byebug not installed.'
            exit(1)
          end
          pry ::Pagerduty::CLT
        end
      end

      class AcknowledgeCommand < AbstractCommand
        parameter('PATTERN', 'pattern to match (on node)', required: false)
        option('--interactive', :flag, 'Interactively acknowledge', default: false)
        option('--everyone', :flag, 'ALL incidents, not just mine', default: false)
        option('--yes', :flag, "Don't confirm, just do it!", default: false)

        def execute
          options = { status: [ Status::TRIGGERED ], pattern: pattern }
          options[:user_id] = nil if everyone?
          ack_options = { confirm: !yes? }

          if interactive?
            Incidents.new.where(options).acknowledge!
          else
            Incidents.new.where(options).acknowledge_all!(ack_options)
          end
        end
      end

      class ReassignCommand < AbstractCommand
        parameter('USER_PATTERN', 'user to reassign incident(s) to', required: true)
        parameter('PATTERN', 'pattern to match (on node)', required: false)
        option('--yes', :flag, "Don't confirm, just do it!", default: false)

        def execute
          options = { status: Status::UNRESOLVED, pattern: pattern, user_id: nil }
          reassign_options = { confirm: !yes? }

          users = User.search(user_pattern)

          if users.count > 1
            puts Formatters::Users::Table.new(users).render
            user_id = ask("\n%s match(e)s, please enter a User ID: " % users.count)
            users = unless user_id.empty?
                      users.select { |user| user.match?(user_id) }
                    else
                      []
                    end
          end

          if users.count == 1
            user = users.first
          else
            $logger.error "Unable find User using pattern '#{user_pattern}'"
            exit(1)
          end

          Incidents.new.where(options).reassign_all!(user, reassign_options)
        end
      end

      class ResolveCommand < AbstractCommand
        parameter('PATTERN', 'pattern to match (on node)', required: false)
        option('--interactive', :flag, 'Interactively acknowledge', default: false)
        option('--everyone', :flag, 'All incidents, not just mine', default: false)
        option('--yes', :flag, "Don't confirm, just do it!", default: false)

        def execute
          options = { status: Status::UNRESOLVED, pattern: pattern }
          options[:user_id] = nil if everyone?
          resolve_options = { confirm: !yes? }

          if interactive?
            Incidents.new.where(options).resolve!
          else
            Incidents.new.where(options).resolve_all!(resolve_options)
          end
        end
      end

      class ListNeedingAttentionCommand < AbstractCommand
        option('--everyone', :flag, 'All incidents, not just mine', default: false)
        option([ '-f', '--format' ], 'FORMAT', 'Format', default: 'table')

        def execute
          options = { status: Status::UNRESOLVED }
          options[:user_id] = nil if everyone?

          incidents = Incidents.new.where(options)

          formater_klass = case format
          when 'table'
            Formatters::Incidents::Table
          when 'csv'
            Formatters::Incidents::CSV
          when 'json'
            Formatters::Incidents::JSON
          else
            fail("Unknown format type '#{format}'")
          end

          output = formater_klass.new(incidents).render

          if output
            puts output
            puts "\n%s match(es)" % incidents.count if format == 'table'
          end
        end
      end

      class SchedulesCommand < AbstractCommand
        option [ '-q', '--query' ], 'QUERY', 'Query'

        def execute
          options = { query: query }

          schedules = Schedules.new.where(options)
          table = Formatters::Schedules::Table.new(schedules).render
          puts table if table
        end
      end

      class OncallCommand < AbstractCommand
        option [ '-q', '--query' ], 'QUERY', 'Query'

        def execute
          options = { query: query }
{'fraction_non_alphanumeric': 0.1092436974789916, 'fraction_numerical': 0.0010913456291607553, 'mean_word_length': 1.9743589743589745, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 5, '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': '3066851', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2414, 'n_words': 682}