# frozen_string_literal: true # Requirements # ======================================================================= # Stdlib # ----------------------------------------------------------------------- # Deps # ----------------------------------------------------------------------- # Project / Package # ----------------------------------------------------------------------- # Refinements # ======================================================================= using NRSER using NRSER::Types # Definitions # ======================================================================= # CLI interface using the `thor` gem. # # @see http://whatisthor.com/ # class Locd::CLI::Command::Main < Locd::CLI::Command::Base # Constants # ============================================================================ # Just the symbol log levels from {SemnaticLogger::LEVELS} converted to # frozen strings # # @return [Array] # LOG_LEVEL_STRINGS = SemanticLogger::LEVELS.map { |sym| sym.to_s.freeze } # Global (Class-Level) Options # ============================================================================ # # Applicable to all commands. # common_class_options :backtrace class_option :log_level, desc: "Set log level", type: :string, enum: LOG_LEVEL_STRINGS. zip( LOG_LEVEL_STRINGS.map { |level| level[0] } ). flatten, default: ENV['LOCD_LOG_LEVEL'] class_option :debug, desc: "Set log level to debug", type: :boolean class_option :trace, desc: "Set log level to trace", type: :boolean class_option :verbose, desc: "Turn on DEBUG-level logging", aliases: '-v', type: :boolean class_option :json, desc: "Output in JSON format (to STDOUT)", type: :boolean class_option :yaml, desc: "Output in YAML format (to STDOUT)", type: :boolean # Construction # ============================================================================ # Wrap {Thor#initialize} to call {#init_setup_logging} after letting `super` # do it's thing so logging is setup before we do anything else. # def initialize args = [], local_options = {}, config = {} super args, local_options, config # If anything raises in here, the command seems to just silently exit..? begin init_setup_logging! rescue Exception => e $stderr.write \ "ERROR: #{ e.message } #{ e.class }\n#{ e.backtrace.join( "\n" ) }\n" end logger.debug "initialized", args: args, local_options: local_options, options: self.options end # Helpers # ============================================================================ # protected # Setup logging using {#options}. # def init_setup_logging! kwds = {} level = if options[:trace] :trace elsif options[:debug] :debug elsif options[:log_level] LOG_LEVEL_STRINGS.find_only { |level| level.start_with? options[:log_level] }.to_sym end Locd::Logging.level = level unless level.nil? Locd::Logging.set_config_levels if [:trace, :debug].include? Locd::Logging.level logger.send Locd::Logging.level, "Hello! We about to start the show..." end end # end protected public # Commands # ============================================================================ # Querying # ---------------------------------------------------------------------------- desc "version", "Print the version" def version respond Locd::VERSION end # System # ---------------------------------------------------------------------------- desc "config", "Dump config" def config respond Locd.config.to_h end # Sub-Commands # ============================================================================ desc 'site SUBCOMMAND...', 'Deal with site agents.' subcommand 'site', Locd::CLI::Command::Site desc 'job SUBCOMMAND...', 'Deal with job agents.' subcommand 'job', Locd::CLI::Command::Job desc 'proxy SUBCOMMAND...', 'Deal with the HTTP proxy that routes requests to servers' subcommand 'proxy', Locd::CLI::Command::Proxy desc 'rotate-logs SUBCOMMAND...', 'Deal with the log rotation job' # map :'rotate-logs' => :rotate_logs subcommand 'rotate_logs', Locd::CLI::Command::RotateLogs end # class Locd::CLI::Command::Main