# frozen_string_literal: true # Requirements # ======================================================================= # Stdlib # ----------------------------------------------------------------------- require 'shellwords' # Deps # ----------------------------------------------------------------------- require 'thor' # Project / Package # ----------------------------------------------------------------------- # Refinements # ======================================================================= require 'nrser/refinements/types' using NRSER::Types # Definitions # ======================================================================= # Manage log rotation. # class Locd::CLI::Command::RotateLogs < Locd::CLI::Command::Job # Helpers # ============================================================================ # def self.agent_class Locd::Agent::RotateLogs end protected # ======================================================================== def agent @agent ||= begin agent = agent_class.get if agent.nil? logger.error "Agent plist not found", expected_path: agent_class.plist_abs_path.to_s logger.info "Run `locd setup` to create it." exit 1 end agent end end # end protected public # Commands # ============================================================================ desc "add [OPTIONS] [-- CMD_TEMPLATE...]", "Add agents that runs a command in the current directory" include_options groups: [:write, :add, :respond_with_agents] def add *cmd_template, **kwds if cmd_template.empty? || cmd_template.all?( &:empty? ) cmd_template = agent_class.default_cmd_template end if options.key? :label raise Thor::ProhibitedArgumentError, "Label can not be customized on system agents" end kwds[:start_interval] ||= if options[:every] || options[:at] get_start_interval else agent_class.default_start_interval end super *cmd_template, **kwds # agent = agent_class.add **add_kwds # # logger.info "`#{ agent.label }` agent created." # # agent.reload if options[:load] # # respond agent end desc "run", "Rotate agent logs using `newsyslog`" def run_ Locd::Newsyslog.run_all end # Agent Interface # ---------------------------------------------------------------------------- desc 'status', 'Print agent status' def status respond \ label: agent.label, status: agent.status end desc "rm", "Remove (uninstall, delete) the agent" map remove: :rm def rm agent.remove end desc "plist", "Show the agent's launchd property list" def plist if options[:json] || options[:yaml] respond agent.plist else respond agent.path.read end end desc "start", "Start the agent" option :write, desc: "Set `launchd` *Disabled* key to `false`", type: :boolean def start agent.start **option_kwds( :write ) end desc "stop", "Stop the agent" include_options groups: [:stop] option :write, desc: "Set `launchd` *Disabled* key to `true`", type: :boolean def stop agent.stop **option_kwds( :unload, groups: :stop ) end desc "restart", "Restart the agent" include_options groups: [:stop] option :write, desc: "Set `launchd` *Disabled* key to `false`", type: :boolean def restart agent.restart **option_kwds( :write, groups: :stop ) end end # module Locd::CLI::RotateLogs