Skip to content

Commit be10d05

Browse files
committed
refactor: fix all Credo code readability and refactoring issues
- Remove parentheses from all zero-arity function definitions (33 functions) - Fix documentation typos (Cofnig -> Config) - Split long comment line in admin/request.ex - Refactor Enum.map + Enum.join to more efficient Enum.map_join - Reduce Credo issues from 36 to 4 (1 warning, 2 design suggestions, 1 complexity)
1 parent 2784baa commit be10d05

File tree

8 files changed

+44
-43
lines changed

8 files changed

+44
-43
lines changed

lib/caddy.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ defmodule Caddy do
2424
Set `caddy_bin` to the path of Caddy binary file and start `Caddy.Server`.
2525
2626
```
27-
Caddy.Cofnig.set_bin("/usr/bin/caddy")
27+
Caddy.Config.set_bin("/usr/bin/caddy")
2828
Caddy.restart_server()
2929
```
3030
3131
This will restart server automatically
3232
3333
```
34-
Caddy.Cofnig.set_bin!("/usr/bin/caddy")
34+
Caddy.Config.set_bin!("/usr/bin/caddy")
3535
```
3636
3737
## Config
@@ -56,7 +56,7 @@ defmodule Caddy do
5656
@doc """
5757
Restart Caddy Server
5858
"""
59-
def restart_server() do
59+
def restart_server do
6060
case Supervisor.restart_child(__MODULE__, Caddy.Server) do
6161
{:error, :running} ->
6262
Supervisor.terminate_child(__MODULE__, Caddy.Server)
@@ -76,8 +76,8 @@ defmodule Caddy do
7676
@spec start(binary()) :: :ignore | {:error, any()} | {:ok, pid()}
7777
def start(caddy_bin), do: start_link(caddy_bin: caddy_bin)
7878

79-
@spec start() :: :ignore | {:error, any()} | {:ok, pid()}
80-
def start() do
79+
@spec start :: :ignore | {:error, any()} | {:ok, pid()}
80+
def start do
8181
caddy_bin = System.find_executable("caddy")
8282
start_link(caddy_bin: caddy_bin)
8383
end
@@ -115,6 +115,6 @@ defmodule Caddy do
115115
defdelegate set_global(global), to: Caddy.ConfigProvider
116116
defdelegate set_additional(additionals), to: Caddy.ConfigProvider
117117
defdelegate set_site(name, site), to: Caddy.ConfigProvider
118-
defdelegate backup_config(), to: Caddy.ConfigProvider
119-
defdelegate restore_config(), to: Caddy.ConfigProvider
118+
defdelegate backup_config, to: Caddy.ConfigProvider
119+
defdelegate restore_config, to: Caddy.ConfigProvider
120120
end

lib/caddy/admin.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule Caddy.Admin do
2222
{:noreply, state}
2323
end
2424

25-
defp check_caddy_server() do
25+
defp check_caddy_server do
2626
%{"listen" => "unix/" <> _} = Caddy.Admin.Api.get_config("admin")
2727
rescue
2828
error ->

lib/caddy/admin/api.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defmodule Caddy.Admin.Api do
2323
GET /reverse_proxy/upstreams Returns the current status of the configured proxy upstreams
2424
```
2525
"""
26-
def api(), do: nil
26+
def api, do: nil
2727

2828
@doc """
2929
Get info from caddy server
@@ -96,7 +96,7 @@ defmodule Caddy.Admin.Api do
9696
@doc """
9797
Stops the active configuration and exits the process
9898
"""
99-
def stop() do
99+
def stop do
100100
start_time = System.monotonic_time()
101101

102102
case request_module().post("/stop", "", "application/json") do
@@ -140,7 +140,7 @@ defmodule Caddy.Admin.Api do
140140
end
141141
end
142142

143-
def get_config() do
143+
def get_config do
144144
start_time = System.monotonic_time()
145145

146146
case request_module().get("/config/") do
@@ -372,7 +372,7 @@ defmodule Caddy.Admin.Api do
372372
end
373373
end
374374

375-
def delete_config() do
375+
def delete_config do
376376
start_time = System.monotonic_time()
377377

378378
case request_module().delete("/config/") do
@@ -432,7 +432,7 @@ defmodule Caddy.Admin.Api do
432432
Check server health status
433433
"""
434434
@spec health_check() :: {:ok, map()} | {:error, binary()}
435-
def health_check() do
435+
def health_check do
436436
start_time = System.monotonic_time()
437437

438438
case request_module().get("/config/") do
@@ -469,7 +469,7 @@ defmodule Caddy.Admin.Api do
469469
Get detailed server info including version and uptime
470470
"""
471471
@spec server_info() :: {:ok, map()} | {:error, binary()}
472-
def server_info() do
472+
def server_info do
473473
start_time = System.monotonic_time()
474474

475475
case request_module().get("/") do

lib/caddy/admin/request.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ defmodule Caddy.Admin.Request do
120120
do_recv(socket)
121121
end
122122

123-
defp get_admin_sock() do
123+
defp get_admin_sock do
124124
Application.get_env(:caddy, :config)
125125
|> get_in(["admin", "listen"])
126126
|> String.replace(~r/^unix\//, "")
@@ -156,7 +156,8 @@ defmodule Caddy.Admin.Request do
156156
# Now we only have body left.
157157
# # Depending on headers here you may want to do different things.
158158
# # The response might be chunked, or upgraded in case you have attached to the container
159-
# # Now I can receive the response. Because of `:active, false} I need to explicitly ask for data, otherwise it gets send to the process as messages.
159+
# # Now I can receive the response. Because of `:active, false} I need to explicitly
160+
# # ask for data, otherwise it gets send to the process as messages.
160161
case :proplists.get_value(:"Content-Type", resp.headers) do
161162
"application/json" -> {:ok, resp, Jason.decode!(read_body(socket, resp))}
162163
_ -> {:ok, resp, read_body(socket, resp)}

lib/caddy/config.ex

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,65 +27,65 @@ defmodule Caddy.Config do
2727
defdelegate user_home, to: System
2828

2929
@doc "Get configurable base path for caddy files"
30-
def base_path() do
30+
def base_path do
3131
Application.get_env(:caddy, :base_path, Path.join(user_home(), ".local/share/caddy"))
3232
end
3333

3434
@doc "Get configurable priv path"
35-
def priv_path() do
35+
def priv_path do
3636
Application.get_env(:caddy, :priv_path, Application.app_dir(:caddy, "priv"))
3737
end
3838

3939
@doc "Get share path (base path)"
40-
def share_path(), do: base_path()
40+
def share_path, do: base_path()
4141

4242
@doc "Get etc path for configuration files"
43-
def etc_path() do
43+
def etc_path do
4444
Application.get_env(:caddy, :etc_path, Path.join(base_path(), "etc"))
4545
end
4646

4747
@doc "Get run path for runtime files"
48-
def run_path() do
48+
def run_path do
4949
Application.get_env(:caddy, :run_path, Path.join(base_path(), "run"))
5050
end
5151

5252
@doc "Get tmp path for temporary files"
53-
def tmp_path() do
53+
def tmp_path do
5454
Application.get_env(:caddy, :tmp_path, Path.join(base_path(), "tmp"))
5555
end
5656

5757
@doc "Get XDG config home path"
58-
def xdg_config_home() do
58+
def xdg_config_home do
5959
Application.get_env(:caddy, :xdg_config_home, Path.join(base_path(), "config"))
6060
end
6161

6262
@doc "Get XDG data home path"
63-
def xdg_data_home() do
63+
def xdg_data_home do
6464
Application.get_env(:caddy, :xdg_data_home, Path.join(base_path(), "data"))
6565
end
6666

6767
@doc "Get environment file path"
68-
def env_file() do
68+
def env_file do
6969
Application.get_env(:caddy, :env_file, Path.join(etc_path(), "envfile"))
7070
end
7171

7272
@doc "Get init configuration file path"
73-
def init_file() do
73+
def init_file do
7474
Application.get_env(:caddy, :init_file, Path.join(etc_path(), "init.json"))
7575
end
7676

7777
@doc "Get PID file path"
78-
def pid_file() do
78+
def pid_file do
7979
Application.get_env(:caddy, :pid_file, Path.join(run_path(), "caddy.pid"))
8080
end
8181

8282
@doc "Get socket file path"
83-
def socket_file() do
83+
def socket_file do
8484
Application.get_env(:caddy, :socket_file, Path.join(run_path(), "caddy.sock"))
8585
end
8686

8787
@doc "Get saved JSON configuration file path"
88-
def saved_json_file() do
88+
def saved_json_file do
8989
Application.get_env(
9090
:caddy,
9191
:saved_json_file,
@@ -94,10 +94,10 @@ defmodule Caddy.Config do
9494
end
9595

9696
@doc false
97-
def paths(), do: [priv_path(), share_path(), etc_path(), run_path(), tmp_path()]
97+
def paths, do: [priv_path(), share_path(), etc_path(), run_path(), tmp_path()]
9898

9999
@doc false
100-
def ensure_path_exists() do
100+
def ensure_path_exists do
101101
paths()
102102
|> Enum.reduce_while(true, fn path, _acc ->
103103
case File.mkdir_p(path) do
@@ -214,7 +214,7 @@ defmodule Caddy.Config do
214214

215215
@doc "Get backup file path"
216216
@spec backup_json_file() :: Path.t()
217-
def backup_json_file() do
217+
def backup_json_file do
218218
Application.get_env(
219219
:caddy,
220220
:backup_json_file,
@@ -232,7 +232,7 @@ defmodule Caddy.Config do
232232
233233
#{Enum.join(additional, "\n\n")}
234234
235-
#{Enum.map(sites, fn
235+
#{Enum.map_join(sites, "\n\n", fn
236236
{name, site} when is_binary(site) -> """
237237
## #{name}
238238
#{name} {
@@ -246,7 +246,7 @@ defmodule Caddy.Config do
246246
}
247247
"""
248248
_ -> ""
249-
end) |> Enum.join("\n\n")}
249+
end)}
250250
"""
251251
end
252252

@@ -427,7 +427,7 @@ defmodule Caddy.Config do
427427
end
428428

429429
@doc false
430-
def init_env() do
430+
def init_env do
431431
[
432432
{"HOME", share_path()},
433433
{"XDG_CONFIG_HOME", xdg_config_home()},

lib/caddy/config_provider.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule Caddy.ConfigProvider do
4141

4242
@doc "Get current configuration"
4343
@spec get_config() :: Config.t()
44-
def get_config() do
44+
def get_config do
4545
start_time = System.monotonic_time()
4646
config = Agent.get(__MODULE__, & &1)
4747
duration = System.monotonic_time() - start_time
@@ -109,7 +109,7 @@ defmodule Caddy.ConfigProvider do
109109

110110
@doc "Backup current configuration"
111111
@spec backup_config() :: :ok | {:error, term()}
112-
def backup_config() do
112+
def backup_config do
113113
config = get_config()
114114
backup_file = Config.backup_json_file()
115115
start_time = System.monotonic_time()
@@ -140,7 +140,7 @@ defmodule Caddy.ConfigProvider do
140140

141141
@doc "Restore configuration from backup"
142142
@spec restore_config() :: {:ok, Config.t()} | {:error, term()}
143-
def restore_config() do
143+
def restore_config do
144144
backup_file = Config.backup_json_file()
145145
start_time = System.monotonic_time()
146146

@@ -171,7 +171,7 @@ defmodule Caddy.ConfigProvider do
171171

172172
@doc "Save current configuration"
173173
@spec save_config() :: :ok | {:error, term()}
174-
def save_config() do
174+
def save_config do
175175
config = get_config()
176176
start_time = System.monotonic_time()
177177

lib/caddy/server.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ defmodule Caddy.Server do
102102
Get Caddyfile content of the current running server
103103
"""
104104
@spec get_caddyfile() :: binary()
105-
def get_caddyfile() do
105+
def get_caddyfile do
106106
Path.expand("Caddyfile", Config.etc_path()) |> File.read!()
107107
end
108108

109-
defp bootstrap() do
109+
defp bootstrap do
110110
Logger.debug("Caddy Server bootstrap")
111111
start_time = System.monotonic_time()
112112

@@ -216,7 +216,7 @@ defmodule Caddy.Server do
216216
end
217217
end
218218

219-
defp cleanup_pidfile() do
219+
defp cleanup_pidfile do
220220
pidfile = Config.pid_file()
221221

222222
if pidfile |> File.exists?() do

lib/caddy/telemetry.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ defmodule Caddy.Telemetry do
101101
Returns list of all Caddy telemetry events.
102102
"""
103103
@spec list_events() :: list()
104-
def list_events() do
104+
def list_events do
105105
[
106106
[:caddy, :config, :set],
107107
[:caddy, :config, :get],

0 commit comments

Comments
 (0)