# frozen_string_literal: true # Requirements # ======================================================================= # Stdlib # ----------------------------------------------------------------------- # Deps # ----------------------------------------------------------------------- require 'plist' require 'fileutils' # Project / Package # ----------------------------------------------------------------------- # Refinements # ======================================================================= using NRSER using NRSER::Types # Definitions # ======================================================================= # Represents a backend agent that the proxy can route to. # # Agents are managed by `launchd`, macOS's system service manager, and # created on-demand by generating "property list" files (`.plist`, XML format) # and installing them in the user's `~/Library/LaunchAgents` directory. # # From there they can be managed directly with macOS's `launchctl` utility, # with the `lunchy` gem, etc. # class Locd::Agent # Constants # ========================================================================== # Attribute / method names that {#to_h} uses. # # @return [Hamster::SortedSet] # TO_H_NAMES = Hamster::SortedSet[:label, :path, :plist, :status] # Mixins # ========================================================================== # Add {.logger} and {#logger} methods include SemanticLogger::Loggable # Make agents sortable by label include Comparable # Class Methods # ========================================================================== # Test if the parse of a property list is for a Loc'd agent by seeing if # it has the config key (from `Locd.config[:agent, :config_key]`) as a key. # # @param [Hash] plist # {include:file:doc/include/plist.md} # # @return [Boolean] # `true` if the plist looks like it's from Loc'd. # def self.plist? plist plist.key? Locd.config[:agent, :config_key] end # .plist? # @!group Computing Paths # -------------------------------------------------------------------------- # Absolute path to `launchd` plist directory for the current user, which is # an expansion of `~/Library/LaunchAgents`. # # @return [Pathname] # def self.user_plist_abs_dir Pathname.new( '~/Library/LaunchAgents' ).expand_path end # Absolute path for the plist file given it's label, equivalent to expanding # `~/Library/LaunchAgents/