Current section
Files
Jump to
Current section
Files
lib/cli.ex
defmodule MarsExplorer.CLI do
alias MarsExplorer.{Exploration, Probe}
@moduledoc """
Module responsible for Mars Explorer CLI
"""
@doc """
Receives the name of a file containing all the instructions of an exploration, executes them and displays the result
## Example
iex> MarsExplorer.CLI.main(["instructions.txt"])
:ok
1 3 N
5 1 E
"""
@spec main([String.t()]) :: :ok
def main([file_name]) do
{:ok, _pid} = MarsExplorer.ExplorationsController.start_link([])
if File.exists?(file_name) do
explorations =
File.stream!(file_name)
|> Enum.map(&String.trim/1)
|> MarsExplorer.InstructionsInterpreter.interpret()
|> MarsExplorer.InstructionsRunner.run()
MarsExplorer.ExplorationsController.clean_explorations()
Enum.each(explorations, &report_exploration/1)
else
IO.puts("The file #{file_name} does not exist")
end
end
def main([]) do
IO.puts("Usage: mars_explorer instructions.txt")
end
def main(_) do
IO.puts("Usage: mars_explorer instructions.txt")
end
defp report_exploration(%Exploration{
probe: %Probe{north: north, east: east, direction: direction}
}) do
direction_initial =
direction
|> Atom.to_string()
|> String.upcase()
|> String.first()
IO.puts("#{east} #{north} #{direction_initial}")
end
end