use std::{io::Write, path::Path};
use sokf_core::{CacheMode, GuideReport, NewOptions};
use crate::{cli::NewArgs, output, render};
pub fn new(repo_root: Option<&Path>, cache: CacheMode, args: NewArgs) -> sokf_core::Result<u8> {
let (cwd, root) = output::root(repo_root)?;
let opts = NewOptions {
type_name: output::required(args.type_name, "TYPE")?,
slug: args.slug,
id: args.id,
title: args.title,
seq: args.seq,
states: args.state,
dir: args.dir,
for_asset: args.for_asset,
cwd,
write: args.write,
id_only: args.id_only,
cache,
};
let result = sokf_core::new(&root, &opts)?;
let mut buf = Vec::new();
if args.json {
buf = output::json_line(&result)?;
} else if args.id_only {
writeln!(buf, "{}", result.id).map_err(output::stdout_err)?;
} else if args.write {
writeln!(buf, "created {}", result.path).map_err(output::stdout_err)?;
for index in &result.indexes {
writeln!(buf, "updated {index}").map_err(output::stdout_err)?;
}
} else {
buf.extend_from_slice(result.document.as_bytes());
}
output::write_stdout(&buf)?;
if !args.json
&& !args.id_only
&& let Some(guide) = &result.guide
{
let mut text =
b"\nThe guide of the new document: the guidance and the example of each part.\n\n"
.to_vec();
let report = GuideReport {
documents: vec![guide.clone()],
};
render::guide(&mut text, &report).map_err(output::stdout_err)?;
let _ = std::io::stderr().write_all(&text);
}
if let Some(e) = &result.index_error {
eprintln!("warning: new: {e}, so an index may be stale: run `sokf validate --fix`");
}
Ok(output::EXIT_OK)
}