From c729d2115bc5575257df2327094f4592c9ba4815 Mon Sep 17 00:00:00 2001 From: Fries Date: Fri, 30 Jun 2023 22:11:44 -0700 Subject: [PATCH] add a add command to the cli this command can add sites to the names.json file --- cli/src/arguments.rs | 44 ++++++++++++++++++++++++++++++++++---------- cli/src/commands.rs | 22 ++++++++++++++++++++-- cli/src/main.rs | 6 ++++-- shared/src/names.rs | 6 +++--- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/cli/src/arguments.rs b/cli/src/arguments.rs index 7db619e..413b6f5 100644 --- a/cli/src/arguments.rs +++ b/cli/src/arguments.rs @@ -1,30 +1,54 @@ -use clap::{Parser, command, Subcommand, arg, Args}; +use clap::{arg, command, Args, Parser, Subcommand}; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] pub(crate) struct Arguments { + #[arg(help = "the path to the names.json file")] + pub(crate) path: String, #[command(subcommand)] - pub(crate) command: Commands + pub(crate) command: Commands, } - #[derive(Subcommand, Debug)] pub(crate) enum Commands { #[command(about = "print the current webring sites and their names")] Print { - #[arg(help = "the path to the names.json file")] - path: String, #[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, + }, + Remove { + #[arg( + long, + short, + required = true, + help = "the url of the site you want to remove." + )] + url: String, + }, } #[derive(Args, Debug)] #[group(required = false)] -pub struct Group { +pub struct PrintGroup { #[arg(long, short, action = clap::ArgAction::SetTrue, conflicts_with = "name", help = "print the url only")] pub(crate) url: bool, #[arg(long, short, action = clap::ArgAction::SetTrue, conflicts_with = "url", help = "print the name only")] - pub(crate) name: bool + pub(crate) name: bool, } diff --git a/cli/src/commands.rs b/cli/src/commands.rs index e5d80ee..f21906c 100644 --- a/cli/src/commands.rs +++ b/cli/src/commands.rs @@ -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 { + 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(); @@ -16,3 +23,14 @@ pub(crate) fn print(path: &String, group: &Group) { println!("{:?}", site); } } + +pub(crate) fn add(path: &String, url: &String, name: &Option) { + 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(); +} diff --git a/cli/src/main.rs b/cli/src/main.rs index 482b758..e6f330e 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,6 +1,6 @@ use arguments::{Arguments, Commands}; use clap::Parser; -use commands::print; +use commands::{add, print}; mod arguments; mod commands; @@ -9,6 +9,8 @@ fn main() { let args = Arguments::parse(); 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!(), } } diff --git a/shared/src/names.rs b/shared/src/names.rs index 112862e..cbf8e4f 100644 --- a/shared/src/names.rs +++ b/shared/src/names.rs @@ -1,6 +1,6 @@ -use serde::Deserialize; +use serde::{Deserialize, Serialize}; -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct Site { pub url: String, pub name: Option, @@ -15,6 +15,6 @@ pub enum NamesError { pub fn load_names(names: String) -> Result, NamesError> { match serde_json::from_str::>(&names) { Ok(content) => Ok(content), - Err(_) => Err(NamesError::ParseError) + Err(_) => Err(NamesError::ParseError), } }