Packages
ferricstore
0.2.0
0.11.14
0.11.12
0.11.11
0.11.10
0.11.9
0.11.8
0.11.7
0.11.6
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.1
0.9.0
0.8.0
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.2.0
0.1.0
FerricFlow durable workflows and queues with native-protocol storage, Raft durability, and Bitcask persistence.
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/ferricstore.keys.ex
defmodule Mix.Tasks.Ferricstore.Keys do
@moduledoc """
Lists keys stored in FerricStore, with optional glob pattern filtering.
## Usage
mix ferricstore.keys [pattern]
## Arguments
* `pattern` (optional) -- a glob pattern to filter keys. Supports `*`
(match any sequence of characters) and `?` (match exactly one character).
When omitted, all keys are listed.
## Examples
# List all keys
mix ferricstore.keys
# List keys matching a prefix
mix ferricstore.keys "user:*"
# List keys with a single-char wildcard
mix ferricstore.keys "k?"
## Output
Prints each matching key on its own line, followed by a summary count.
"""
use Mix.Task
@shortdoc "List keys with optional glob pattern filtering"
@doc """
Runs the keys task, listing keys that match the given pattern.
## Parameters
* `args` -- list of command-line arguments. The first element, if present,
is treated as a glob pattern.
"""
@spec run(list()) :: :ok
@impl Mix.Task
def run(args) do
ensure_started()
pattern = List.first(args)
ctx = FerricStore.Instance.get(:default)
all_keys = Ferricstore.Store.Router.keys(ctx)
matched =
if pattern do
regex = glob_to_regex(pattern)
Enum.filter(all_keys, &Regex.match?(regex, &1))
else
all_keys
end
sorted = Enum.sort(matched)
Enum.each(sorted, fn key ->
Mix.shell().info(key)
end)
if pattern do
Mix.shell().info("\n#{length(sorted)} key(s) matching \"#{pattern}\"")
else
Mix.shell().info("\n#{length(sorted)} key(s) total")
end
:ok
end
defp ensure_started do
Mix.Task.run("app.start")
end
@doc false
@spec glob_to_regex(binary()) :: Regex.t()
def glob_to_regex(pattern) do
regex_str =
pattern
|> String.graphemes()
|> Enum.map_join(&escape_glob_char/1)
Regex.compile!("^#{regex_str}$")
end
defp escape_glob_char("*"), do: ".*"
defp escape_glob_char("?"), do: "."
defp escape_glob_char(char), do: Regex.escape(char)
end