Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve boolean environment variables reading #4632

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 12 additions & 35 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,9 @@ data_dir = data_dir || persistent_cache_dir || System.get_env("DEFAULT_DATA_DIR"
persistent_cache_dir = persistent_cache_dir || data_dir

enable_email_verification =
config_dir
|> get_var_from_path_or_env("ENABLE_EMAIL_VERIFICATION", "false")
|> String.to_existing_atom()
get_bool_from_path_or_env(config_dir, "ENABLE_EMAIL_VERIFICATION", false)

is_selfhost =
config_dir
|> get_var_from_path_or_env("SELFHOST", "true")
|> String.to_existing_atom()
is_selfhost = get_bool_from_path_or_env(config_dir, "SELFHOST", true)

# by default, registration is disabled in self-hosted setups
disable_registration_default = to_string(is_selfhost)
Expand All @@ -274,15 +269,10 @@ custom_script_name =
config_dir
|> get_var_from_path_or_env("CUSTOM_SCRIPT_NAME", "script")

disable_cron =
config_dir
|> get_var_from_path_or_env("DISABLE_CRON", "false")
|> String.to_existing_atom()
disable_cron = get_bool_from_path_or_env(config_dir, "DISABLE_CRON", false)

log_failed_login_attempts =
config_dir
|> get_var_from_path_or_env("LOG_FAILED_LOGIN_ATTEMPTS", "false")
|> String.to_existing_atom()
get_bool_from_path_or_env(config_dir, "LOG_FAILED_LOGIN_ATTEMPTS", false)

websocket_url = get_var_from_path_or_env(config_dir, "WEBSOCKET_URL", "")

Expand Down Expand Up @@ -421,7 +411,7 @@ if config_env() in [:ce, :ce_dev, :ce_test] do
end

db_maybe_ipv6 =
if get_var_from_path_or_env(config_dir, "ECTO_IPV6") do
if get_bool_from_path_or_env(config_dir, "ECTO_IPV6") do
if config_env() in [:ce, :ce_dev, :ce_test] do
Logger.warning(
"ECTO_IPV6 is no longer necessary as all TCP connections now try IPv6 automatically with IPv4 fallback"
Expand Down Expand Up @@ -522,9 +512,7 @@ config :plausible, Plausible.HelpScout,
config :plausible, :imported,
max_buffer_size: get_int_from_path_or_env(config_dir, "IMPORTED_MAX_BUFFER_SIZE", 10_000)

maybe_ch_ipv6 =
get_var_from_path_or_env(config_dir, "ECTO_CH_IPV6", "false")
|> String.to_existing_atom()
maybe_ch_ipv6 = get_bool_from_path_or_env(config_dir, "ECTO_CH_IPV6", false)

if maybe_ch_ipv6 && config_env() in [:ce, :ce_dev, :ce_test] do
Logger.warning(
Expand Down Expand Up @@ -632,27 +620,22 @@ case mailer_adapter do
password: get_var_from_path_or_env(config_dir, "SMTP_USER_PWD"),
tls: :if_available,
allowed_tls_versions: [:tlsv1, :"tlsv1.1", :"tlsv1.2"],
ssl: get_var_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED") || false,
ssl: get_bool_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED", false),
retries: get_var_from_path_or_env(config_dir, "SMTP_RETRIES") || 2,
no_mx_lookups: get_var_from_path_or_env(config_dir, "SMTP_MX_LOOKUPS_ENABLED") || true
no_mx_lookups: get_bool_from_path_or_env(config_dir, "SMTP_MX_LOOKUPS_ENABLED", true)

"Bamboo.Mua" ->
config :plausible, Plausible.Mailer, adapter: Bamboo.Mua

# prevents common problems with Erlang's TLS v1.3
middlebox_comp_mode =
get_var_from_path_or_env(config_dir, "SMTP_MIDDLEBOX_COMP_MODE", "false")
get_bool_from_path_or_env(config_dir, "SMTP_MIDDLEBOX_COMP_MODE", false)

middlebox_comp_mode = String.to_existing_atom(middlebox_comp_mode)
config :plausible, Plausible.Mailer, ssl: [middlebox_comp_mode: middlebox_comp_mode]

if relay = get_var_from_path_or_env(config_dir, "SMTP_HOST_ADDR") do
port = get_int_from_path_or_env(config_dir, "SMTP_HOST_PORT", 587)

ssl_enabled =
if ssl_enabled = get_var_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED") do
String.to_existing_atom(ssl_enabled)
end
ssl_enabled = get_bool_from_path_or_env(config_dir, "SMTP_HOST_SSL_ENABLED")

protocol =
cond do
Expand Down Expand Up @@ -892,10 +875,7 @@ end

config :tzdata, :data_dir, Path.join(persistent_cache_dir || System.tmp_dir!(), "tzdata_data")

promex_disabled? =
config_dir
|> get_var_from_path_or_env("PROMEX_DISABLED", "true")
|> String.to_existing_atom()
promex_disabled? = get_bool_from_path_or_env(config_dir, "PROMEX_DISABLED", true)

config :plausible, Plausible.PromEx,
disabled: promex_disabled?,
Expand All @@ -922,10 +902,7 @@ if not is_selfhost do
config :plausible, Plausible.Site, default_ingest_threshold: site_default_ingest_threshold
end

s3_disabled? =
config_dir
|> get_var_from_path_or_env("S3_DISABLED", "true")
|> String.to_existing_atom()
s3_disabled? = get_bool_from_path_or_env(config_dir, "S3_DISABLED", true)

unless s3_disabled? do
s3_env = [
Expand Down
21 changes: 21 additions & 0 deletions lib/plausible/helpers/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,25 @@ defmodule Plausible.ConfigHelpers do
end
end
end

def get_bool_from_path_or_env(config_dir, var_name, default \\ nil) do
case get_var_from_path_or_env(config_dir, var_name) do
nil -> default
var -> parse_bool(var)
end
end

defp parse_bool(var) do
case String.downcase(var) do
t when t in ["1", "t", "true", "y", "yes", "on"] ->
true

f when f in ["0", "f", "false", "n", "no", "off"] ->
false

_ ->
raise ArgumentError,
"Invalid boolean value: #{inspect(var)}. Expected one of: 1, 0, t, f, true, false, y, n, on, off"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could extract those guard clauses and combine here, 'yes' and 'no' are missing

end
end
end
Loading