Skip to content

Commit

Permalink
Support returning info data
Browse files Browse the repository at this point in the history
Helpful if someone wants to use the generated Info data, but doesn't
want to jump through the hoops of capturing IO
  • Loading branch information
jjcarstens committed Aug 3, 2022
1 parent d805024 commit 8f39a5c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/vintage_net.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ defmodule VintageNet do
@typedoc """
Valid options for `VintageNet.info/1`
* `:into` - Specify an implementation of `Collectable` to send the chardata
result into. (defaults to `IO.stream(:stdio, :line)`)
* `:redact` - Whether to hide passwords and similar information from the output (defaults to `true`)
"""
@type info_options :: {:redact, boolean()}
@type info_options :: [redact: boolean()]

@typedoc """
A VintageNet property
Expand Down Expand Up @@ -356,9 +358,11 @@ defmodule VintageNet do
Options include:
* `:into` - Specify an implementation of `Collectable` to send the chardata
result into. (defaults to `IO.stream(:stdio, :line)`)
* `:redact` - Set to `false` to print out passwords
"""
@spec info([info_options()]) :: :ok
@spec info(info_options()) :: :ok
defdelegate info(options \\ []), to: Info

@doc """
Expand Down
18 changes: 14 additions & 4 deletions lib/vintage_net/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ defmodule VintageNet.Info do
@doc """
Print the current network status
"""
@spec info([VintageNet.info_options()]) :: :ok
@spec info(VintageNet.info_options()) :: :ok
def info(opts \\ []) do
info_as_ansidata(opts)
|> IO.ANSI.format()
|> IO.puts()
end

@doc """
Format the information as ANSI data
"""
@spec info_as_ansidata(VintageNet.info_options()) :: IO.ANSI.ansidata()
def info_as_ansidata(opts \\ []) do
case vintage_net_app_info() do
{:ok, text} ->
ifnames = interfaces_to_show()
IO.write([text, format_header(), format_interfaces(ifnames, opts)])
[text, format_header(), format_interfaces(ifnames, opts)]

{:error, text} ->
IO.puts(text)
text
end
end

Expand All @@ -29,7 +39,7 @@ defmodule VintageNet.Info do
end

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

defp format_interfaces(ifnames, opts) do
Expand Down
10 changes: 10 additions & 0 deletions test/vintage_net/info_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ defmodule VintageNet.InfoTest do
assert output =~ "No interfaces"
end

test "info_as_ansidata can return ansidata" do
output = Info.info_as_ansidata()
assert is_list(output)
output_str = to_string(output)

assert output_str =~ "All interfaces"
assert output_str =~ "Available interfaces"
assert output_str =~ "No interfaces"
end

test "info works with a configured interface" do
capture_log(fn ->
:ok = VintageNet.configure("eth0", %{type: VintageNetTest.TestTechnology})
Expand Down

0 comments on commit 8f39a5c

Please sign in to comment.