78 lines
1.8 KiB
Rust
78 lines
1.8 KiB
Rust
use std::path::Path;
|
|
|
|
use shared::names;
|
|
use shared::{errors::Error, names::Site};
|
|
|
|
use crate::arguments::PrintGroup;
|
|
|
|
fn group_printing(seperator: &Option<char>, site: Site, group: &PrintGroup) {
|
|
let mut string = String::new();
|
|
let delimiter = seperator.unwrap_or(',');
|
|
|
|
if group.url {
|
|
string += &site.url;
|
|
}
|
|
|
|
if group.name {
|
|
if !string.is_empty() {
|
|
string += &format!("{}{}", delimiter, site.name.unwrap_or("None".to_string()))
|
|
} else {
|
|
string += &site.name.unwrap_or("None".to_string());
|
|
}
|
|
}
|
|
|
|
log::info!("{}", string);
|
|
}
|
|
|
|
pub(crate) fn print(
|
|
path: &Path,
|
|
group: &PrintGroup,
|
|
seperator: &Option<char>,
|
|
) -> Result<(), Error> {
|
|
let names_file = names::read_names_file(path)?;
|
|
let names = names::load_names(names_file)?;
|
|
|
|
for site in names {
|
|
if !group.url && !group.name {
|
|
log::info!("{:?}", site);
|
|
continue;
|
|
}
|
|
|
|
group_printing(seperator, site, group);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn add(path: &Path, url: &String, name: &Option<String>) -> Result<(), Error> {
|
|
let names_file = names::read_names_file(path)?;
|
|
let mut names = names::load_names(names_file)?;
|
|
|
|
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();
|
|
log::info!("added {:?} to names.json", site);
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn remove(path: &Path, url: &String) -> Result<(), Error> {
|
|
let names_file = names::read_names_file(path)?;
|
|
let mut names = names::load_names(names_file)?;
|
|
|
|
names.retain(|site| {
|
|
if &site.url == url {
|
|
log::info!("removing {:?} from names.json", site);
|
|
}
|
|
&site.url != url
|
|
});
|
|
let json = serde_json::to_string(&names).unwrap();
|
|
std::fs::write(path, json).unwrap();
|
|
|
|
Ok(())
|
|
}
|