# encoding: UTF-8 # frozen_string_literal: true # Requirements # ======================================================================= ### Stdlib ### require 'shellwords' ### Deps ### require 'thor' ### Project / Package ### require_relative './agent' # 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 Proxy < Agent # Helpers # ============================================================================ # def self.agent_class Locd::Agent::Proxy end protected # ======================================================================== def proxy @proxy ||= begin proxy = Locd::Agent::Proxy.get if proxy.nil? logger.error "Proxy agent plist not found", expected_path: Locd::Agent::Proxy.plist_abs_path.to_s logger.info "Run `locd proxy add` to create it." exit 1 end proxy end end # end protected public # Commands # ============================================================================ desc "add", "Add agents that runs a command in the current directory" include_shared t[ groups: t.HasAny( :add, :respond_with_agents ) ] include_shared t[ groups: :write, name: t.Not( :label ) ] def add agent = agent_class.add **option_kwds( groups: :write ) logger.info "`#{ agent.label }` agent created." agent.reload if options[:load] respond agent end desc "run", "Run the proxy server (in the foreground)" method_option :bind, desc: "Address to bind the proxy server to", aliases: ['-b', '--host', '-h'], type: :string, default: Locd.config[:proxy, :bind] method_option :port, desc: "Port to run the proxy on", aliases: '-p', type: :numeric, default: Locd.config[:proxy, :port] def run_ Locd::Proxy.serve \ bind: options[:bind], port: options[:port] end desc "port", "Get port Loc'd proxy is running on or configured for" def port respond Locd::Proxy.port end # Agent Interface # ---------------------------------------------------------------------------- desc 'status', 'Print proxy status' def status respond \ label: proxy.label, port: proxy.port, status: proxy.status end desc "rm", "Remove (uninstall, delete) the proxy agent" map remove: :rm def rm proxy.remove end desc "plist", "Show the proxy's launchd property list" def plist agent = proxy if options[:json] || options[:yaml] respond agent.plist else respond agent.path.read end end desc "start", "Start the proxy" include_shared t[ groups: :start ] def start proxy.start **option_kwds( groups: :start ) end desc "stop", "Stop the proxy" include_shared t[ groups: :stop ] option :write, desc: "Set `launchd` *Disabled* key to `true`", type: :boolean def stop proxy.stop **option_kwds( groups: :stop ) end desc "restart", "Restart the proxy" include_shared t[ groups: t.HasAny( :start, :stop ) ] option :write, desc: "Set `launchd` *Disabled* key to `false`", type: :boolean def restart proxy.restart **option_kwds( groups: [ :start, :stop ] ) end end # class Proxy # /Namespace # ============================================================================ end # module Command end # module CLI end # module Locd