Skip to content

Commit

Permalink
use elixir-fortune
Browse files Browse the repository at this point in the history
  • Loading branch information
mnishiguchi committed Sep 8, 2023
1 parent 0ca0c32 commit f61e155
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 90 deletions.
40 changes: 20 additions & 20 deletions config/default_tips.txt → fortunes/tips
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
Compiling systems on linux or via `mix nerves.system.shell`? Don't forgot about
`make help` and other tips from the Buildroot manual (thanks @ericr3r)
https://buildroot.org/downloads/manual/manual.html#make-tips

%
Configuring the Elixir Logger to show SASL reports can help debug unexpected
GenServer restarts.
https://hexdocs.pm/logger/Logger.html#module-erlang-otp-integration

%
Erlinit is a small program that starts the Erlang VM on boot. It has many
options - especially for debugging startup issues.
https://hexdocs.pm/nerves/advanced-configuration.html#overriding-erlinit-config-from-mix-config

%
Get all of the default packages for starting a new Nerves project by depending
on `:nerves_pack` https://github.com/nerves-project/nerves_pack

%
Get some insights to Nerves internals with this high level overview of the
Nerves architecture and choice to use the BEAM VM https://youtu.be/VzOaSvTcvU4

%
Identify your Nerves devices using a unique ID that's already programmed into
the hardware using the boardid command. Details at
https://github.com/nerves-project/boardid/ and the boardid.config file.

%
Make small code changes to your running application by copy/pasting Elixir
source files at the IEx prompt.

%
Need more than Elixir logs? Run `dmesg` to see log messages from device drivers
and the Linux kernel. Nerves also routes kernel log messages to the Elixir
logger.

%
Nerves automatically reboots devices when the Erlang VM exits. This can make
some debugging harder, but you can easily disable it to let those sessions hang
for debugging

%
Nerves enables hardware watchdogs and connects them to Erlang's heart feature
to detect and recover from the Erlang VM hanging. See
https://embedded-elixir.com/post/2018-12-10-heart/ to learn more about this.

%
Nerves maintains a set of examples for common use-cases with devices. Things
like running a phoenix app, using SQLite, controlling GPIO, using Zig lang, and
more! https://github.com/nerves-project/nerves_examples

%
Nerves stores all BEAM files, the Erlang runtime and various support libraries
and apps in a compressed and read-only SquashFS filesystem. The writable
application partition is mounted at `/data`

%
Nerves stores all BEAM files, the Erlang runtime and various support
libraries/apps in a compressed and read-only SquashFS filesystem. Need to write
to disk? Use the application partition mounted R/W at `/data`

%
Run `Nerves.Runtime.revert` to go back to the previously loaded firmware.

%
See if someone has already implemented support for a sensor or other hardware
device that you have by checking https://elixir-circuits.github.io/.

%
Sometimes when compiling Nerves systems, order matters! Use `make
show-build-order` while in `mix nerves.system.shell` to see the compilation
order and make sure all your 🦆🦆 are in a row (thanks @ericr3r!)

%
Sometimes, you just need to see whats on your Nerves device's disk. Luckily,
there are a few ways to do that. You can even use VSCode!
https://embedded-elixir.com/post/2021-05-08-nerves-file-peeking/#sshfs

%
Use `RingLogger.next` to dump the current log buffer to screen. Use
`log_attach` from `Toolshed` to attach the current session to the Elixir logger
for live logs. https://hexdocs.pm/toolshed/Toolshed.Log.html#log_attach/1

%
Use `mix firmware.unpack` to decompress a local copy of your firmware on host
and inspect the files within before installing on device

%
Use the `RamoopsLogger` backend to store log messages in DRAM that can survive
unexpected reboots. https://github.com/smartrent/ramoops_logger

%
Want more traditional shell tools in your Nerves IEx? Use Toolshed! Its
included by default and has may familiar functions, such as `cat`, `ls`, or
even `weather` 🌤️ https://hexdocs.pm/toolshed
20 changes: 9 additions & 11 deletions lib/nerves_motd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule NervesMOTD do
"""

alias Nerves.Runtime.KV
alias NervesMOTD.Tips
alias NervesMOTD.Utils

@excluded_ifnames [~c"lo", ~c"lo0"]
Expand All @@ -26,8 +25,7 @@ defmodule NervesMOTD do
@type option() ::
{:logo, IO.ANSI.ansidata()}
| {:extra_rows, [row()]}
| {:show_tip, boolean()}
| {:extra_tips, [String.t()]}
| {:fortune, boolean()}

@typedoc """
One row of information
Expand All @@ -53,8 +51,7 @@ defmodule NervesMOTD do
an empty logo (`""`) to remove it completely.
* `:extra_rows` - a list of custom rows or a callback for returning rows.
The callback can be a 0-arity function reference or MFArgs tuple.
* `:show_tip` - a boolean flag to show the tip of the day.
* `:extra_tips` - additional custom tips as a list of strings.
* `:fortune` - a boolean flag to show fortune.
"""
@spec print([option()]) :: :ok
def print(opts \\ []) do
Expand All @@ -72,7 +69,7 @@ defmodule NervesMOTD do
"""
Nerves CLI help: https://hexdocs.pm/nerves/iex-with-nerves.html
""",
tips(opts)
fortune(opts)
]
|> IO.ANSI.format()
|> IO.puts()
Expand All @@ -90,13 +87,14 @@ defmodule NervesMOTD do
Keyword.get(opts, :logo, @logo)
end

@spec tips([option()]) :: IO.ANSI.ansidata()
defp tips(opts) do
if opts[:show_tip] do
@spec fortune([option()]) :: IO.ANSI.ansidata()
defp fortune(opts) do
if opts[:fortune] do
"""
=== Tip of the day ===
#{Tips.random(opts)}
------------------------------------------------------------------------
#{Fortune.random!()}
------------------------------------------------------------------------
"""
else
[]
Expand Down
29 changes: 0 additions & 29 deletions lib/nerves_motd/tips.ex

This file was deleted.

2 changes: 2 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule NervesMOTD.MixProject do
description: description(),
package: package(),
deps: deps(),
compilers: Mix.compilers() ++ [:strfile_compiler],
dialyzer: dialyzer(),
preferred_cli_env: %{
docs: :docs,
Expand All @@ -34,6 +35,7 @@ defmodule NervesMOTD.MixProject do
[
{:nerves_runtime, "~> 0.8"},
{:nerves_time, "~> 0.4", optional: true},
{:fortune, path: "../elixir-fortune"},
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.25", only: :docs, runtime: false},
Expand Down
24 changes: 0 additions & 24 deletions test/nerves_motd/tips_test.exs

This file was deleted.

12 changes: 6 additions & 6 deletions test/nerves_motd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ defmodule NervesMOTDTest do
assert capture_motd() == ""
end

describe "tip of the day" do
describe "fortune" do
setup _context do
NervesMOTD.MockRuntime
|> Mox.expect(:applications, 1, default_applications_code())
Expand All @@ -359,14 +359,14 @@ defmodule NervesMOTDTest do
:ok
end

@tip_of_the_day_regex ~r/Tip of the day/
@fortune_regex ~r/---*.*---*/

test "does not show tip by default" do
refute capture_motd() =~ @tip_of_the_day_regex
test "does not show fortune by default" do
refute capture_motd() =~ @fortune_regex
end

test "shows tip when enabled" do
assert capture_motd(show_tip: true) =~ @tip_of_the_day_regex
test "shows fortune when enabled" do
assert capture_motd(fortune: true) =~ @fortune_regex
end
end
end

0 comments on commit f61e155

Please sign in to comment.