# typed: strict # frozen_string_literal: true require "abstract_command" require "services/system" require "services/cli" require "services/commands/list" require "services/commands/cleanup" require "services/commands/info" require "services/commands/restart" require "services/commands/run" require "services/commands/start" require "services/commands/stop" require "services/commands/kill" module Homebrew module Cmd class Services < AbstractCommand cmd_args do usage_banner <<~EOS `services` [] Manage background services with macOS' `launchctl`(1) daemon manager or Linux's `systemctl`(1) service manager. If `sudo` is passed, operate on `/Library/LaunchDaemons` or `/usr/lib/systemd/system` (started at boot). Otherwise, operate on `~/Library/LaunchAgents` or `~/.config/systemd/user` (started at login). [`sudo`] `brew services` [`list`] (`--json`) (`--debug`): List information about all managed services for the current user (or root). Provides more output from Homebrew and `launchctl`(1) or `systemctl`(1) if run with `--debug`. [`sudo`] `brew services info` (|`--all`|`--json`): List all managed services for the current user (or root). [`sudo`] `brew services run` (|`--all`): Run the service without registering to launch at login (or boot). [`sudo`] `brew services start` (|`--all`|`--file=`): Start the service immediately and register it to launch at login (or boot). [`sudo`] `brew services stop` (|`--all`): Stop the service immediately and unregister it from launching at login (or boot). [`sudo`] `brew services kill` (|`--all`): Stop the service immediately but keep it registered to launch at login (or boot). [`sudo`] `brew services restart` (|`--all`): Stop (if necessary) and start the service immediately and register it to launch at login (or boot). [`sudo`] `brew services cleanup`: Remove all unused services. EOS flag "--file=", description: "Use the service file from this location to `start` the service." flag "--sudo-service-user=", description: "When run as root on macOS, run the service(s) as this user." flag "--max-wait=", description: "Wait at most this many seconds for `stop` to finish stopping a service. " \ "Omit this flag or set this to zero (0) seconds to wait indefinitely." switch "--all", description: "Run on all services." switch "--json", description: "Output as JSON." switch "--no-wait", description: "Don't wait for `stop` to finish stopping the service." conflicts "--max-wait=", "--no-wait" named_args max: 2 end sig { override.void } def run # pbpaste's exit status is a proxy for detecting the use of reattach-to-user-namespace if ENV["HOMEBREW_TMUX"] && (File.exist?("/usr/bin/pbpaste") && !quiet_system("/usr/bin/pbpaste")) raise UsageError, "`brew services` cannot run under tmux!" end # Keep this after the .parse to keep --help fast. require "utils" if !::Services::System.launchctl? && !::Services::System.systemctl? raise UsageError, "`brew services` is supported only on macOS or Linux (with systemd)!" end if (sudo_service_user = args.sudo_service_user) unless ::Services::System.root? raise UsageError, "`brew services` is supported only when running as root!" end unless ::Services::System.launchctl? raise UsageError, "`brew services --sudo-service-user` is currently supported only on macOS " \ "(but we'd love a PR to add Linux support)!" end ::Services::Cli.sudo_service_user = sudo_service_user end # Parse arguments. subcommand, formula, = args.named if [*::Services::Commands::List::TRIGGERS, *::Services::Commands::Cleanup::TRIGGERS].include?(subcommand) raise UsageError, "The `#{subcommand}` subcommand does not accept a formula argument!" if formula raise UsageError, "The `#{subcommand}` subcommand does not accept the --all argument!" if args.all? end if args.file if ::Services::Commands::Start::TRIGGERS.exclude?(subcommand) raise UsageError, "The `#{subcommand}` subcommand does not accept the --file= argument!" elsif args.all? raise UsageError, "The start subcommand does not accept the --all and --file= arguments at the same time!" end end opoo "The --all argument overrides provided formula argument!" if formula.present? && args.all? targets = if args.all? if subcommand == "start" ::Services::Formulae.available_services(loaded: false, skip_root: !::Services::System.root?) elsif subcommand == "stop" ::Services::Formulae.available_services(loaded: true, skip_root: !::Services::System.root?) else ::Services::Formulae.available_services end elsif formula [::Services::FormulaWrapper.new(Formulary.factory(formula))] else [] end # Exit successfully if --all was used but there is nothing to do return if args.all? && targets.empty? if ::Services::System.systemctl? ENV["DBUS_SESSION_BUS_ADDRESS"] = ENV.fetch("HOMEBREW_DBUS_SESSION_BUS_ADDRESS", nil) ENV["XDG_RUNTIME_DIR"] = ENV.fetch("HOMEBREW_XDG_RUNTIME_DIR", nil) end # Dispatch commands and aliases. case subcommand.presence when *::Services::Commands::List::TRIGGERS ::Services::Commands::List.run(json: args.json?) when *::Services::Commands::Cleanup::TRIGGERS ::Services::Commands::Cleanup.run when *::Services::Commands::Info::TRIGGERS ::Services::Commands::Info.run(targets, verbose: args.verbose?, json: args.json?) when *::Services::Commands::Restart::TRIGGERS ::Services::Commands::Restart.run(targets, verbose: args.verbose?) when *::Services::Commands::Run::TRIGGERS ::Services::Commands::Run.run(targets, verbose: args.verbose?) when *::Services::Commands::Start::TRIGGERS ::Services::Commands::Start.run(targets, args.file, verbose: args.verbose?) when *::Services::Commands::Stop::TRIGGERS max_wait = args.max_wait.to_f ::Services::Commands::Stop.run(targets, verbose: args.verbose?, no_wait: args.no_wait?, max_wait:) when *::Services::Commands::Kill::TRIGGERS ::Services::Commands::Kill.run(targets, verbose: args.verbose?) else raise UsageError, "unknown subcommand: `#{subcommand}`" end end end end end