service: prefer symbols over strings in DSL

This is more in keeping with the other DSL methods and Ruby
convention along with the fact that these socket names are
just used internally by launchd.
This commit is contained in:
apainintheneck 2023-09-26 19:58:55 -07:00
parent ddfa723f77
commit 5fb9f90457
3 changed files with 22 additions and 14 deletions

View File

@ -190,15 +190,15 @@ module Homebrew
SOCKET_STRING_REGEX = %r{([a-z]+)://([a-z0-9.]+):([0-9]+)}i.freeze
sig {
params(value: T.nilable(T.any(String, T::Hash[String, String])))
.returns(T.nilable(T::Hash[String, T::Hash[Symbol, String]]))
params(value: T.nilable(T.any(String, T::Hash[Symbol, String])))
.returns(T.nilable(T::Hash[Symbol, T::Hash[Symbol, String]]))
}
def sockets(value = nil)
return @sockets if value.nil?
@sockets = case value
when String
{ "Listeners" => value }
{ listeners: value }
when Hash
value
end.transform_values do |socket_string|
@ -580,8 +580,6 @@ module Homebrew
raise ArgumentError, "Unexpected run command: #{api_hash["run"]}"
end
hash[:keep_alive] = api_hash["keep_alive"].transform_keys(&:to_sym) if api_hash.key?("keep_alive")
if api_hash.key?("environment_variables")
hash[:environment_variables] = api_hash["environment_variables"].to_h do |key, value|
[key.to_sym, replace_placeholders(value)]
@ -600,12 +598,22 @@ module Homebrew
hash[key.to_sym] = replace_placeholders(value)
end
%w[interval cron launch_only_once require_root restart_delay macos_legacy_timers sockets].each do |key|
%w[interval cron launch_only_once require_root restart_delay macos_legacy_timers].each do |key|
next if (value = api_hash[key]).nil?
hash[key.to_sym] = value
end
%w[sockets keep_alive].each do |key|
next unless (value = api_hash[key])
hash[key.to_sym] = if value.is_a?(Hash)
value.transform_keys(&:to_sym)
else
value
end
end
hash
end

View File

@ -281,7 +281,7 @@ describe Homebrew::Service do
\t<true/>
\t<key>Sockets</key>
\t<dict>
\t\t<key>Listeners</key>
\t\t<key>listeners</key>
\t\t<dict>
\t\t\t<key>SockFamily</key>
\t\t\t<string>IPv4v6</string>
@ -299,7 +299,7 @@ describe Homebrew::Service do
[
stub_formula_with_service_sockets("tcp://127.0.0.1:80"),
stub_formula_with_service_sockets({ "Listeners" => "tcp://127.0.0.1:80" }),
stub_formula_with_service_sockets({ listeners: "tcp://127.0.0.1:80" }),
].each do |f|
plist = f.service.to_plist
expect(plist).to eq(plist_expect)
@ -310,7 +310,7 @@ describe Homebrew::Service do
f = stub_formula do
service do
run [opt_bin/"beanstalkd", "test"]
sockets "Socket" => "tcp://0.0.0.0:80", "SocketTLS" => "tcp://0.0.0.0:443"
sockets socket: "tcp://0.0.0.0:80", socket_tls: "tcp://0.0.0.0:443"
end
end
@ -339,7 +339,7 @@ describe Homebrew::Service do
\t<true/>
\t<key>Sockets</key>
\t<dict>
\t\t<key>Socket</key>
\t\t<key>socket</key>
\t\t<dict>
\t\t\t<key>SockFamily</key>
\t\t\t<string>IPv4v6</string>
@ -350,7 +350,7 @@ describe Homebrew::Service do
\t\t\t<key>SockServiceName</key>
\t\t\t<string>80</string>
\t\t</dict>
\t\t<key>SocketTLS</key>
\t\t<key>socket_tls</key>
\t\t<dict>
\t\t\t<key>SockFamily</key>
\t\t\t<string>IPv4v6</string>
@ -1049,7 +1049,7 @@ describe Homebrew::Service do
run_type: :immediate,
working_dir: "/$HOME",
cron: "0 0 * * 0",
sockets: { "Listeners" => "tcp://0.0.0.0:80" },
sockets: { listeners: "tcp://0.0.0.0:80" },
}
end

View File

@ -1052,7 +1052,7 @@ The `sockets` method accepts a formatted socket definition as `<type>://<host>:<
Please note that sockets will be accessible on IPv4 and IPv6 addresses by default.
If you only need one socket and you don't care about the name (the default is `Listeners`):
If you only need one socket and you don't care about the name (the default is `listeners`):
```rb
service do
@ -1066,7 +1066,7 @@ If you need multiple sockets and/or you want to specify the name:
```rb
service do
run [opt_bin/"beanstalkd", "test"]
sockets "Socket" => "tcp://0.0.0.0:80", "SocketTLS" => "tcp://0.0.0.0:443"
sockets http: "tcp://0.0.0.0:80", https: "tcp://0.0.0.0:443"
end
```