Packages
avrora
0.18.0
0.30.2
0.30.1
0.30.0
0.29.2
0.29.1
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.2
0.24.1
0.24.0
0.23.0
0.22.0
0.21.1
0.21.0
0.20.0
0.19.0
0.18.1
0.18.0
0.17.0
0.16.0
0.15.0
0.14.1
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.5.1
0.5.0
0.4.0
0.3.1
0.3.0
0.2.2
0.2.1
0.2.0
0.1.1-beta
0.1.0-beta
An Elixir library for working with Avro messages conveniently. It supports local schema files and Confluent® schema registry.
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/avrora.reg.schema.ex
defmodule Mix.Tasks.Avrora.Reg.Schema do
use Mix.Task
@moduledoc """
Register either one schema or all schemas in the `Avrora.Config.schemas_path`
directory.
mix avrora.reg.schema [--all] [--name NAME] [--as NEW_NAME]
The search of the schemas will be performed under path configured in `schemas_path`
configuration option. One of either option must be given.
## Options
* `--name` - the full name of the schema to register (exclusive with `--all`)
* `--as` - the name which will be used to register schema (i.e subject)
* `--all` - register all found schemas
The `--as` option is possible to use only together with `--name`.
The `--name` option expects that given schema name will comply to
`Avrora.Storage.File` module rules.
For example, if the schema name is `io.confluent.Payment` it should be stored
as `<schemas path>/io/confluent/Payment.avsc`
## Usage
mix avrora.reg.schema --name io.confluent.Payment
mix avrora.reg.schema --name io.confluent.Payment --as MyCustomName
mix avrora.reg.schema --all
"""
alias Avrora.{Config, Utils}
@shortdoc "Register schema(s) in the Confluent Schema Registry"
@impl Mix.Task
def run(argv) do
{:ok, _} = Application.ensure_all_started(:avrora)
{:ok, _} = Avrora.start_link()
case argv do
["--all"] ->
[schemas_path(), "**", "*.avsc"]
|> Path.join()
|> Path.wildcard()
|> Enum.each(fn file_path ->
file_path
|> Path.relative_to(schemas_path())
|> Path.rootname()
|> String.replace("/", ".")
|> register()
end)
["--name", name] ->
name |> String.trim() |> register()
["--name", name, "--as", new_name] ->
name |> String.trim() |> register(as: String.trim(new_name))
_ ->
message = """
don't know how to handle `#{Enum.join(argv, " ")}'
please use #{IO.ANSI.yellow()}mix help avrora.reg.schema#{IO.ANSI.reset()} for help
"""
Mix.shell().error(message)
exit({:shutdown, 1})
end
end
defp register(name, opts \\ []) do
opts = Keyword.merge(opts, force: true)
case Utils.Registrar.register_schema_by_name(name, opts) do
{:ok, _} ->
message =
if Keyword.has_key?(opts, :as) do
"schema `#{name}' will be registered as `#{opts[:as]}'"
else
"schema `#{name}' will be registered"
end
Mix.shell().info(message)
{:error, error} ->
Mix.shell().error("schema `#{name}' will be skipped due to an error `#{error}'")
end
end
defp schemas_path, do: Config.self().schemas_path()
end