Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/toolshed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions lib_src/core/fake_shell.ex
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/toolshed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule ToolshedTest do
cat: 1,
cmd: 1,
date: 0,
fake_shell: 0,
geo: 0,
geo: 1,
grep: 2,
Expand Down