use super::Invocation;
use super::redteam::value;
use super::redteam_write::{self as rw, Contexts};
use crate::clients::SINGLE_REQUEST_BUDGET;
use crate::error::CliError;
use crate::json::OValue;
use crate::modelsec::views::{self, Kind};
use crate::output::Format;
use crate::writes::Change;
use scm::airs::model_security::ModelSecurityClient;
use std::ffi::OsStr;
pub fn config(
path: &OsStr,
) -> Result<(serde_json::Map<String, serde_json::Value>, String), CliError> {
let object = crate::files::read_config(path)?;
Ok((object, crate::files::shown(path)))
}
pub async fn connect(
invocation: &mut Invocation,
format: Option<Format>,
) -> Result<(ModelSecurityClient, Format), CliError> {
let connected = invocation
.connect_model_security(None)
.await
.map_err(super::nothing_changed)?;
let format = connected.format(format, false);
Ok((connected.client, format))
}
pub fn options() -> scm::Result<scm::RequestOptions> {
scm::RequestOptions::with_timeout(SINGLE_REQUEST_BUDGET)
.map(scm::RequestOptions::without_response_defaults)
}
pub async fn read_first<T: serde::Serialize>(
invocation: &Invocation,
call: impl Future<Output = scm::Result<T>>,
) -> Result<OValue, CliError> {
let answer = super::redteam::read(invocation, call)
.await
.map_err(super::nothing_changed)?;
value(&answer)
}
pub fn confirm(invocation: &mut Invocation, message: &str) -> Result<bool, CliError> {
rw::confirm(invocation, message)
}
pub async fn send<T>(
invocation: &Invocation,
change: Change,
hint: Option<&str>,
call: impl AsyncFnMut() -> scm::Result<T>,
) -> Result<T, CliError> {
rw::send(invocation, change, hint, &Contexts::plain(), call).await
}
pub fn ack(
invocation: &mut Invocation,
kind: Kind,
format: Format,
done: &str,
record: &OValue,
) -> Result<(), CliError> {
if kind == Kind::Scan {
views::register_uris(std::slice::from_ref(record), &invocation.secrets);
}
let mut doc = views::project(kind, record);
views::withhold(&mut doc, &invocation.secrets);
let uuid = doc
.get("uuid")
.and_then(OValue::as_str)
.unwrap_or_default()
.to_owned();
invocation.output.success(&format!("{done}: {uuid}"));
views::render_get(&mut invocation.output, kind, format, &doc)
}