View Source GPGex (gpg_ex v1.0.0-alpha.2)

A simple wrapper to run GPG commands.

Tested on Linux with gpg (GnuPG) 2.2.27.

Warning

This is a pre-release version. As such, anything may change at any time, the public API should not be considered stable, and using a pinned version is recommended.

Configuration

You can configure the library with both the global_keystore key in the configuration and the keystore option in functions.

The keystore option in functions takes precedence over the global_keystore configuration, and if none of these are provided, your default GPG keystore (i.e. "homedir") will be used.

You can specify an optional directory to be passed to --homedir, and an optional filename to be passed to --keyring (in which case --no-default-keyring will be automatically added).

config :gpg_ex,
  global_keystore: %{
    path: "priv/global_gpg_homedir",
    keyring: "global_gpg_keyring.kbx"
  }

Note: the following configuration is used in the "Examples" section throughout this documentation:

config :gpg_ex,
  global_keystore: %{
    path: "/tmp/gpg_ex_keystore"
  }

Summary

Functions

Runs GPG with the given args.

Same as cmd/1 but raises a RuntimeError if the command fails.

Functions

@spec cmd([String.t()], keyword()) ::
  {:ok, [String.t()], [String.t()]}
  | {:error, [String.t()], [String.t()], [String.t()]}

Runs GPG with the given args.

Returns parsed status messages and stdout rows separately in a tuple.

Also returns the command arguments if it failed.

See gnupg/doc/DETAILS for a full list and description of statuses and their arguments.

Options

Examples

iex> {:ok, messages, stdout} = GPGex.cmd(["--recv-keys", "18D5DCA13E5D61587F552A1BDEB5A837B34DD01D"])
iex> messages
[
  "KEY_CONSIDERED 18D5DCA13E5D61587F552A1BDEB5A837B34DD01D 0",
  "IMPORTED DEB5A837B34DD01D GPGEx Test <spam@sherlox.io>",
  "IMPORT_OK 1 18D5DCA13E5D61587F552A1BDEB5A837B34DD01D",
  "IMPORT_RES 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0"
]
iex> stdout
[
  "key DEB5A837B34DD01D: public key \"GPGEx Test <spam@sherlox.io>\" imported",
  "Total number processed: 1",
  "imported: 1"
]

iex> GPGex.cmd(["--delete-keys", "18D5DCA13E5D61587F552A1BDEB5A837B34DD01D"])
{:ok, [], []}

iex> GPGex.cmd(["--recv-keys", "91C8AFC4674BF0963E7A90CEB7FFBE9D2DF23D67"])
{:error,
  ["FAILURE recv-keys 167772218"],
  ["keyserver receive failed: No data"],
  ["--homedir", "/tmp/gpg_ex_keystore", "--batch", "--status-fd=1", "--recv-keys", "91C8AFC4674BF0963E7A90CEB7FFBE9D2DF23D67"]
}
@spec cmd!([String.t()], keyword()) :: {[String.t()], [String.t()]}

Same as cmd/1 but raises a RuntimeError if the command fails.

Examples

iex> GPGex.cmd!(["--unknown-option"])
** (RuntimeError) GPG command 'gpg --homedir /tmp/gpg_ex_keystore --batch --status-fd=1 --unknown-option' failed with:
invalid option "--unknown-option"

iex> {_, _} = GPGex.cmd!(["--recv-keys", "18D5DCA13E5D61587F552A1BDEB5A837B34DD01D"])