Current section
Files
Jump to
Current section
Files
lib/kuu.ex
defmodule Kuu do
@moduledoc """
Elixir binding for the kuu CLI-argument-definition spec (early placeholder).
kuu is a language-agnostic specification (https://github.com/kawaz/kuu).
This version delegates to the `kuu-cli` reference binary found on PATH;
a native implementation is planned to grow under this name.
"""
@version Mix.Project.config()[:version]
def version, do: @version
@doc "Run kuu-cli with raw arguments. Returns {:ok, stdout} | {:error, reason}."
def run(args) when is_list(args) do
case System.find_executable("kuu-cli") do
nil ->
{:error,
"kuu-cli not found on PATH. Install it from https://github.com/kawaz/kuu-cli " <>
"(this early version of the kuu package delegates to the reference CLI)"}
exe ->
case System.cmd(exe, args, stderr_to_stdout: false) do
{out, 0} -> {:ok, out}
{err, _} -> {:error, "kuu-cli failed: #{err}"}
end
end
end
@doc "Parse args against a kuu wire-format definition (JSON string)."
def parse(definition_json, args) when is_binary(definition_json) and is_list(args) do
path = Path.join(System.tmp_dir!(), "kuu-def-#{System.unique_integer([:positive])}.json")
File.write!(path, definition_json)
try do
run(["parse", path, "--" | args])
after
File.rm(path)
end
end
end