Skip to content

Commit

Permalink
VintageNet.info: show disabled interfaces too
Browse files Browse the repository at this point in the history
This is important to know now that power management applies to disabled
interfaces and also since it's good to know when someone explicitly
disables an interface.

This does a minor refactor of how things are printed as well, but the
result is almost identical.
  • Loading branch information
fhunleth committed Jul 24, 2020
1 parent cfdf139 commit 6d54e00
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
73 changes: 49 additions & 24 deletions lib/vintage_net/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,64 @@ defmodule VintageNet.Info do
IO.puts("VintageNet hasn't been loaded yet. Try again soon.")

version ->
IO.write(["VintageNet ", version, "\n\n"])
ifnames = interfaces_to_show()

do_info(opts)
IO.write([format_header(version), format_interfaces(ifnames, opts)])
end
end

defp do_info(opts) do
IO.write("""
defp format_header(version) do
"""
VintageNet #{version}
All interfaces: #{inspect(VintageNet.all_interfaces())}
Available interfaces: #{inspect(VintageNet.get(["available_interfaces"]))}
""")
ifnames = VintageNet.configured_interfaces()
"""
end

defp format_interfaces([], _opts) do
"No interfaces"
end

defp format_interfaces(ifnames, opts) do
Enum.map(ifnames, &format_interface(&1, opts))
end

defp interfaces_to_show() do
type = VintageNet.match(["interface", :_, "type"])

for {[_interface, ifname, _type], _value} <- type do
ifname
end
end

if ifnames == [] do
IO.puts("\nNo configured interfaces")
defp format_interface(ifname, opts) do
if VintageNet.get(["interface", ifname, "present"]) do
[
"Interface ",
ifname,
"\n",
format_if_attribute(ifname, "type", "Type"),
format_if_attribute(ifname, "present", "Present"),
format_if_attribute(ifname, "state", "State", true),
format_if_attribute(ifname, "connection", "Connection", true),
format_addresses(ifname),
" Configuration:\n",
format_config(ifname, " ", opts),
"\n"
]
else
ifnames
|> Enum.map(fn ifname ->
[
"\nInterface ",
ifname,
"\n",
format_if_attribute(ifname, "type", "Type"),
format_if_attribute(ifname, "present", "Present"),
format_if_attribute(ifname, "state", "State", true),
format_if_attribute(ifname, "connection", "Connection", true),
format_addresses(ifname),
" Configuration:\n",
format_config(ifname, " ", opts)
]
end)
|> IO.puts()
[
"Interface ",
ifname,
"\n",
format_if_attribute(ifname, "type", "Type"),
" Present: false\n",
" Configuration:\n",
format_config(ifname, " ", opts),
"\n"
]
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/vintage_net/info_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule VintageNet.InfoTest do

assert output =~ "All interfaces"
assert output =~ "Available interfaces"
assert output =~ "No configured interfaces"
assert output =~ "No interfaces"
end

test "info works with a configured interface" do
Expand Down

0 comments on commit 6d54e00

Please sign in to comment.