feat: add emqx_config_helper

This commit is contained in:
x1001100011 2021-07-18 22:00:34 -07:00
parent ef4c30b2fd
commit 4c1e03bbcd
10 changed files with 140 additions and 1 deletions

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

26
apps/emqx_config_helper/.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/
# If you run "mix test --cover", coverage assets end up here.
/cover/
# The directory Mix downloads your dependencies sources to.
/deps/
# Where third-party dependencies like ExDoc output generated docs.
/doc/
# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
# Ignore package tarball (built via "mix hex.build").
emqx_config_helper-*.tar
# Temporary files, for example, from tests.
/tmp/

View File

@ -0,0 +1,21 @@
# EmqxConfigHelper
**TODO: Add description**
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `emqx_config_helper` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:emqx_config_helper, "~> 0.1.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/emqx_config_helper](https://hexdocs.pm/emqx_config_helper).

View File

@ -0,0 +1,2 @@
defmodule EmqxConfigHelper do
end

View File

@ -0,0 +1,35 @@
defmodule EmqxConfigHelper.Hocon do
def hocon(_application, options) do
{:ok, config} =
options
|> Keyword.fetch!(:config_file)
|> List.wrap()
|> Enum.map(&to_charlist/1)
|> :hocon.files(%{format: :richmap})
options
|> Keyword.fetch!(:schema_module)
|> :hocon_schema.check(config, %{atom_key: true, return_plain: true})
|> IO.inspect
|> Enum.each(fn {application, envs} ->
Config.config(application, Enum.to_list(envs))
end)
end
end
defmodule EmqxConfigHelper.Cuttlefish do
def cuttlefish(application, options) do
config =
options
|> Keyword.fetch!(:config_file)
|> :cuttlefish_conf.file()
application
|> :code.priv_dir()
|> Path.join("#{application}.schema")
|> List.wrap()
|> :cuttlefish_schema.files()
|> :cuttlefish_generator.map(config)
|> Enum.each(fn {application, envs} -> Config.config(application, envs) end)
end
end

View File

@ -0,0 +1,30 @@
defmodule EmqxConfigHelper.MixProject do
use Mix.Project
def project do
[
app: :emqx_config_helper,
version: "0.1.0",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
lockfile: "../../mix.lock",
elixir: "~> 1.12",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
def application do
[
extra_applications: [:logger]
]
end
defp deps do
[
{:cuttlefish, github: "emqx/cuttlefish", tag: "v4.0.1"},
{:hocon, github: "emqx/hocon"}
]
end
end

View File

@ -0,0 +1,8 @@
defmodule EmqxConfigHelperTest do
use ExUnit.Case
doctest EmqxConfigHelper
test "greets the world" do
assert EmqxConfigHelper.hello() == :world
end
end

View File

@ -0,0 +1 @@
ExUnit.start()

View File

@ -100,6 +100,10 @@ defmodule EmqxReleaseHelper.Applications do
overlay :plugin overlay :plugin
end end
application :emqx_config_helper do
start_type :load
end
application :emqx_exhook, %{release_type: :cloud} do application :emqx_exhook, %{release_type: :cloud} do
start_type :load start_type :load
overlay :plugin overlay :plugin

View File

@ -1,5 +1,13 @@
import Config import Config
import EmqxConfigHelper.Hocon
import EmqxConfigHelper.Cuttlefish
File.cwd! |> IO.inspect hocon :emqx_prometheus,
schema_module: :emqx_prometheus_schema,
config_file: "etc/plugins/emqx_prometheus.conf"
cuttlefish :emqx_sn,
schema_file: "emqx_sn.schema",
config_file: "etc/plugins/emqx_sn.conf"
config :mnesia, dir: '/tmp/mnesia' config :mnesia, dir: '/tmp/mnesia'