Add base64 output for admin query command.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
dasha_uwu
2025-07-08 23:49:18 +00:00
committed by Jason Volk
parent 34abe1cce4
commit 9dd2b28605
3 changed files with 15 additions and 2 deletions

1
Cargo.lock generated
View File

@@ -4837,6 +4837,7 @@ dependencies = [
name = "tuwunel_admin"
version = "1.2.0"
dependencies = [
"base64",
"clap",
"const-str",
"ctor",

View File

@@ -84,6 +84,7 @@ zstd_compression = [
]
[dependencies]
base64.workspace = true
clap.workspace = true
tuwunel-api.workspace = true
tuwunel-core.workspace = true

View File

@@ -1,5 +1,6 @@
use std::{borrow::Cow, collections::BTreeMap, ops::Deref, sync::Arc};
use base64::prelude::*;
use clap::Subcommand;
use futures::{FutureExt, Stream, StreamExt, TryStreamExt};
use tokio::time::Instant;
@@ -30,6 +31,10 @@ pub(crate) enum RawCommand {
/// Key
key: String,
/// Encode as base64
#[arg(long, short)]
base64: bool,
},
/// - Raw database delete (for string keys)
@@ -421,13 +426,19 @@ pub(super) async fn raw_del(&self, map: String, key: String) -> Result {
}
#[admin_command]
pub(super) async fn raw_get(&self, map: String, key: String) -> Result {
pub(super) async fn raw_get(&self, map: String, key: String, base64: bool) -> Result {
let map = self.services.db.get(&map)?;
let timer = Instant::now();
let handle = map.get(&key).await?;
let query_time = timer.elapsed();
let result = String::from_utf8_lossy(&handle);
let result = if base64 {
BASE64_STANDARD.encode(&handle)
} else {
String::from_utf8_lossy(&handle).to_string()
};
self.write_str(&format!("Query completed in {query_time:?}:\n\n```rs\n{result:?}\n```"))
.await
}