diff --git a/lib/toolshed.ex b/lib/toolshed.ex index b677d39..e0f891a 100644 --- a/lib/toolshed.ex +++ b/lib/toolshed.ex @@ -22,6 +22,7 @@ defmodule Toolshed do * `date/0` - print out the current date and time * `dmesg/0` - print kernel messages (Nerves-only) * `exit/0` - exit out of an IEx session + * `fake_shell/0` - starts a sh like session that sends all lines to `cmd/1` * `fw_validate/0` - marks the current image as valid (check Nerves system if supported) * `geo/1` - print out a rough physical location * `grep/2` - print out lines that match a regular expression diff --git a/lib_src/core/fake_shell.ex b/lib_src/core/fake_shell.ex new file mode 100644 index 0000000..99823a8 --- /dev/null +++ b/lib_src/core/fake_shell.ex @@ -0,0 +1,56 @@ +# SPDX-FileCopyrightText: 2025 Marc Lainez +# +# SPDX-License-Identifier: Apache-2.0 +# +defmodule Toolshed.Core.FakeShell do + @prompt "fksh> " + + @doc """ + Start an interactive fake shell session. + + The shell reads lines from `IO.gets/1` and delegates command execution to + `Toolshed.cmd/1`. It is intended for manual use or lightweight integration + testing where an interactive prompt is convenient. + + Example + + iex> Toolshed.fake_shell() + Starting fake shell. Type 'exit' to quit. + fksh> echo hello + ... + + The function returns `:ok` when the user types `exit`. + """ + @spec fake_shell() :: :ok + def fake_shell() do + IO.puts("Starting fake shell. Type 'exit' to quit.") + fake_shell_loop() + end + + defp fake_shell_loop() do + case IO.gets(@prompt) do + :eof -> + :ok + + "exit\n" -> + :ok + + line -> + line + |> String.trim() + |> exec() + + fake_shell_loop() + end + end + + defp exec(""), do: :ok + + defp exec(cmdline) do + _ = Toolshed.cmd(cmdline) + :ok + rescue + e -> + IO.puts("error: #{inspect(e)}") + end +end diff --git a/test/toolshed_test.exs b/test/toolshed_test.exs index 367c348..d5606a4 100644 --- a/test/toolshed_test.exs +++ b/test/toolshed_test.exs @@ -12,6 +12,7 @@ defmodule ToolshedTest do cat: 1, cmd: 1, date: 0, + fake_shell: 0, geo: 0, geo: 1, grep: 2,