Skip to content

Commit

Permalink
Add configured?/1
Browse files Browse the repository at this point in the history
  • Loading branch information
mnishiguchi authored and fhunleth committed Mar 31, 2024
1 parent c7789eb commit a7bbb73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/vintage_net_wifi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -995,4 +995,17 @@ defmodule VintageNetWiFi do
end

defp trim_orphan_backslash(s), do: s

@doc """
Check if a wlan network interface is configured.
"""
@spec configured?(map | binary) :: boolean
def configured?(wlan_ifname) when is_binary(wlan_ifname) do
wlan_ifname |> VintageNet.get_configuration() |> configured?()
end

def configured?(wlan_config) when wlan_config == %{type: VintageNetWiFi}, do: false
def configured?(%{vintage_net_wifi: %{networks: []}}), do: false
def configured?(%{vintage_net_wifi: %{networks: [_ | _]}}), do: true
def configured?(_), do: false
end
32 changes: 32 additions & 0 deletions test/vintage_net_wifi_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2661,4 +2661,36 @@ defmodule VintageNetWiFiTest do

assert expected_output == VintageNetWiFi.summarize_access_points(input)
end

test "Check if wlan is already configured" do
configured = %{
type: VintageNetWiFi,
ipv4: %{method: :dhcp},
vintage_net_wifi: %{
networks: [
%{
key_mgmt: :wpa_psk,
ssid: "IEEE",
psk: "F42C6FC52DF0EBEF9EBB4B90B38A5F902E83FE1B135A70E23AED762E9710A12E",
mode: :infrastructure
}
]
}
}

empty1 = %{
type: VintageNetWiFi
}

empty2 = %{
type: VintageNetWiFi,
vintage_net_wifi: %{networks: []},
ipv4: %{method: :disabled}
}

assert VintageNetWiFi.configured?(configured)
refute VintageNetWiFi.configured?(empty1)
refute VintageNetWiFi.configured?(empty2)
refute VintageNetWiFi.configured?(%{})
end
end

0 comments on commit a7bbb73

Please sign in to comment.