use clap::{Args, Subcommand, builder::PossibleValuesParser};
use crate::cli::lifecycle::{self, DeleteArgs, DisableArgs, EnableArgs, ShowArgs};
use zad::error::Result;
use zad::service::registry::SERVICES;
use super::{
service_discord, service_gcal, service_list, service_onepass, service_slack, service_spotify,
service_status, service_telegram, service_ymusic,
};
use service_discord::DiscordLifecycle;
use service_gcal::GcalLifecycle;
use service_onepass::OnePassLifecycle;
use service_slack::SlackLifecycle;
use service_spotify::SpotifyLifecycle;
use service_telegram::TelegramLifecycle;
use service_ymusic::YmusicLifecycle;
#[derive(Debug, Args)]
pub struct ServiceArgs {
#[command(subcommand)]
pub action: Action,
}
#[derive(Debug, Subcommand)]
#[allow(clippy::large_enum_variant)]
pub enum Action {
Create(CreateArgs),
Enable(EnableAction),
Disable(DisableAction),
List(service_list::ListArgs),
Show(ShowAction),
Status(StatusArgs),
Delete(DeleteAction),
}
#[derive(Debug, Args)]
pub struct CreateArgs {
#[command(subcommand)]
pub service: CreateService,
}
#[derive(Debug, Subcommand)]
pub enum CreateService {
#[command(name = "1pass")]
OnePass(service_onepass::CreateArgs),
Discord(service_discord::CreateArgs),
Gcal(service_gcal::CreateArgs),
Spotify(service_spotify::CreateArgs),
Slack(service_slack::CreateArgs),
Telegram(service_telegram::CreateArgs),
Ymusic(service_ymusic::CreateArgs),
}
#[derive(Debug, Args)]
pub struct EnableAction {
#[command(subcommand)]
pub service: EnableService,
}
#[derive(Debug, Subcommand)]
pub enum EnableService {
#[command(name = "1pass")]
OnePass(EnableArgs),
Discord(EnableArgs),
Gcal(EnableArgs),
Slack(EnableArgs),
Spotify(EnableArgs),
Telegram(EnableArgs),
Ymusic(EnableArgs),
}
#[derive(Debug, Args)]
pub struct DisableAction {
#[command(subcommand)]
pub service: DisableService,
}
#[derive(Debug, Subcommand)]
pub enum DisableService {
#[command(name = "1pass")]
OnePass(DisableArgs),
Discord(DisableArgs),
Gcal(DisableArgs),
Slack(DisableArgs),
Spotify(DisableArgs),
Telegram(DisableArgs),
Ymusic(DisableArgs),
}
#[derive(Debug, Args)]
pub struct ShowAction {
#[command(subcommand)]
pub service: ShowService,
}
#[derive(Debug, Subcommand)]
pub enum ShowService {
#[command(name = "1pass")]
OnePass(ShowArgs),
Discord(ShowArgs),
Gcal(ShowArgs),
Slack(ShowArgs),
Spotify(ShowArgs),
Telegram(ShowArgs),
Ymusic(ShowArgs),
}
#[derive(Debug, Args)]
pub struct StatusArgs {
#[arg(long, value_name = "NAME", value_parser = PossibleValuesParser::new(SERVICES))]
pub service: Option<String>,
#[arg(long)]
pub json: bool,
}
#[derive(Debug, Args)]
pub struct DeleteAction {
#[command(subcommand)]
pub service: DeleteService,
}
#[derive(Debug, Subcommand)]
pub enum DeleteService {
#[command(name = "1pass")]
OnePass(DeleteArgs),
Discord(DeleteArgs),
Gcal(DeleteArgs),
Slack(DeleteArgs),
Spotify(DeleteArgs),
Telegram(DeleteArgs),
Ymusic(DeleteArgs),
}
pub async fn run(args: ServiceArgs) -> Result<()> {
match args.action {
Action::Create(c) => match c.service {
CreateService::OnePass(a) => lifecycle::run_create::<OnePassLifecycle>(a).await,
CreateService::Discord(a) => lifecycle::run_create::<DiscordLifecycle>(a).await,
CreateService::Gcal(a) => lifecycle::run_create::<GcalLifecycle>(a).await,
CreateService::Slack(a) => lifecycle::run_create::<SlackLifecycle>(a).await,
CreateService::Spotify(a) => lifecycle::run_create::<SpotifyLifecycle>(a).await,
CreateService::Telegram(a) => lifecycle::run_create::<TelegramLifecycle>(a).await,
CreateService::Ymusic(a) => lifecycle::run_create::<YmusicLifecycle>(a).await,
},
Action::Enable(a) => match a.service {
EnableService::OnePass(a) => lifecycle::run_enable::<OnePassLifecycle>(a),
EnableService::Discord(a) => lifecycle::run_enable::<DiscordLifecycle>(a),
EnableService::Gcal(a) => lifecycle::run_enable::<GcalLifecycle>(a),
EnableService::Slack(a) => lifecycle::run_enable::<SlackLifecycle>(a),
EnableService::Spotify(a) => lifecycle::run_enable::<SpotifyLifecycle>(a),
EnableService::Telegram(a) => lifecycle::run_enable::<TelegramLifecycle>(a),
EnableService::Ymusic(a) => lifecycle::run_enable::<YmusicLifecycle>(a),
},
Action::Disable(d) => match d.service {
DisableService::OnePass(a) => lifecycle::run_disable::<OnePassLifecycle>(a),
DisableService::Discord(a) => lifecycle::run_disable::<DiscordLifecycle>(a),
DisableService::Gcal(a) => lifecycle::run_disable::<GcalLifecycle>(a),
DisableService::Slack(a) => lifecycle::run_disable::<SlackLifecycle>(a),
DisableService::Spotify(a) => lifecycle::run_disable::<SpotifyLifecycle>(a),
DisableService::Telegram(a) => lifecycle::run_disable::<TelegramLifecycle>(a),
DisableService::Ymusic(a) => lifecycle::run_disable::<YmusicLifecycle>(a),
},
Action::List(a) => service_list::run(a),
Action::Show(s) => match s.service {
ShowService::OnePass(a) => lifecycle::run_show::<OnePassLifecycle>(a),
ShowService::Discord(a) => lifecycle::run_show::<DiscordLifecycle>(a),
ShowService::Gcal(a) => lifecycle::run_show::<GcalLifecycle>(a),
ShowService::Slack(a) => lifecycle::run_show::<SlackLifecycle>(a),
ShowService::Spotify(a) => lifecycle::run_show::<SpotifyLifecycle>(a),
ShowService::Telegram(a) => lifecycle::run_show::<TelegramLifecycle>(a),
ShowService::Ymusic(a) => lifecycle::run_show::<YmusicLifecycle>(a),
},
Action::Status(s) => match s.service.as_deref() {
None => service_status::run_all(s).await,
Some("1pass") => {
lifecycle::run_status::<OnePassLifecycle>(lifecycle::StatusArgs { json: s.json })
.await
}
Some("discord") => {
lifecycle::run_status::<DiscordLifecycle>(lifecycle::StatusArgs { json: s.json })
.await
}
Some("gcal") => {
lifecycle::run_status::<GcalLifecycle>(lifecycle::StatusArgs { json: s.json }).await
}
Some("slack") => {
lifecycle::run_status::<SlackLifecycle>(lifecycle::StatusArgs { json: s.json })
.await
}
Some("spotify") => {
lifecycle::run_status::<SpotifyLifecycle>(lifecycle::StatusArgs { json: s.json })
.await
}
Some("telegram") => {
lifecycle::run_status::<TelegramLifecycle>(lifecycle::StatusArgs { json: s.json })
.await
}
Some("ymusic") => {
lifecycle::run_status::<YmusicLifecycle>(lifecycle::StatusArgs { json: s.json })
.await
}
Some(other) => Err(zad::error::ZadError::Invalid(format!(
"unhandled service in status dispatch: `{other}`"
))),
},
Action::Delete(d) => match d.service {
DeleteService::OnePass(a) => lifecycle::run_delete::<OnePassLifecycle>(a),
DeleteService::Discord(a) => lifecycle::run_delete::<DiscordLifecycle>(a),
DeleteService::Gcal(a) => lifecycle::run_delete::<GcalLifecycle>(a),
DeleteService::Slack(a) => lifecycle::run_delete::<SlackLifecycle>(a),
DeleteService::Spotify(a) => lifecycle::run_delete::<SpotifyLifecycle>(a),
DeleteService::Telegram(a) => lifecycle::run_delete::<TelegramLifecycle>(a),
DeleteService::Ymusic(a) => lifecycle::run_delete::<YmusicLifecycle>(a),
},
}
}