use clap::{Args, Parser, Subcommand, ValueEnum};
use http::Uri;
use crate::{caldav, carddav};
#[derive(Clone, ValueEnum)]
enum Verbosity {
Error,
Warn,
Info,
Debug,
Trace,
}
#[derive(Args)]
pub(crate) struct Server {
#[arg(long)]
pub(crate) server_url: Uri,
}
#[derive(Args)]
#[group(required = true, multiple = false)]
struct Proto {
#[arg(long)]
caldav: bool,
#[arg(long)]
carddav: bool,
}
#[derive(Parser)]
#[clap(author, version = env!("DAVCLI_VERSION"), about, long_about = None)]
pub(crate) struct Cli {
#[command(flatten)]
proto: Proto,
#[command(subcommand)]
pub(crate) command: ServerCommand,
#[clap(short, long)]
verbose: Option<Verbosity>,
}
#[derive(Subcommand)]
pub(crate) enum ServerCommand {
Discover,
FindCollections,
ListItems { collection_href: String },
Tree,
Get { resource_href: String },
Create { resource_href: String },
Delete {
#[arg(long)]
force: bool,
href: String,
},
}
impl Cli {
pub(crate) fn execute(self) -> anyhow::Result<()> {
assert_ne!(self.proto.carddav, self.proto.caldav);
if self.proto.caldav {
caldav::execute(self.command)
} else {
carddav::execute(self.command)
}
}
pub(crate) fn log_level(&self) -> log::Level {
match self.verbose {
Some(Verbosity::Error) => log::Level::Error,
Some(Verbosity::Warn) | None => log::Level::Warn,
Some(Verbosity::Info) => log::Level::Info,
Some(Verbosity::Debug) => log::Level::Debug,
Some(Verbosity::Trace) => log::Level::Trace,
}
}
}
#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert();
}