feat: add bags CLI commands

This commit is contained in:
Jon Seager 2025-11-27 14:02:38 +00:00
parent ef6f30d6c3
commit f0cef61513
No known key found for this signature in database
2 changed files with 115 additions and 0 deletions

View file

@ -0,0 +1,101 @@
use anyhow::Result;
use clap::Args;
use serde_json::json;
use super::print_json;
use crate::domain::ids::{BagId, RoastId};
use crate::infrastructure::client::BrewlogClient;
#[derive(Debug, Args)]
pub struct AddBagCommand {
#[arg(long)]
pub roast_id: i64,
#[arg(long)]
pub roast_date: Option<String>,
#[arg(long)]
pub amount: f64,
}
pub async fn add_bag(client: &BrewlogClient, command: AddBagCommand) -> Result<()> {
let roast_date = command
.roast_date
.map(|d| chrono::NaiveDate::parse_from_str(&d, "%Y-%m-%d"))
.transpose()?;
let bag = client
.bags()
.create(RoastId::new(command.roast_id), roast_date, command.amount)
.await?;
print_json(&bag)
}
#[derive(Debug, Args)]
pub struct ListBagsCommand {
#[arg(long)]
pub roast_id: Option<i64>,
}
pub async fn list_bags(client: &BrewlogClient, command: ListBagsCommand) -> Result<()> {
let bags = client
.bags()
.list(command.roast_id.map(RoastId::new))
.await?;
print_json(&bags)
}
#[derive(Debug, Args)]
pub struct GetBagCommand {
#[arg(long)]
pub id: i64,
}
pub async fn get_bag(client: &BrewlogClient, command: GetBagCommand) -> Result<()> {
let bag = client.bags().get(BagId::new(command.id)).await?;
print_json(&bag)
}
#[derive(Debug, Args)]
pub struct UpdateBagCommand {
#[arg(long)]
pub id: i64,
#[arg(long)]
pub remaining: Option<f64>,
#[arg(long)]
pub closed: Option<bool>,
#[arg(long)]
pub finished_at: Option<String>,
}
pub async fn update_bag(client: &BrewlogClient, command: UpdateBagCommand) -> Result<()> {
let finished_at = command
.finished_at
.map(|d| chrono::NaiveDate::parse_from_str(&d, "%Y-%m-%d"))
.transpose()?;
let bag = client
.bags()
.update(
BagId::new(command.id),
command.remaining,
command.closed,
finished_at,
)
.await?;
print_json(&bag)
}
#[derive(Debug, Args)]
pub struct DeleteBagCommand {
#[arg(long)]
pub id: i64,
}
pub async fn delete_bag(client: &BrewlogClient, command: DeleteBagCommand) -> Result<()> {
let id = BagId::new(command.id);
client.bags().delete(id).await?;
let response = json!({
"status": "deleted",
"resource": "bag",
"id": id.into_inner(),
});
print_json(&response)
}

View file

@ -1,9 +1,11 @@
pub mod bags;
pub mod roasters; pub mod roasters;
pub mod roasts; pub mod roasts;
pub mod tokens; pub mod tokens;
use std::net::SocketAddr; use std::net::SocketAddr;
use bags::{AddBagCommand, DeleteBagCommand, GetBagCommand, ListBagsCommand, UpdateBagCommand};
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
use roasters::{AddRoasterCommand, DeleteRoasterCommand, GetRoasterCommand, UpdateRoasterCommand}; use roasters::{AddRoasterCommand, DeleteRoasterCommand, GetRoasterCommand, UpdateRoasterCommand};
use roasts::{AddRoastCommand, DeleteRoastCommand, GetRoastCommand, ListRoastsCommand}; use roasts::{AddRoastCommand, DeleteRoastCommand, GetRoastCommand, ListRoastsCommand};
@ -58,6 +60,18 @@ pub enum Commands {
GetRoast(GetRoastCommand), GetRoast(GetRoastCommand),
#[command(name = "delete-roast")] #[command(name = "delete-roast")]
DeleteRoast(DeleteRoastCommand), DeleteRoast(DeleteRoastCommand),
// Bags
#[command(name = "add-bag")]
AddBag(AddBagCommand),
#[command(name = "list-bags")]
ListBags(ListBagsCommand),
#[command(name = "get-bag")]
GetBag(GetBagCommand),
#[command(name = "update-bag")]
UpdateBag(UpdateBagCommand),
#[command(name = "delete-bag")]
DeleteBag(DeleteBagCommand),
} }
#[derive(Debug, Args)] #[derive(Debug, Args)]