# frozen_string_literal: true # Requirements # ======================================================================= # Stdlib # ----------------------------------------------------------------------- require 'shellwords' # Deps # ----------------------------------------------------------------------- require 'thor' # Project / Package # ----------------------------------------------------------------------- # Refinements # ======================================================================= require 'nrser/refinements/types' using NRSER::Types # Definitions # ======================================================================= # CLI interface using the `thor` gem. # # @see http://whatisthor.com/ # class Locd::CLI::Command::Proxy < Locd::CLI::Command::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 setup` 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_options groups: [:write, :add, :respond_with_agents] def add agent = agent_class.add **option_kwds( groups: :write ) logger.info "`#{ agent.label }` agent created." agent.load 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" option :write, desc: "Set `launchd` *Disabled* key to `false`", type: :boolean def start proxy.start **option_kwds( :write ) end desc "stop", "Stop the proxy" include_options groups: [:stop] option :write, desc: "Set `launchd` *Disabled* key to `true`", type: :boolean def stop proxy.stop **option_kwds( :unload, groups: :stop ) end desc "restart", "Restart the proxy" include_options groups: [:stop] option :write, desc: "Set `launchd` *Disabled* key to `false`", type: :boolean def restart proxy.restart **option_kwds( :write, groups: :stop ) end end # module Locd::CLI::Proxy