Skip to content

Commit

Permalink
Merge pull request #625 from stefansundin/filter
Browse files Browse the repository at this point in the history
Improve behavior of `--all`
  • Loading branch information
MikeMcQuaid authored Feb 20, 2024
2 parents 404a744 + fdb56f1 commit 13d68f9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 10 additions & 1 deletion cmd/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,22 @@ def services
opoo "The --all argument overrides provided formula argument!" if formula.present? && args.all?

targets = if args.all?
::Service::Formulae.available_services
if subcommand == "start"
::Service::Formulae.available_services(loaded: false, skip_root: !::Service::System.root?)
elsif subcommand == "stop"
::Service::Formulae.available_services(loaded: true, skip_root: !::Service::System.root?)
else
::Service::Formulae.available_services
end
elsif formula
[::Service::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 ::Service::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)
Expand Down
17 changes: 11 additions & 6 deletions lib/service/formulae.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ module Service
module Formulae
module_function

# All available services
# All available services, with optional filters applied
# @private
def available_services
def available_services(loaded: nil, skip_root: false)
require "formula"

Formula.installed
.map { |formula| FormulaWrapper.new(formula) }
.select { |formula| formula.service? || formula.plist? }
.sort_by(&:name)
formulae = Formula.installed
.map { |formula| FormulaWrapper.new(formula) }
.select { |formula| formula.service? || formula.plist? }
.sort_by(&:name)

formulae = formulae.select { |formula| formula.loaded? == loaded } unless loaded.nil?
formulae = formulae.reject { |formula| formula.owner == "root" } if skip_root

formulae
end

# List all available services with status, user, and path to the file.
Expand Down

0 comments on commit 13d68f9

Please sign in to comment.