1use clap::{Args, Subcommand, builder::PossibleValuesParser};
10
11use crate::cli::lifecycle::{self, DeleteArgs, DisableArgs, EnableArgs, ShowArgs};
12use zad::error::Result;
13use zad::service::registry::SERVICES;
14
15use super::{
16 service_discord, service_gcal, service_list, service_onepass, service_slack, service_spotify,
17 service_status, service_telegram, service_ymusic,
18};
19use service_discord::DiscordLifecycle;
20use service_gcal::GcalLifecycle;
21use service_onepass::OnePassLifecycle;
22use service_slack::SlackLifecycle;
23use service_spotify::SpotifyLifecycle;
24use service_telegram::TelegramLifecycle;
25use service_ymusic::YmusicLifecycle;
26
27#[derive(Debug, Args)]
28pub struct ServiceArgs {
29 #[command(subcommand)]
30 pub action: Action,
31}
32
33#[derive(Debug, Subcommand)]
34#[allow(clippy::large_enum_variant)]
35pub enum Action {
36 Create(CreateArgs),
38 Enable(EnableAction),
40 Disable(DisableAction),
42 List(service_list::ListArgs),
44 Show(ShowAction),
46 Status(StatusArgs),
49 Delete(DeleteAction),
51}
52
53#[derive(Debug, Args)]
54pub struct CreateArgs {
55 #[command(subcommand)]
56 pub service: CreateService,
57}
58
59#[derive(Debug, Subcommand)]
60pub enum CreateService {
61 #[command(name = "1pass")]
64 OnePass(service_onepass::CreateArgs),
65 Discord(service_discord::CreateArgs),
68 Gcal(service_gcal::CreateArgs),
71 Spotify(service_spotify::CreateArgs),
74 Slack(service_slack::CreateArgs),
77 Telegram(service_telegram::CreateArgs),
80 Ymusic(service_ymusic::CreateArgs),
83}
84
85#[derive(Debug, Args)]
86pub struct EnableAction {
87 #[command(subcommand)]
88 pub service: EnableService,
89}
90
91#[derive(Debug, Subcommand)]
92pub enum EnableService {
93 #[command(name = "1pass")]
95 OnePass(EnableArgs),
96 Discord(EnableArgs),
98 Gcal(EnableArgs),
100 Slack(EnableArgs),
102 Spotify(EnableArgs),
104 Telegram(EnableArgs),
106 Ymusic(EnableArgs),
108}
109
110#[derive(Debug, Args)]
111pub struct DisableAction {
112 #[command(subcommand)]
113 pub service: DisableService,
114}
115
116#[derive(Debug, Subcommand)]
117pub enum DisableService {
118 #[command(name = "1pass")]
120 OnePass(DisableArgs),
121 Discord(DisableArgs),
123 Gcal(DisableArgs),
125 Slack(DisableArgs),
127 Spotify(DisableArgs),
129 Telegram(DisableArgs),
131 Ymusic(DisableArgs),
133}
134
135#[derive(Debug, Args)]
136pub struct ShowAction {
137 #[command(subcommand)]
138 pub service: ShowService,
139}
140
141#[derive(Debug, Subcommand)]
142pub enum ShowService {
143 #[command(name = "1pass")]
145 OnePass(ShowArgs),
146 Discord(ShowArgs),
148 Gcal(ShowArgs),
150 Slack(ShowArgs),
152 Spotify(ShowArgs),
154 Telegram(ShowArgs),
156 Ymusic(ShowArgs),
158}
159
160#[derive(Debug, Args)]
167pub struct StatusArgs {
168 #[arg(long, value_name = "NAME", value_parser = PossibleValuesParser::new(SERVICES))]
171 pub service: Option<String>,
172
173 #[arg(long)]
176 pub json: bool,
177}
178
179#[derive(Debug, Args)]
180pub struct DeleteAction {
181 #[command(subcommand)]
182 pub service: DeleteService,
183}
184
185#[derive(Debug, Subcommand)]
186pub enum DeleteService {
187 #[command(name = "1pass")]
190 OnePass(DeleteArgs),
191 Discord(DeleteArgs),
194 Gcal(DeleteArgs),
197 Slack(DeleteArgs),
200 Spotify(DeleteArgs),
203 Telegram(DeleteArgs),
206 Ymusic(DeleteArgs),
209}
210
211pub async fn run(args: ServiceArgs) -> Result<()> {
212 match args.action {
213 Action::Create(c) => match c.service {
214 CreateService::OnePass(a) => lifecycle::run_create::<OnePassLifecycle>(a).await,
215 CreateService::Discord(a) => lifecycle::run_create::<DiscordLifecycle>(a).await,
216 CreateService::Gcal(a) => lifecycle::run_create::<GcalLifecycle>(a).await,
217 CreateService::Slack(a) => lifecycle::run_create::<SlackLifecycle>(a).await,
218 CreateService::Spotify(a) => lifecycle::run_create::<SpotifyLifecycle>(a).await,
219 CreateService::Telegram(a) => lifecycle::run_create::<TelegramLifecycle>(a).await,
220 CreateService::Ymusic(a) => lifecycle::run_create::<YmusicLifecycle>(a).await,
221 },
222 Action::Enable(a) => match a.service {
223 EnableService::OnePass(a) => lifecycle::run_enable::<OnePassLifecycle>(a),
224 EnableService::Discord(a) => lifecycle::run_enable::<DiscordLifecycle>(a),
225 EnableService::Gcal(a) => lifecycle::run_enable::<GcalLifecycle>(a),
226 EnableService::Slack(a) => lifecycle::run_enable::<SlackLifecycle>(a),
227 EnableService::Spotify(a) => lifecycle::run_enable::<SpotifyLifecycle>(a),
228 EnableService::Telegram(a) => lifecycle::run_enable::<TelegramLifecycle>(a),
229 EnableService::Ymusic(a) => lifecycle::run_enable::<YmusicLifecycle>(a),
230 },
231 Action::Disable(d) => match d.service {
232 DisableService::OnePass(a) => lifecycle::run_disable::<OnePassLifecycle>(a),
233 DisableService::Discord(a) => lifecycle::run_disable::<DiscordLifecycle>(a),
234 DisableService::Gcal(a) => lifecycle::run_disable::<GcalLifecycle>(a),
235 DisableService::Slack(a) => lifecycle::run_disable::<SlackLifecycle>(a),
236 DisableService::Spotify(a) => lifecycle::run_disable::<SpotifyLifecycle>(a),
237 DisableService::Telegram(a) => lifecycle::run_disable::<TelegramLifecycle>(a),
238 DisableService::Ymusic(a) => lifecycle::run_disable::<YmusicLifecycle>(a),
239 },
240 Action::List(a) => service_list::run(a),
241 Action::Show(s) => match s.service {
242 ShowService::OnePass(a) => lifecycle::run_show::<OnePassLifecycle>(a),
243 ShowService::Discord(a) => lifecycle::run_show::<DiscordLifecycle>(a),
244 ShowService::Gcal(a) => lifecycle::run_show::<GcalLifecycle>(a),
245 ShowService::Slack(a) => lifecycle::run_show::<SlackLifecycle>(a),
246 ShowService::Spotify(a) => lifecycle::run_show::<SpotifyLifecycle>(a),
247 ShowService::Telegram(a) => lifecycle::run_show::<TelegramLifecycle>(a),
248 ShowService::Ymusic(a) => lifecycle::run_show::<YmusicLifecycle>(a),
249 },
250 Action::Status(s) => match s.service.as_deref() {
251 None => service_status::run_all(s).await,
252 Some("1pass") => {
253 lifecycle::run_status::<OnePassLifecycle>(lifecycle::StatusArgs { json: s.json })
254 .await
255 }
256 Some("discord") => {
257 lifecycle::run_status::<DiscordLifecycle>(lifecycle::StatusArgs { json: s.json })
258 .await
259 }
260 Some("gcal") => {
261 lifecycle::run_status::<GcalLifecycle>(lifecycle::StatusArgs { json: s.json }).await
262 }
263 Some("slack") => {
264 lifecycle::run_status::<SlackLifecycle>(lifecycle::StatusArgs { json: s.json })
265 .await
266 }
267 Some("spotify") => {
268 lifecycle::run_status::<SpotifyLifecycle>(lifecycle::StatusArgs { json: s.json })
269 .await
270 }
271 Some("telegram") => {
272 lifecycle::run_status::<TelegramLifecycle>(lifecycle::StatusArgs { json: s.json })
273 .await
274 }
275 Some("ymusic") => {
276 lifecycle::run_status::<YmusicLifecycle>(lifecycle::StatusArgs { json: s.json })
277 .await
278 }
279 Some(other) => Err(zad::error::ZadError::Invalid(format!(
283 "unhandled service in status dispatch: `{other}`"
284 ))),
285 },
286 Action::Delete(d) => match d.service {
287 DeleteService::OnePass(a) => lifecycle::run_delete::<OnePassLifecycle>(a),
288 DeleteService::Discord(a) => lifecycle::run_delete::<DiscordLifecycle>(a),
289 DeleteService::Gcal(a) => lifecycle::run_delete::<GcalLifecycle>(a),
290 DeleteService::Slack(a) => lifecycle::run_delete::<SlackLifecycle>(a),
291 DeleteService::Spotify(a) => lifecycle::run_delete::<SpotifyLifecycle>(a),
292 DeleteService::Telegram(a) => lifecycle::run_delete::<TelegramLifecycle>(a),
293 DeleteService::Ymusic(a) => lifecycle::run_delete::<YmusicLifecycle>(a),
294 },
295 }
296}