add a add command to the cli

this command can add sites to the names.json file
This commit is contained in:
Fries 2023-06-30 22:11:44 -07:00
parent c481999c55
commit c729d2115b
4 changed files with 61 additions and 17 deletions

View File

@ -1,30 +1,54 @@
use clap::{Parser, command, Subcommand, arg, Args}; use clap::{arg, command, Args, Parser, Subcommand};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
pub(crate) struct Arguments { pub(crate) struct Arguments {
#[arg(help = "the path to the names.json file")]
pub(crate) path: String,
#[command(subcommand)] #[command(subcommand)]
pub(crate) command: Commands pub(crate) command: Commands,
} }
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
pub(crate) enum Commands { pub(crate) enum Commands {
#[command(about = "print the current webring sites and their names")] #[command(about = "print the current webring sites and their names")]
Print { Print {
#[arg(help = "the path to the names.json file")]
path: String,
#[command(flatten)] #[command(flatten)]
group: Group group: PrintGroup,
} },
#[command(about = "add a site to the webring")]
Add {
#[arg(
long,
short,
required = true,
help = "the url of the site you want to add. example: \"example.com\"."
)]
url: String,
#[arg(
long,
short,
required = false,
help = "the personal name of the site. this is not required."
)]
name: Option<String>,
},
Remove {
#[arg(
long,
short,
required = true,
help = "the url of the site you want to remove."
)]
url: String,
},
} }
#[derive(Args, Debug)] #[derive(Args, Debug)]
#[group(required = false)] #[group(required = false)]
pub struct Group { pub struct PrintGroup {
#[arg(long, short, action = clap::ArgAction::SetTrue, conflicts_with = "name", help = "print the url only")] #[arg(long, short, action = clap::ArgAction::SetTrue, conflicts_with = "name", help = "print the url only")]
pub(crate) url: bool, pub(crate) url: bool,
#[arg(long, short, action = clap::ArgAction::SetTrue, conflicts_with = "url", help = "print the name only")] #[arg(long, short, action = clap::ArgAction::SetTrue, conflicts_with = "url", help = "print the name only")]
pub(crate) name: bool pub(crate) name: bool,
} }

View File

@ -1,6 +1,13 @@
use crate::arguments::Group; use shared::names::Site;
pub(crate) fn print(path: &String, group: &Group) { 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 names = std::fs::read_to_string(path).unwrap();
let parsed_names = shared::names::load_names(names).unwrap(); let parsed_names = shared::names::load_names(names).unwrap();
@ -16,3 +23,14 @@ pub(crate) fn print(path: &String, group: &Group) {
println!("{:?}", site); 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();
}

View File

@ -1,6 +1,6 @@
use arguments::{Arguments, Commands}; use arguments::{Arguments, Commands};
use clap::Parser; use clap::Parser;
use commands::print; use commands::{add, print};
mod arguments; mod arguments;
mod commands; mod commands;
@ -9,6 +9,8 @@ fn main() {
let args = Arguments::parse(); let args = Arguments::parse();
match &args.command { match &args.command {
Commands::Print { path, group } => print(path, group) Commands::Print { group } => print(&args.path, &group),
Commands::Add { url, name } => add(&args.path, url, name),
Commands::Remove { url } => todo!(),
} }
} }

View File

@ -1,6 +1,6 @@
use serde::Deserialize; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Site { pub struct Site {
pub url: String, pub url: String,
pub name: Option<String>, pub name: Option<String>,
@ -15,6 +15,6 @@ pub enum NamesError {
pub fn load_names(names: String) -> Result<Vec<Site>, NamesError> { pub fn load_names(names: String) -> Result<Vec<Site>, NamesError> {
match serde_json::from_str::<Vec<Site>>(&names) { match serde_json::from_str::<Vec<Site>>(&names) {
Ok(content) => Ok(content), Ok(content) => Ok(content),
Err(_) => Err(NamesError::ParseError) Err(_) => Err(NamesError::ParseError),
} }
} }