use crate::cmd::{AxCliCommand, ConsoleOpt};
use ax_core::{
node_connection::{request_single, Task},
util::formats::{ActyxOSError, ActyxOSResult, AdminRequest, AdminResponse},
};
use futures::{stream, Stream};
use serde::Serialize;
pub struct SettingsGet();
impl AxCliCommand for SettingsGet {
type Opt = GetOpt;
type Output = serde_json::Value;
fn run(opts: Self::Opt) -> Box<dyn Stream<Item = ActyxOSResult<Self::Output>> + Unpin> {
let r = Box::pin(run(opts));
Box::new(stream::once(r))
}
fn pretty(result: Self::Output) -> String {
serde_yaml::to_string(&result).unwrap_or_else(|e| format!("Unknown error converting settings to yaml: {}", e))
}
}
#[derive(clap::Parser, Clone, Debug)]
pub struct GetOpt {
#[command(flatten)]
actual_opts: GetSettingsCommand,
#[command(flatten)]
console_opt: ConsoleOpt,
}
#[derive(clap::Parser, Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct GetSettingsCommand {
#[arg(long = "no-defaults")]
pub(crate) no_defaults: bool,
#[arg(name = "SCOPE", value_parser = super::parse_scope)]
pub(crate) scope: ax_core::settings::Scope,
}
pub async fn run(opts: GetOpt) -> ActyxOSResult<serde_json::Value> {
let (mut conn, peer) = opts.console_opt.connect().await?;
request_single(
&mut conn,
move |tx| {
Task::Admin(
peer,
AdminRequest::SettingsGet {
no_defaults: opts.actual_opts.no_defaults,
scope: opts.actual_opts.scope,
},
tx,
)
},
|t| match t {
AdminResponse::SettingsGetResponse(resp) => Ok(resp),
r => Err(ActyxOSError::internal(format!("Unexpected reply: {:?}", r))),
},
)
.await
}