44 lines
961 B
Rust
44 lines
961 B
Rust
use shared::{
|
|
errors::{Error, ErrorStatus},
|
|
names::{self, Site},
|
|
};
|
|
use std::path::Path;
|
|
|
|
use crate::commands::utils::{site_string, PrintOptions};
|
|
|
|
pub(crate) fn add(
|
|
path: &Path,
|
|
url: &String,
|
|
name: &Option<String>,
|
|
separator: &String,
|
|
) -> Result<(), Error> {
|
|
let names_file = names::read_names_file(path)?;
|
|
let mut names = names::load_names(names_file)?;
|
|
|
|
if names.iter().any(|site| site.url.contains(url)) {
|
|
return Err(Error {
|
|
status: ErrorStatus::AlreadyExistsError,
|
|
data:
|
|
"this url already exists in names.json. you can't have more then 1 of the same url."
|
|
.into(),
|
|
});
|
|
}
|
|
|
|
let site = Site {
|
|
url: url.to_string(),
|
|
name: name.to_owned(),
|
|
};
|
|
|
|
log::debug!("adding {:?} to {}", site, path.display());
|
|
names.push(site.clone());
|
|
|
|
let json = serde_json::to_string(&names).unwrap();
|
|
std::fs::write(path, json).unwrap();
|
|
println!(
|
|
"added {} to names.json",
|
|
site_string(&site, PrintOptions::All, separator)
|
|
);
|
|
|
|
Ok(())
|
|
}
|