Skip to content

Commit

Permalink
cli: Allow to specify a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
A6GibKm committed Sep 18, 2024
1 parent d8523a2 commit 86c0672
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
process::{ExitCode, Termination},
};

use clap::{Parser, Subcommand};
use clap::{Args, Parser, Subcommand};
use oo7::dbus::Service;
use time::{OffsetDateTime, UtcOffset};

Expand Down Expand Up @@ -124,9 +124,16 @@ enum Commands {
}

impl Commands {
async fn execute(self) -> Result<(), Error> {
async fn execute(self, args: &Arguments) -> Result<(), Error> {
let service = Service::new().await?;
let collection = service.default_collection().await?;
let collection = if let Some(alias) = &args.collection {
service
.with_alias(alias)
.await?
.ok_or_else(|| Error(format!("Collection '{alias}' not found")))?
} else {
service.default_collection().await?
};
match self {
Commands::Delete { attributes } => {
let items = collection.search_items(&attributes).await?;
Expand Down Expand Up @@ -193,12 +200,27 @@ impl Commands {
struct Cli {
#[command(subcommand)]
command: Commands,
#[command(flatten)]
args: Arguments,
}

#[derive(Args)]
struct Arguments {
#[arg(
name = "collection",
short,
long,
global = true,
help = "Specify a collection. The default collection will be used if not specified"
)]
collection: Option<String>,
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
let cli = Cli::parse();
cli.command.execute().await
let args = &cli.args;
cli.command.execute(args).await
}

// Source <https://github.com/clap-rs/clap/blob/master/examples/typed-derive.rs#L48>
Expand Down

0 comments on commit 86c0672

Please sign in to comment.