meowy-webring/cli/src/commands.rs

37 lines
876 B
Rust

use shared::names::Site;
use crate::arguments::PrintGroup;
fn read_names(path: &String) -> Vec<Site> {
let names = std::fs::read_to_string(path).unwrap();
shared::names::load_names(names).unwrap()
}
pub(crate) fn print(path: &String, group: &PrintGroup) {
let names = std::fs::read_to_string(path).unwrap();
let parsed_names = shared::names::load_names(names).unwrap();
for site in parsed_names {
if group.name {
println!("{}", site.name.unwrap_or_default());
continue;
}
if group.url {
println!("{}", site.url);
continue;
}
println!("{:?}", site);
}
}
pub(crate) fn add(path: &String, url: &String, name: &Option<String>) {
let mut names = read_names(path);
let site = Site {
url: url.to_string(),
name: name.to_owned(),
};
names.push(site);
let json = serde_json::to_string(&names).unwrap();
std::fs::write(path, json).unwrap();
}