<commit_before>#!/usr/bin/env ruby

require 'syrup.rb'
require 'trollop'
require 'flapjack-diner'

module SyrupCLI
  # Thanks to agent462 and the Sensu-CLI project for the base CLI design.
  # https://github.com/agent462/sensu-cli

  class Cli
    OBJECTS = %w(contact medium pagerduty rule entity check)

    BASE_COMMANDS        = %w(create get update delete)
    MAINTENANCE_COMMANDS = %w(create-scheduled-maintenance delete-scheduled-maintenance start-unscheduled-maintenance update-unscheduled-maintenance get-maintenance-periods status outages downtimes test)

    CONTACT_COMMANDS     = BASE_COMMANDS
    MEDIUM_COMMANDS      = BASE_COMMANDS
    PAGERDUTY_COMMANDS   = BASE_COMMANDS
    RULE_COMMANDS        = BASE_COMMANDS
    ENTITY_COMMANDS      = %w(create get update).concat(MAINTENANCE_COMMANDS)
    CHECK_COMMANDS       = %w(create update).concat(MAINTENANCE_COMMANDS)

    CONTACT_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Contact Commands **
          syrup contact create
          syrup contact get
          syrup contact update
          syrup contact delete\n\r
        EOS
    MEDIUM_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Medium Commands **
          syrup medium create
          syrup medium get
          syrup medium update
          syrup medium delete\n\r
        EOS
    PAGERDUTY_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** PagerDuty Commands **
          syrup pagerduty create
          syrup pagerduty get
          syrup pagerduty update
          syrup pagerduty delete\n\r
        EOS
    RULE_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Notification Rule Commands **
          syrup rule create
          syrup rule get
          syrup rule update
          syrup rule delete\n\r
        EOS
    ENTITY_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Entity Commands **
          syrup entity get
          syrup entity update
          syrup entity status
          syrup entity test\n\r
        EOS
        # TODO: Re-add 'syrup check get' if it turns out to be a valid option
    CHECK_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Check Commands **
          syrup check update
          syrup check status
          syrup check test\n\r
        EOS

    attr_accessor :global_args
    attr_accessor :object
    attr_accessor :object_args
    attr_accessor :action
    attr_accessor :action_args

    def parse
      @global_parser = Trollop::Parser.new do
#        version "sensu-cli version: #{SensuCli::VERSION}"
        version "\n\rsyrup version #{Syrup::VERSION}"
        banner "\n\rsyrup: Create or manipulate objects in your Flapjack environment.\n\r"
        banner "Available subcommands: (syrup SUB-COMMAND --help for detailed usage information)\n\r"
        banner CONTACT_BANNER
        banner MEDIUM_BANNER
        banner PAGERDUTY_BANNER
        banner RULE_BANNER
        banner ENTITY_BANNER
        banner CHECK_BANNER
        banner "Global options:"
        opt :host, "Host to connect to", :default => "localhost"
        opt :port, "Port to connect to", :default => "3081"
        opt :log, "Flapjack API log", :default => "flapjack_diner.log"
        opt :pretty, "Pretty-print JSON output"
        stop_on OBJECTS
      end

      Trollop::with_standard_exception_handling @global_parser do
        @global_args = @global_parser.parse ARGV
        raise Trollop::HelpNeeded if ARGV.empty? # show help screen
      end
      @object = ARGV.shift
      self.respond_to?(@object) ? send(@object) : explode(@global_parser)

    end

    def subparser(cli)
      Trollop::Parser.new do
        banner Cli.const_get("#{cli}_BANNER")
        stop_on Cli.const_get("#{cli}_COMMANDS")
      end
    end

    def explode_if_empty(opts, command)
      explode(opts) if ARGV.empty? && command != 'list'
    end

    def explode(opts)
      Trollop::with_standard_exception_handling opts do
        raise Trollop::HelpNeeded # show help screen
      end
    end


    def contact
      opts = subparser('CONTACT')
      @action = ARGV.shift
      case @action
      when 'create'
        @action_args = Trollop::options do
          banner <<-EOS.gsub(/^ {12}/, '')
            \n\rsyrup contact create: Create a new contact.

            By default, this will create a new e-mail medium attached to the contact.

            NOTE: Flapjack creates and maintains a 'default' notification rule for all contacts. You can modify the 'blackhole' attributes on this rule, but any other changes will cause a new blank rule to be created.

            Example: syrup --GLOBALS contact create --first-name FIRST --last-name LAST --email EMAIL

            Options:
          EOS
          opt :id,               "Unique identifier (generated if omitted)", :type => :string
          opt :first_name,       "First name (required)", :type => :string, :required => true
          opt :last_name,        "Last name (required)", :type => :string, :required => true
          opt :email,            "Email address (required)", :type => :string, :required => true
          opt :interval,         "Notification interval for email", :type => :integer, :default => 7200
          opt :rollup_threshold, "Rollup threshold for email", :type => :integer, :default => 0
          opt :timezone,         "Time zone", :type => :string
          opt :tags,             "Tags (comma-separated)", :type => :string
          opt :no_media,         "Do not automatically create the email medium"
#          opt :no_all_entity,    "Do not add to the ALL entity"
          #TODO: There appears to be no way to set rules or entities on creation
        end
      when 'get'
        @action_args = Trollop::options do
          banner <<-EOS.gsub(/^ {12}/, '')
            \n\rsyrup contact get: Get JSON contact data.

            Specify IDs as comma-separated values, or no IDs to get all contacts.

            Example: syrup --GLOBALS contact get [--ids FIRST,SECOND,THIRD]

            Options:<commit_msg>[EOPI-331] Linting<commit_after>#!/usr/bin/env ruby

require 'syrup.rb'
require 'trollop'
require 'flapjack-diner'

module SyrupCLI
  # Thanks to agent462 and the Sensu-CLI project for the base CLI design.
  # https://github.com/agent462/sensu-cli

  class Cli
    OBJECTS = %w(contact medium pagerduty rule entity check)

    BASE_COMMANDS        = %w(create get update delete)
    MAINTENANCE_COMMANDS = %w(create-scheduled-maintenance delete-scheduled-maintenance start-unscheduled-maintenance update-unscheduled-maintenance get-maintenance-periods status outages downtimes test)

    CONTACT_COMMANDS     = BASE_COMMANDS
    MEDIUM_COMMANDS      = BASE_COMMANDS
    PAGERDUTY_COMMANDS   = BASE_COMMANDS
    RULE_COMMANDS        = BASE_COMMANDS
    ENTITY_COMMANDS      = %w(create get update).concat(MAINTENANCE_COMMANDS)
    CHECK_COMMANDS       = %w(create update).concat(MAINTENANCE_COMMANDS)

    CONTACT_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Contact Commands **
          syrup contact create
          syrup contact get
          syrup contact update
          syrup contact delete\n\r
        EOS
    MEDIUM_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Medium Commands **
          syrup medium create
          syrup medium get
          syrup medium update
          syrup medium delete\n\r
        EOS
    PAGERDUTY_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** PagerDuty Commands **
          syrup pagerduty create
          syrup pagerduty get
          syrup pagerduty update
          syrup pagerduty delete\n\r
        EOS
    RULE_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Notification Rule Commands **
          syrup rule create
          syrup rule get
          syrup rule update
          syrup rule delete\n\r
        EOS
    ENTITY_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Entity Commands **
          syrup entity create-ALL
          syrup entity get
          syrup entity update
          syrup entity status
          syrup entity test\n\r
        EOS
        # TODO: Re-add 'syrup check get' if it turns out to be a valid option
    CHECK_BANNER = <<-EOS.gsub(/^ {10}/, '')
          ** Check Commands **
          syrup check update
          syrup check status
          syrup check test\n\r
        EOS

    attr_accessor :global_args
    attr_accessor :object
    attr_accessor :object_args
    attr_accessor :action
    attr_accessor :action_args

    def parse
      @global_parser = Trollop::Parser.new do
#        version "sensu-cli version: #{SensuCli::VERSION}"
        version "\n\rsyrup version #{Syrup::VERSION}"
        banner "\n\rsyrup: Create or manipulate objects in your Flapjack environment.\n\r"
        banner "Available subcommands: (syrup SUB-COMMAND --help for detailed usage information)\n\r"
        banner CONTACT_BANNER
        banner MEDIUM_BANNER
        banner PAGERDUTY_BANNER
        banner RULE_BANNER
        banner ENTITY_BANNER
        banner CHECK_BANNER
        banner "Global options:"
        opt :host, "Host to connect to", :default => "localhost"
        opt :port, "Port to connect to", :default => "3081"
        opt :log, "Flapjack API log", :default => "flapjack_diner.log"
        opt :pretty, "Pretty-print JSON output"
        stop_on OBJECTS
      end

      Trollop::with_standard_exception_handling @global_parser do
        @global_args = @global_parser.parse ARGV
        raise Trollop::HelpNeeded if ARGV.empty? # show help screen
      end
      @object = ARGV.shift
      self.respond_to?(@object) ? send(@object) : explode(@global_parser)

    end

    def subparser(cli)
      Trollop::Parser.new do
        banner Cli.const_get("#{cli}_BANNER")
        stop_on Cli.const_get("#{cli}_COMMANDS")
      end
    end

    def explode_if_empty(opts, command)
      explode(opts) if ARGV.empty? && command != 'list'
    end

    def explode(opts)
      Trollop::with_standard_exception_handling opts do
        raise Trollop::HelpNeeded # show help screen
      end
    end


    def contact
      opts = subparser('CONTACT')
      @action = ARGV.shift
      case @action
      when 'create'
        @action_args = Trollop::options do
          banner <<-EOS.gsub(/^ {12}/, '')
            \n\rsyrup contact create: Create a new contact.

            By default, this will create a new e-mail medium attached to the contact.

            NOTE: Flapjack creates and maintains a 'default' notification rule for all contacts. You can modify the 'blackhole' attributes on this rule, but any other changes will cause a new blank rule to be created.

            Example: syrup --GLOBALS contact create --first-name FIRST --last-name LAST --email EMAIL

            Options:
          EOS
          opt :id,               "Unique identifier (generated if omitted)", :type => :string
          opt :first_name,       "First name (required)", :type => :string, :required => true
          opt :last_name,        "Last name (required)", :type => :string, :required => true
          opt :email,            "Email address (required)", :type => :string, :required => true
          opt :interval,         "Notification interval for email", :type => :integer, :default => 7200
          opt :rollup_threshold, "Rollup threshold for email", :type => :integer, :default => 0
          opt :timezone,         "Time zone", :type => :string
          opt :tags,             "Tags (comma-separated)", :type => :string
          opt :no_media,         "Do not automatically create the email medium"
#          opt :no_all_entity,    "Do not add to the ALL entity"
          #TODO: There appears to be no way to set rules or entities on creation.
          # Consider automatically adding to ALL entity once we can reliably get back the new contact's ID.
        end
      when 'get'
        @action_args = Trollop::options do
          banner <<-EOS.gsub(/^ {12}/, '')
            \n\rsyrup contact get: Get JSON contact data.

            Specify IDs as comma-separated values, or no IDs to get all contacts.

            Example: syrup --GLOBALS contact get [--ids FIRST,SECOND,THIRD]

            Options:
{'fraction_non_alphanumeric': 0.09348582794629537, 'fraction_numerical': 0.005387037957898227, 'mean_word_length': 2.190639873083025, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 2, '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': '606691', 'n_tokens_mistral': 3722, 'n_tokens_neox': 3649, 'n_words': 1101}