use super::Invocation;
use super::modelsec_write as w;
use super::redteam::value;
use crate::error::CliError;
use crate::modelsec::body;
use crate::modelsec::change::{SCAN_CREATE_HINT, SCAN_CREATED};
use crate::modelsec::views::{self, Kind};
use crate::output::Format;
use std::ffi::OsStr;
pub async fn create(
invocation: &mut Invocation,
config: &OsStr,
format: Option<Format>,
) -> Result<(), CliError> {
let (object, _) = w::config(config)?;
if let Some(uri) = object.get("model_uri").and_then(serde_json::Value::as_str) {
for part in views::uri_secrets(uri) {
invocation.secrets.add(&part);
}
}
let request = body::scan_create(object)?;
let (ms, format) = w::connect(invocation, format).await?;
let scans = ms.scans();
let answer = w::send(
invocation,
SCAN_CREATED,
Some(SCAN_CREATE_HINT),
async || scans.create(&request, Some(&w::options()?)).await,
)
.await?;
invocation.committed = true;
w::ack(
invocation,
Kind::Scan,
format,
"Scan created",
&value(&answer)?,
)
}