use anyhow::Context as _;
use reedline_repl_rs::clap::{Arg, ArgMatches, Command};
use reedline_repl_rs::Repl;
use s_macro::s;
use wildland_corex::LocalSecureStorage;
use super::sledlss::SledLss;
use crate::{match_context, Context};
#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_create(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
match_context!(context, lss => None, cargo => None)?;
let path = args
.get_one::<String>("path")
.context("path not provided")?;
context.set_lss(SledLss::new(s!(path)))?;
Ok(Some(s!("global lss instance created")))
}
#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_insert(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
let lss = match_context!(context, lss => Some(_))?;
let key = args.get_one::<String>("key").context("key not provided")?;
let value = args
.get_one::<String>("value")
.context("value not provided")?;
lss.insert(s!(key), s!(value))?;
Ok(Some(s!("key inserted")))
}
#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_get(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
let lss = match_context!(context, lss => Some(_))?;
let key = args.get_one::<String>("key").context("key not provided")?;
let value = lss.get(s!(key))?;
Ok(value)
}
#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_contains_key(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
let lss = match_context!(context, lss => Some(_))?;
let key = args.get_one::<String>("key").context("key not provided")?;
let value = lss.contains_key(s!(key))?;
Ok(Some(s!(value)))
}
#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_keys(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
let lss = match_context!(context, lss => Some(_))?;
let value = lss.keys()?;
Ok(Some(value.join(",")))
}
#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_remove(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
let lss = match_context!(context, lss => Some(_))?;
let key = args.get_one::<String>("key").context("key not provided")?;
let value = lss.remove(s!(key))?;
Ok(value)
}
#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_len(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
let lss = match_context!(context, lss => Some(_))?;
let value = lss.len()?;
Ok(Some(s!(value)))
}
#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_wipe(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
let lss = match_context!(context, lss => Some(_))?;
for each in lss.keys()? {
lss.remove(s!(each))?;
}
Ok(Some(s!("lss wiped")))
}
#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_is_empty(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
let lss = match_context!(context, lss => Some(_))?;
let value = lss.is_empty()?;
Ok(Some(s!(value)))
}
#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_wipe_clean(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
let lss = match_context!(context, lss => Some(_))?;
tracing::warn!("Wiping clean the local secure storage");
let keys = lss.keys()?;
for key in keys {
lss.remove(s!(key))?;
}
Ok(Some(s!()))
}
pub fn extend(repl: Repl<Context, anyhow::Error>) -> Repl<Context, anyhow::Error> {
repl.with_command(
Command::new("lss-insert")
.about("Insert a key/value pair into the local secure storage")
.arg(Arg::new("key").required(true))
.arg(Arg::new("value").required(true)),
h_lss_insert,
)
.with_command(
Command::new("lss-get")
.about("Get a value from the local secure storage")
.arg(Arg::new("key").required(true)),
h_lss_get,
)
.with_command(
Command::new("lss-create")
.about("Create or load local secure storage")
.arg(Arg::new("path").required(true)),
h_lss_create,
)
.with_command(
Command::new("lss-contains-key")
.about("Check if a key exists in the local secure storage")
.arg(Arg::new("key").required(true)),
h_lss_contains_key,
)
.with_command(
Command::new("lss-keys").about("Get all keys from the local secure storage"),
h_lss_keys,
)
.with_command(
Command::new("lss-remove")
.about("Remove a key from the local secure storage")
.arg(Arg::new("key").required(true)),
h_lss_remove,
)
.with_command(
Command::new("lss-len").about("Get the number of keys in the local secure storage"),
h_lss_len,
)
.with_command(
Command::new("lss-wipe").about("Wipe LSS content"),
h_lss_wipe,
)
.with_command(
Command::new("lss-is-empty").about("Check if the local secure storage is empty"),
h_lss_is_empty,
)
.with_command(
Command::new("lss-wipe-clean").about("wipe clean the local secure storage"),
h_lss_wipe_clean,
)
}