diff --git a/lib/towerops/billing/billing_notifier.ex b/lib/towerops/billing/billing_notifier.ex index 748b4de7..02ba4275 100644 --- a/lib/towerops/billing/billing_notifier.ex +++ b/lib/towerops/billing/billing_notifier.ex @@ -12,6 +12,7 @@ defmodule Towerops.Billing.BillingNotifier do alias Towerops.Mailer alias Towerops.Organizations + alias Towerops.Result require Logger @@ -84,7 +85,7 @@ defmodule Towerops.Billing.BillingNotifier do end) # Return success if at least one email was sent - if Enum.any?(results, &match?({:ok, _}, &1)) do + if Enum.any?(results, &Result.ok?/1) do {:ok, results} else {:error, :all_emails_failed} diff --git a/lib/towerops/organizations.ex b/lib/towerops/organizations.ex index 29da6f5f..8a072faf 100644 --- a/lib/towerops/organizations.ex +++ b/lib/towerops/organizations.ex @@ -18,6 +18,7 @@ defmodule Towerops.Organizations do alias Towerops.Organizations.Policy alias Towerops.Organizations.SubscriptionLimits alias Towerops.Repo + alias Towerops.Result alias Towerops.Sites.Site ## Organizations @@ -35,10 +36,7 @@ defmodule Towerops.Organizations do |> Enum.to_list() end |> Repo.transaction() - |> case do - {:ok, ids} -> ids - {:error, _} -> [] - end + |> Result.unwrap_or([]) end @doc """ diff --git a/lib/towerops/proto/wire.ex b/lib/towerops/proto/wire.ex index ee4f12c8..5ab7c4a3 100644 --- a/lib/towerops/proto/wire.ex +++ b/lib/towerops/proto/wire.ex @@ -8,6 +8,8 @@ defmodule Towerops.Proto.Wire do import Bitwise + alias Towerops.Result + # Wire type constants @wire_varint 0 @wire_64bit 1 @@ -179,10 +181,7 @@ defmodule Towerops.Proto.Wire do """ @spec skip_field(0..5, binary()) :: {:ok, binary()} | {:error, wire_error()} def skip_field(@wire_varint, data) do - case decode_varint(data) do - {:ok, {_value, rest}} -> {:ok, rest} - {:error, e} -> {:error, e} - end + Result.map(decode_varint(data), fn {_value, rest} -> rest end) end def skip_field(@wire_64bit, data) when byte_size(data) >= 8 do @@ -193,10 +192,7 @@ defmodule Towerops.Proto.Wire do def skip_field(@wire_64bit, _data), do: {:error, :unexpected_eof} def skip_field(@wire_length_delimited, data) do - case decode_bytes(data) do - {:ok, {_field_data, rest}} -> {:ok, rest} - {:error, e} -> {:error, e} - end + Result.map(decode_bytes(data), fn {_field_data, rest} -> rest end) end def skip_field(@wire_32bit, data) when byte_size(data) >= 4 do diff --git a/lib/towerops/security/four_oh_four_tracker.ex b/lib/towerops/security/four_oh_four_tracker.ex index 6f10d8ae..0b09ed10 100644 --- a/lib/towerops/security/four_oh_four_tracker.ex +++ b/lib/towerops/security/four_oh_four_tracker.ex @@ -6,6 +6,7 @@ defmodule Towerops.Security.FourOhFourTracker do When an IP hits 5+ unique 404s, a ban is created/escalated. """ + alias Towerops.Result alias Towerops.Security.BruteForce require Logger @@ -59,10 +60,7 @@ defmodule Towerops.Security.FourOhFourTracker do conn = Towerops.Redix try do - case Redix.command(conn, ["SCARD", key]) do - {:ok, count} -> count - {:error, _} -> 0 - end + Result.unwrap_or(Redix.command(conn, ["SCARD", key]), 0) catch :exit, _ -> 0 end diff --git a/lib/towerops/snmp/wireless_client_discovery.ex b/lib/towerops/snmp/wireless_client_discovery.ex index de1798c9..e3c9220b 100644 --- a/lib/towerops/snmp/wireless_client_discovery.ex +++ b/lib/towerops/snmp/wireless_client_discovery.ex @@ -8,6 +8,7 @@ defmodule Towerops.Snmp.WirelessClientDiscovery do - MikroTik RouterOS: mtxrWlRtabTable """ + alias Towerops.Result alias Towerops.Snmp.Client alias Towerops.Snmp.WirelessClientDiscovery.Parser @@ -186,10 +187,7 @@ defmodule Towerops.Snmp.WirelessClientDiscovery do # --- Helpers --- defp walk_or_empty(client_opts, oid) do - case Client.walk(client_opts, oid) do - {:ok, entries} -> {:ok, entries} - {:error, _} -> {:ok, %{}} - end + {:ok, Result.unwrap_or(Client.walk(client_opts, oid), %{})} end # Group walk results by row index, extracting the index suffix from OID keys diff --git a/lib/towerops/topology.ex b/lib/towerops/topology.ex index 484abf7c..73644c82 100644 --- a/lib/towerops/topology.ex +++ b/lib/towerops/topology.ex @@ -10,6 +10,7 @@ defmodule Towerops.Topology do alias Towerops.Devices alias Towerops.Devices.Device alias Towerops.Repo + alias Towerops.Result alias Towerops.Snmp.ArpEntry alias Towerops.Snmp.Device, as: SnmpDevice alias Towerops.Snmp.Interface @@ -388,7 +389,7 @@ defmodule Towerops.Topology do |> Enum.map(&upsert_grouped_evidence(&1, device.id, lookup, now)) |> Enum.reject(&(&1 == :skip)) - has_changes = Enum.any?(results, &match?({:ok, _}, &1)) + has_changes = Enum.any?(results, &Result.ok?/1) # Auto-inference disabled - device type is now manual-only # maybe_update_device_role(device) @@ -1111,7 +1112,7 @@ defmodule Towerops.Topology do {:ok, %{neighbors: neighbors}} -> now = DateTime.utc_now() results = Enum.map(neighbors, &upsert_device_neighbor(device_id, &1, now)) - success_count = Enum.count(results, &match?({:ok, _}, &1)) + success_count = Enum.count(results, &Result.ok?/1) {:ok, success_count} {:error, reason} = error ->