use crate::{BoxError, StandardOptions};
use std::fs::exists;
use tracing::{info, warn};
pub async fn init(name: &Option<String>, _flags: &StandardOptions) -> Result<(), BoxError> {
if !exists(".asimov")? {
info!("Creating the directory `{}`...", ".asimov");
std::fs::create_dir_all(".asimov/")?;
warn!("Created the directory `{}`.", ".asimov");
}
if !exists(".asimov/module.yaml")? {
let module_name = if let Some(name) = name {
name.clone()
} else if exists("Cargo.toml")? {
let cargo_toml = distrib::rust::load_cargo_toml("Cargo.toml")?;
let package_name = cargo_toml.package().name();
package_name
.strip_prefix("asimov-")
.and_then(|s| s.strip_suffix("-module"))
.unwrap_or(package_name)
.to_string()
} else {
"mymodule".to_string() };
info!("Creating the file `{}`...", ".asimov/module.yaml");
std::fs::write(
".asimov/module.yaml",
format!(
"# See: https://asimov-specs.github.io/module-manifest/\n---\nname: {}\n",
module_name
),
)?;
warn!("Created the file `{}`.", ".asimov/module.yaml");
}
Ok(())
}