Skip to content

Commit 52d0429

Browse files
committed
chore: split large blocks into functions to help readability
1 parent bb6985d commit 52d0429

File tree

7 files changed

+169
-149
lines changed

7 files changed

+169
-149
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This does not (and cannot) work with Homebrew on Linux (so don't file Linux issu
1515
## Usage
1616

1717
See [the `brew services` section of the `brew man` output](https://docs.brew.sh/Manpage#services-subcommand) or `brew services --help`.
18+
To specify a plist file use `brew services <command> <formula> --file=<file>`.
1819

1920

2021
## Tests

cmd/services.rb

+45-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def services_args
3333
[`sudo`] `brew services cleanup`:
3434
Remove all unused services.
3535
EOS
36+
flag "--file=", description: "Use the plist file from this location to start or run the service."
3637
switch "--all", description: "Run <subcommand> on all services."
3738
end
3839
end
@@ -42,9 +43,52 @@ def services
4243

4344
raise UsageError, "`brew services` is supported only on macOS!" unless OS.mac?
4445

46+
# pbpaste's exit status is a proxy for detecting the use of reattach-to-user-namespace
47+
raise UsageError, "`brew services` cannot run under tmux!" if ENV["TMUX"] && !quiet_system("/usr/bin/pbpaste")
48+
4549
# Keep this after the .parse to keep --help fast.
4650
require_relative "../lib/services_cli"
4751

48-
Homebrew::ServicesCli.run!(args)
52+
# Parse arguments.
53+
subcommand, formula, custom_plist, = args.named
54+
55+
if custom_plist.present?
56+
odeprecated "with file as last argument", "`--file=` to specify a plist file"
57+
else
58+
custom_plist = args.file
59+
end
60+
61+
if ["list", "cleanup"].include?(subcommand)
62+
raise UsageError, "The `#{subcommand}` subcommand does not accept a formula argument!" if formula
63+
raise UsageError, "The `#{subcommand}` subcommand does not accept the --all argument!" if args.all?
64+
end
65+
66+
target = if args.all?
67+
available_services
68+
elsif formula
69+
Service.new(Formulary.factory(formula))
70+
end
71+
72+
# Dispatch commands and aliases.
73+
case subcommand.presence
74+
when nil, "list", "ls"
75+
Homebrew::ServicesCli.list
76+
when "cleanup", "clean", "cl", "rm"
77+
Homebrew::ServicesCli.cleanup
78+
when "restart", "relaunch", "reload", "r"
79+
Homebrew::ServicesCli.check(target) &&
80+
Homebrew::ServicesCli.restart(target, custom_plist, verbose: args.verbose?)
81+
when "run"
82+
Homebrew::ServicesCli.check(target) &&
83+
Homebrew::ServicesCli.run(target)
84+
when "start", "launch", "load", "s", "l"
85+
Homebrew::ServicesCli.check(target) &&
86+
Homebrew::ServicesCli.start(target, custom_plist, verbose: args.verbose?)
87+
when "stop", "unload", "terminate", "term", "t", "u"
88+
Homebrew::ServicesCli.check(target) &&
89+
Homebrew::ServicesCli.stop(target)
90+
else
91+
raise UsageError, "unknown subcommand: `#{subcommand}`"
92+
end
4993
end
5094
end

lib/service.rb

+1-10
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,11 @@ def exit_code
118118
end
119119

120120
# Generate that plist file, dude.
121-
def generate_plist(data = nil, args:)
121+
def generate_plist(data = nil)
122122
data ||= plist.file? ? plist : formula.plist
123123

124124
if data.respond_to?(:file?) && data.file?
125125
data = data.read
126-
elsif data.respond_to?(:keys) && data.key?(:url)
127-
require "open-uri"
128-
data = URI.parse(data).read
129126
elsif !data
130127
odie "Could not read the plist for `#{name}`!"
131128
end
@@ -140,12 +137,6 @@ def generate_plist(data = nil, args:)
140137
data = data.gsub(%r{(<key>UserName</key>\s*<string>)[^<]*(</string>)}, "")
141138
end
142139

143-
if args.verbose?
144-
ohai "Generated plist for #{formula.name}:"
145-
puts " #{data.gsub("\n", "\n ")}"
146-
puts
147-
end
148-
149140
data
150141
end
151142

0 commit comments

Comments
 (0)