Packages

CLI for the Ontogen version control system for RDF datasets.

Current section

Files

Jump to
ontogen_cli lib ontogen cli commands init.ex
Raw

lib/ontogen/cli/commands/init.ex

defmodule Ontogen.CLI.Commands.Init do
alias Ontogen.CLI.Helper
alias Ontogen.Config.Generator
use Ontogen.CLI.Command,
name: :init,
about: "Creates the configuration files of a new Ontogen repository",
args: [],
options: [
store_adapter: [
value_name: "STORE_ADAPTER",
long: "--adapter",
help: "Name of the store adapter",
parser: :string,
required: false
],
directory: [
value_name: "DIRECTORY",
long: "--directory",
help:
"Base directory of the repository (within which the .ontogen/ subdirectory is created)",
parser: :string,
required: false
],
template: [
value_name: "TEMPLATE_DIRECTORY",
long: "--template",
help: "Config template directory to be used to generate the local config",
parser: :string,
required: false
]
]
@ontogen_dir ".ontogen"
@impl true
def call(%{}, options, _flags, []) do
with {:ok, directory} <- setup_directory(options),
:ok <- generate_config(directory, options) do
File.cd!(directory, fn -> Ontogen.Bog.create_salt_base_path() end)
success("Initialized empty Ontogen repository in #{directory}")
end
end
defp ontogen_dir(dir), do: Path.join(dir, @ontogen_dir)
defp setup_directory(options) do
project_dir = options[:directory] || File.cwd!()
if project_dir |> ontogen_dir() |> File.exists?() do
{:error, "Already initialized Ontogen repository found at #{project_dir}"}
else
with :ok <- project_dir |> ontogen_dir() |> File.mkdir_p() do
{:ok, project_dir}
end
end
end
defp generate_config(directory, options) do
with {:ok, adapter} <- Helper.to_adapter(options[:store_adapter]) do
opts =
if template = options[:template] do
[template_dir: template]
else
[]
end
|> Keyword.put(:adapter, adapter)
directory
|> Path.join(Ontogen.Config.Loader.local_path())
|> Generator.generate(opts)
end
end
end