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)]
#[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<String>,
},
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,
}

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 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<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 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!(),
}
}

View file

@ -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<String>,
@ -15,6 +15,6 @@ pub enum NamesError {
pub fn load_names(names: String) -> Result<Vec<Site>, NamesError> {
match serde_json::from_str::<Vec<Site>>(&names) {
Ok(content) => Ok(content),
Err(_) => Err(NamesError::ParseError)
Err(_) => Err(NamesError::ParseError),
}
}