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 Jul 31, 2020
1 parent a0666be commit 71f54a1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/vintage_net.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ defmodule VintageNet do
Valid options for `VintageNet.info/1`
* `:redact` - Whether to hide passwords and similar information from the output (defaults to `true`)
* `:return` - Skip printing and return the formatted chardata
"""
@type info_options :: {:redact, boolean()}

Expand Down
14 changes: 12 additions & 2 deletions lib/vintage_net/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ defmodule VintageNet.Info do
"""
@spec info([VintageNet.info_options()]) :: :ok
def info(opts \\ []) do
chardata = info_chardata(opts)

if opts[:return] == true do
to_string(chardata)
else
IO.write(chardata)
end
end

defp info_chardata(opts) do
case version() do
:not_loaded ->
IO.puts("VintageNet hasn't been loaded yet. Try again soon.")
"VintageNet hasn't been loaded yet. Try again soon."

version ->
ifnames = interfaces_to_show()

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

Expand Down
8 changes: 8 additions & 0 deletions test/vintage_net/info_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ defmodule VintageNet.InfoTest do
assert output =~ "No interfaces"
end

test "info can return chardata" do
output = Info.info(return: true)

assert output =~ "All interfaces"
assert output =~ "Available interfaces"
assert output =~ "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 71f54a1

Please sign in to comment.