# frozen_string_literal: true # Requirements # ======================================================================= # Project / Package # ----------------------------------------------------------------------- require_relative './base' # Refinements # ======================================================================= require 'nrser/refinements/types' using NRSER::Types # Namespace # ======================================================================= module Locd module CLI module Command # Definitions # ======================================================================= # CLI interface using the `thor` gem. # # @see http://whatisthor.com/ # class Agent < Base # Helpers # ========================================================================== # def self.agent_class Locd::Agent end # .agent_class def self.agent_type agent_class.name.split( '::' ).last.downcase end protected # ========================================================================== def agent_class self.class.agent_class end def agent_type self.class.agent_type end def agent_table agents Locd::CLI::Table.build do |t| t.col "PID", &:pid t.col "LEC", desc: "Last Exit Code", &:last_exit_code t.col "Label", &:label t.col "File" do |agent| agent_file agent end t.rows agents end end # The {#pattern} argument is not required because system agent subclasses # don't need or want one. # # My current solution is to check {#pattern} before using it in # {#find_only!} and {#find_multi!} and override those in system agent # commands. # def check_pattern! pattern if pattern.nil? raise Thor::RequiredArgumentMissingError, "No value provided for required arguments 'pattern'" end end # Find exactly one {Locd::Agent} for a `pattern`, using the any `:pattern` # shared options provided, and raising if there are no matches or more # than one. # # @param pattern (see Locd::Agent.find_only!) # # @return [Locd::Agent] # Matched agent. # # @raise If more or less than one agent is matched. # def find_only! pattern check_pattern! pattern agent_class.find_only! pattern, **option_kwds( groups: :pattern ) end def find_multi! pattern check_pattern! pattern # Behavior depend on the `:all` option... if options[:all] # `:all` is set, so we find all the agents for the pattern, raising # if we don't find any agent_class.find_all!( pattern, **option_kwds( groups: :pattern ) ).values else # `:all` is not set, so we need to find exactly one or error [find_only!( pattern )] end end # end protected public # Shared Options # ============================================================================ require_relative './agent/shared' # Commands # ============================================================================ # Querying # ---------------------------------------------------------------------------- require_relative './agent/ls' require_relative './agent/plist' require_relative './agent/status' # Commands for Manipulating Agent Definitions # ---------------------------------------------------------------------------- require_relative './agent/add' require_relative './agent/update' require_relative './agent/rm' # Commands for Manipulating Agent State # -------------------------------------------------------------------------- require_relative './agent/start' require_relative './agent/stop' require_relative './agent/restart' # Assorted Other Commands # ---------------------------------------------------------------------------- require_relative './agent/open' require_relative './agent/truncate_logs' require_relative './agent/tail' end # class Agent # /Namespace # ======================================================================= end # module Command end # module CLI end # module Locd