class Locd::Agent

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.

Constants

TO_H_NAMES

Attribute / method names that {#to_h} uses.

@return [Hamster::SortedSet<Symbol>]

Public Class Methods

default_log_path(workdir, label) click to toggle source

Default path for a {Locd::Agent} output file.

@param [Pathname] workdir

The working directory the agent will run in.

@param [String] label

The agent's label.

@return [Pathname]

If we could find a `tmp` directory walking up from `workdir`.

@return [nil]

If we could not find a `tmp` directory walking
# File lib/locd/agent.rb, line 156
def self.default_log_path workdir, label
  tmp_dir = workdir.find_up 'tmp', test: :directory?, result: :path
  
  if tmp_dir.nil?
    logger.warn "Unable to find `tmp` dir, output will not be redirected",
      workdir: workdir,
      label: label,
      stream: stream
    return nil
  end
  
  unless tmp_dir.writable?
    logger.warn \
      "Found `tmp` dir, but not writable. Output will not be redirected",
      workdir: workdir,
      label: label,
      stream: stream
    return nil
  end
  
  tmp_dir / 'locd' / "#{ label }.log"
end
plist?(plist) click to toggle source

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<String, Object>] plist

{include:file:doc/include/plist.md}

@return [Boolean]

`true` if the plist looks like it's from Loc'd.
# File lib/locd/agent.rb, line 111
def self.plist? plist
  plist.key? Locd.config[:agent, :config_key]
end
plist_abs_path(label) click to toggle source

Absolute path for the plist file given it's label, equivalent to expanding `~/Library/LaunchAgents/<label>.plist`.

@param [String] label

The agent's label.

@return [Pathname]

# File lib/locd/agent.rb, line 137
def self.plist_abs_path label
  user_plist_abs_dir / "#{ label }.plist"
end
resolve_log_path(log_path:, workdir:, label: if log_path.nil?) click to toggle source

Get the path to log `STDOUT` and `STDERR` to given the option value and other options we need to figure it out if that doesn't suffice.

Note that we might not figure it out at all (returning `nil`), and that needs to be ok too (though it's not expected to happen all too often).

@param [nil | String | Pathname] log_path:

The value the user provided (if any).

1.  `nil` - {.default_log_path} is called and it's result returned.

2.  `String | Pathname` - Value is expanded against `workdir` and the
    resulting absolute path is returned (which of course may not exist).

    This means that absolute, home-relative (`~/` paths) and
    `workdir`-relative paths should all produce the expected results.

@return [Pathname]

Absolute path where the agent will try to log output.

@return [nil]

If the user doesn't supply a value and {.default_log_path} returns
`nil`. The agent will not log output in this case.
# File lib/locd/agent.rb, line 204
def self.resolve_log_path log_path:, workdir:, label:
  if log_path.nil?
    default_log_path workdir, label
  else
    log_path.to_pn.expand_path workdir
  end
user_plist_abs_dir() click to toggle source

Absolute path to `launchd` plist directory for the current user, which is an expansion of `~/Library/LaunchAgents`.

@return [Pathname]

# File lib/locd/agent.rb, line 124
def self.user_plist_abs_dir
  Pathname.new( '~/Library/LaunchAgents' ).expand_path
end