68 lines
1.7 KiB
Elixir
68 lines
1.7 KiB
Elixir
|
|
defmodule Mix.Tasks.Localspot.Import do
|
||
|
|
@moduledoc """
|
||
|
|
Imports businesses from a JSON file.
|
||
|
|
|
||
|
|
## Usage
|
||
|
|
|
||
|
|
mix localspot.import path/to/businesses.json
|
||
|
|
|
||
|
|
## JSON Format
|
||
|
|
|
||
|
|
See `Localspot.Businesses.Import` for the expected JSON format.
|
||
|
|
|
||
|
|
## Example
|
||
|
|
|
||
|
|
mix localspot.import priv/data/businesses.json
|
||
|
|
|
||
|
|
"""
|
||
|
|
use Mix.Task
|
||
|
|
|
||
|
|
@shortdoc "Imports businesses from a JSON file"
|
||
|
|
|
||
|
|
@impl Mix.Task
|
||
|
|
def run(args) do
|
||
|
|
case args do
|
||
|
|
[path] ->
|
||
|
|
Mix.Task.run("app.start")
|
||
|
|
import_file(path)
|
||
|
|
|
||
|
|
_ ->
|
||
|
|
Mix.shell().error("Usage: mix localspot.import <path_to_json_file>")
|
||
|
|
exit({:shutdown, 1})
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
defp import_file(path) do
|
||
|
|
Mix.shell().info("Importing businesses from #{path}...")
|
||
|
|
|
||
|
|
case Localspot.Businesses.Import.from_file(path) do
|
||
|
|
{:ok, %{imported: imported, errors: errors}} ->
|
||
|
|
Mix.shell().info("Successfully imported #{imported} business(es)")
|
||
|
|
|
||
|
|
if length(errors) > 0 do
|
||
|
|
Mix.shell().info("#{length(errors)} error(s) occurred:")
|
||
|
|
|
||
|
|
Enum.each(errors, fn {:error, index, reason} ->
|
||
|
|
Mix.shell().error(" - Row #{index}: #{inspect(reason)}")
|
||
|
|
end)
|
||
|
|
end
|
||
|
|
|
||
|
|
{:error, {:file_error, reason}} ->
|
||
|
|
Mix.shell().error("Could not read file: #{inspect(reason)}")
|
||
|
|
exit({:shutdown, 1})
|
||
|
|
|
||
|
|
{:error, {:json_error, reason}} ->
|
||
|
|
Mix.shell().error("Invalid JSON: #{inspect(reason)}")
|
||
|
|
exit({:shutdown, 1})
|
||
|
|
|
||
|
|
{:error, {:invalid_format, message}} ->
|
||
|
|
Mix.shell().error("Invalid format: #{message}")
|
||
|
|
exit({:shutdown, 1})
|
||
|
|
|
||
|
|
{:error, reason} ->
|
||
|
|
Mix.shell().error("Import failed: #{inspect(reason)}")
|
||
|
|
exit({:shutdown, 1})
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|