mod action;
use action::*;
use clap::{error::ErrorKind, CommandFactory as _, Parser, Subcommand, ValueHint};
use fav_core::FavCoreResult;
#[derive(Parser)]
#[command(author, version, about)]
pub struct Cli {
#[clap(subcommand)]
subcmd: Commands,
}
#[derive(Subcommand)]
enum Commands {
Init,
Auth {
#[clap(subcommand)]
subcmd: AuthCommands,
},
Fetch,
Status {
#[arg(value_hint = ValueHint::Other)]
id: Option<String>,
#[arg(long, short)]
sets: bool,
#[arg(long, short)]
res: bool,
#[arg(long, short)]
track: bool,
},
Track {
#[arg(value_hint = ValueHint::Other)]
id: Vec<String>,
},
Untrack {
#[arg(value_hint = ValueHint::Other)]
id: Vec<String>,
},
Pull {
#[arg(value_hint = ValueHint::Other)]
id: Option<Vec<String>>,
},
Daemon {
#[arg(value_hint = ValueHint::Other)]
interval: u64,
},
Completion {
#[arg(value_enum)]
shell: clap_complete::Shell,
},
}
#[derive(Subcommand)]
enum AuthCommands {
Login,
Logout,
}
impl Cli {
pub async fn run() -> FavCoreResult<()> {
let args = Self::parse();
match args.subcmd {
Commands::Init => init()?,
Commands::Auth { subcmd } => match subcmd {
AuthCommands::Login => login().await?,
AuthCommands::Logout => logout().await?,
},
Commands::Status {
id,
sets,
res,
track,
} => match id {
Some(id) => {
if sets | res | track {
Cli::command()
.error(
ErrorKind::ArgumentConflict,
"The id to 'fav status' does not take -s, -r, -t, options.",
)
.exit();
}
status(id)?;
}
None => match (sets, res) {
(false, false) => status_all(sets, true, track)?,
_ => status_all(sets, res, track)?,
},
},
Commands::Fetch => fetch().await?,
Commands::Track { id } => {
for id in id {
track(id)?;
}
}
Commands::Untrack { id } => {
for id in id {
untrack(id)?;
}
}
Commands::Pull { id } => match id {
Some(id) => {
for id in id {
pull(id).await?;
}
}
None => pull_all().await?,
},
Commands::Daemon { interval } => daemon(interval).await,
Commands::Completion { shell } => {
let mut cmd = Cli::command();
clap_complete::generate(shell, &mut cmd, "fav", &mut std::io::stdout());
}
}
Ok(())
}
}