Skip to content

Commit

Permalink
service: prefer symbols over strings in DSL
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apainintheneck committed Sep 27, 2023
1 parent ddfa723 commit 7b1c990
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
20 changes: 14 additions & 6 deletions Library/Homebrew/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ def run_at_load(value = nil)
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|
Expand Down Expand Up @@ -580,8 +580,6 @@ def self.deserialize(api_hash)
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)]
Expand All @@ -600,12 +598,22 @@ def self.deserialize(api_hash)
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

Expand Down
12 changes: 6 additions & 6 deletions Library/Homebrew/test/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def stub_formula_with_service_sockets(sockets_var)
\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>
Expand All @@ -299,7 +299,7 @@ def stub_formula_with_service_sockets(sockets_var)

[
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)
Expand All @@ -310,7 +310,7 @@ def stub_formula_with_service_sockets(sockets_var)
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

Expand Down Expand Up @@ -339,7 +339,7 @@ def stub_formula_with_service_sockets(sockets_var)
\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>
Expand All @@ -350,7 +350,7 @@ def stub_formula_with_service_sockets(sockets_var)
\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>
Expand Down Expand Up @@ -1049,7 +1049,7 @@ def stub_formula_with_service_sockets(sockets_var)
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

Expand Down
4 changes: 2 additions & 2 deletions docs/Formula-Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 socket: "tcp://0.0.0.0:80", socket_tls: "tcp://0.0.0.0:443"
end
```
Expand Down

0 comments on commit 7b1c990

Please sign in to comment.