# frozen_string_literal: true # Requirements # ======================================================================= # Stdlib # ----------------------------------------------------------------------- # Deps # ----------------------------------------------------------------------- require 'thor/completion/bash' # Project / Package # ----------------------------------------------------------------------- # Refinements # ======================================================================= require 'nrser/refinements/types' using NRSER::Types # Namespace # ======================================================================== module Locd module CLI module Command # Definitions # ======================================================================= # CLI interface using the Thor via my `atli` fork. # # @see http://whatisthor.com/ # class Main < 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 } include Thor::Completion::Bash # 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 NRSER::Log.level = level unless level.nil? if levels = Locd.config[:log, :levels] levels.each do |name, level| const = begin NRSER.to_const name rescue next end level = level.to_sym self.logger.debug "Setting logger level", logger: const.logger, level: level const.logger.level = level self.logger.debug "Logger level set", logger: const.logger end end if [:trace, :debug].include? NRSER::Log.level logger.send NRSER::Log.level, "Hello! We about to start the show..." end end # end protected public # Commands # ============================================================================ # add_bash_completion_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 # ============================================================================ subcommand 'agent', Locd::CLI::Command::Agent, desc: 'Deal with agents in general (all types).' subcommand 'site', Locd::CLI::Command::Site, desc: 'Deal with site agents.' subcommand 'job', Locd::CLI::Command::Job, desc: "Deal with job agents." subcommand 'proxy', Locd::CLI::Command::Proxy, desc: 'Deal with the HTTP proxy that routes requests to servers' # subcommand 'rotate-logs', subcommand 'rotate_logs', Locd::CLI::Command::RotateLogs, desc: 'Deal with the log rotation job' subcommand 'project', Locd::CLI::Command::Project, desc: 'Manage Agent definitions for projects' end # class Main # /Namespace # ======================================================================== end # module Command end # module CLI end # module Locd