zad-cli 0.6.4

Command-line interface for zad — connects AI agents to external services (Discord, Slack, Google Calendar, Spotify, Telegram, YouTube Music, 1Password) via scoped service configurations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! CLI shell over the library's typed lifecycle drivers.
//!
//! The typed core — the `LifecycleService` trait, driver functions
//! (`create`, `enable`, `disable`, `show`, `delete`, `status_for`),
//! and the `*Outcome` types — lives in
//! [`zad::service::lifecycle`]. This module adds the CLI-only layer:
//!
//! 1. **`CliLifecycle`** — extends `LifecycleService` with a
//!    `clap::Args`-derived `CreateArgs` associated type and an
//!    interactive `resolve` step that turns CLI flags into
//!    `(Cfg, Secrets)`.
//! 2. **clap arg structs** — `CreateArgsBase`, `BotTokenArgs`,
//!    `ScopesArg`, `EnableArgs`, `DisableArgs`, `ShowArgs`,
//!    `StatusArgs`, `DeleteArgs`. Per-service `CreateArgs` flatten
//!    these in.
//! 3. **`run_*` driver wrappers** that prompt where needed, call
//!    into the library drivers, and render the typed `*Outcome`
//!    values to stdout in human or JSON form.
//! 4. **`resolve_bot_token` / `resolve_scopes`** — the only places
//!    `dialoguer` is invoked from inside the lifecycle layer.

use async_trait::async_trait;
use clap::Args;

use zad::error::{Result, ZadError};
use zad::secrets::Scope;
use zad::service::lifecycle::{
    self, CreateOpts, CreateOutcome, DeleteOpts, DeleteOutcome, DisableOpts, DisableOutcome,
    EnableOpts, EnableOutcome, ShowOpts, ShowOutcome,
};

use crate::cli::DialoguerExt;

// Re-exports so existing per-service adapters and tests keep their
// `use crate::cli::lifecycle::{SecretRef, ServiceStatusOutput, …}`
// paths working unchanged. The library module is the source of truth;
// these are thin facades.
pub use lifecycle::{
    LifecycleService, ProjectBlock, ScopeBlock, SecretRef, ServiceStatusOutput, StatusBlock,
    StatusCheck, leak, status_for,
};

// ---------------------------------------------------------------------------
// CLI extension trait
// ---------------------------------------------------------------------------

/// CLI-side extension of the library's `LifecycleService`. Adds the
/// clap-derived `CreateArgs` shape and the interactive `resolve` step.
/// Implementors live in `cli/service_<name>.rs` alongside their
/// `LifecycleService` impl.
#[async_trait]
pub trait CliLifecycle: LifecycleService {
    /// Per-service `zad service create <name>` flag struct. Must
    /// embed [`CreateArgsBase`] via `#[command(flatten)]` and expose
    /// it through [`CreateArgsLike::base`].
    type CreateArgs: Args + CreateArgsLike + Send + Sync;

    /// Build `(Cfg, Secrets)` from CLI args. Interactive mode prompts
    /// for any `Option<_>` fields that arrived empty; non-interactive
    /// mode returns [`ZadError::MissingRequired`] for anything still
    /// missing.
    async fn resolve(
        args: &Self::CreateArgs,
        non_interactive: bool,
    ) -> Result<(Self::Cfg, Self::Secrets)>;
}

// ---------------------------------------------------------------------------
// Shared clap arg structs
// ---------------------------------------------------------------------------

/// Flags every `zad service create <name>` accepts.
#[derive(Debug, Args)]
pub struct CreateArgsBase {
    /// Write credentials to this project's private service directory
    /// instead of the shared global location.
    #[arg(long)]
    pub local: bool,

    /// Overwrite any existing configuration at the chosen scope.
    #[arg(long)]
    pub force: bool,

    /// Fail instead of prompting for any missing value.
    #[arg(long)]
    pub non_interactive: bool,

    /// Skip the provider-side token validation step.
    #[arg(long)]
    pub no_validate: bool,

    /// Don't open URLs in the system browser.
    #[arg(long)]
    pub no_browser: bool,

    /// Emit machine-readable JSON instead of human-readable text.
    #[arg(long)]
    pub json: bool,
}

/// Lets the driver read [`CreateArgsBase`] out of a service-specific
/// wrapper.
pub trait CreateArgsLike {
    fn base(&self) -> &CreateArgsBase;
}

/// Drop-in clap flags for a single bot-token credential.
#[derive(Debug, Args)]
pub struct BotTokenArgs {
    #[arg(long, conflicts_with = "bot_token_env")]
    pub bot_token: Option<String>,
    #[arg(long, conflicts_with = "bot_token")]
    pub bot_token_env: Option<String>,
}

/// Drop-in clap flag for the common "comma-separated scopes" pattern.
#[derive(Debug, Args)]
pub struct ScopesArg {
    /// Capabilities to enable (comma-separated).
    #[arg(long, value_delimiter = ',')]
    pub scopes: Option<Vec<String>>,
}

#[derive(Debug, Args)]
pub struct EnableArgs {
    #[arg(long)]
    pub force: bool,
    #[arg(long)]
    pub non_interactive: bool,
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct DisableArgs {
    #[arg(long)]
    pub force: bool,
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct ShowArgs {
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct StatusArgs {
    #[arg(long)]
    pub json: bool,
}

#[derive(Debug, Args)]
pub struct DeleteArgs {
    #[arg(long)]
    pub local: bool,
    #[arg(long)]
    pub force: bool,
    #[arg(long)]
    pub json: bool,
}

// ---------------------------------------------------------------------------
// JSON envelopes — wrap the library's typed outcomes in a `command`
// field for the existing CLI contract (e.g. `service.create.discord`).
// ---------------------------------------------------------------------------

#[derive(Debug, serde::Serialize)]
struct CreateEnvelope<'a> {
    command: String,
    scope: &'static str,
    config_path: String,
    #[serde(flatten)]
    service: serde_json::Value,
    scopes: Vec<String>,
    secrets: &'a [SecretRef],
    #[serde(skip_serializing_if = "Option::is_none")]
    authenticated_as: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    hint: Option<&'a str>,
}

#[derive(Debug, serde::Serialize)]
struct EnableEnvelope {
    command: String,
    project_config: String,
    credentials_path: String,
    credentials_scope: &'static str,
}

#[derive(Debug, serde::Serialize)]
struct DisableEnvelope {
    command: String,
    project_config: String,
    was_enabled: bool,
}

#[derive(Debug, serde::Serialize)]
struct ShowEnvelope<'a> {
    command: String,
    service: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    effective: Option<&'static str>,
    global: &'a ScopeBlock,
    local: &'a ScopeBlock,
    project: &'a ProjectBlock,
}

#[derive(Debug, serde::Serialize)]
struct DeleteEnvelope<'a> {
    command: String,
    scope: &'static str,
    config_path: String,
    config_removed: bool,
    secrets: &'a [SecretRef],
    project_still_references: bool,
}

// ---------------------------------------------------------------------------
// CLI driver: create
// ---------------------------------------------------------------------------

pub async fn run_create<T: CliLifecycle>(args: T::CreateArgs) -> Result<()> {
    let base = args.base();
    let (config_path, scope_label, scope_machine, keychain_scope): (_, _, _, Scope<'_>) =
        if base.local {
            let slug = zad::config::path::project_slug()?;
            let p = zad::config::path::project_service_config_path_for(&slug, T::NAME)?;
            (
                p,
                "local (project-scoped)".to_string(),
                "local",
                Scope::Project(leak(slug)),
            )
        } else {
            (
                zad::config::path::global_service_config_path(T::NAME)?,
                "global".to_string(),
                "global",
                Scope::Global,
            )
        };

    let (cfg, creds) = T::resolve(&args, base.non_interactive).await?;

    let validate = !base.no_validate;
    let opts = CreateOpts {
        scope_label: scope_machine,
        scope: keychain_scope,
        config_path: config_path.clone(),
        force: base.force,
        validate,
    };
    let outcome: CreateOutcome = lifecycle::create::<T>(&cfg, &creds, opts).await?;

    if validate
        && let Some(name) = outcome.authenticated_as.as_deref()
        && !base.json
    {
        println!("  ✓ authenticated as `{name}`");
    }

    if base.json {
        let env = CreateEnvelope {
            command: format!("service.create.{}", T::NAME),
            scope: scope_machine,
            config_path: outcome.config_path.display().to_string(),
            service: T::cfg_json(&cfg),
            scopes: T::scopes_of(&cfg).to_vec(),
            secrets: &outcome.secrets,
            authenticated_as: outcome.authenticated_as.as_deref(),
            hint: outcome.hint.as_deref(),
        };
        println!("{}", serde_json::to_string_pretty(&env).unwrap());
    } else {
        let lines = T::cfg_human(&cfg);
        let scopes = T::scopes_of(&cfg);
        let width = label_width(&lines, scopes, &outcome.secrets);
        println!();
        println!("{} credentials created ({scope_label}).", T::DISPLAY);
        let config_label = "config";
        let config_value = outcome.config_path.display().to_string();
        println!("  {config_label:width$} : {config_value}");
        for (label, value) in &lines {
            println!("  {label:width$} : {value}");
        }
        let scopes_label = "scopes";
        let scopes_value = if scopes.is_empty() {
            "(none)".to_string()
        } else {
            scopes.join(", ")
        };
        println!("  {scopes_label:width$} : {scopes_value}");
        for s in &outcome.secrets {
            let label = s.label;
            let account = &s.account;
            println!("  {label:width$} : OS keychain (service=\"zad\", account=\"{account}\")");
        }
        println!();
        println!(
            "Next: run `zad service enable {}` in each project that should use {}.",
            T::NAME,
            T::DISPLAY
        );
        if let Some(url) = outcome.hint.as_deref() {
            println!();
            println!("  open: {url}");
        }
    }

    if let Some(url) = outcome.hint.as_deref()
        && !base.no_browser
        && !base.non_interactive
    {
        let _ = open::that(url);
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// CLI driver: enable
// ---------------------------------------------------------------------------

pub fn run_enable<T: LifecycleService>(args: EnableArgs) -> Result<()> {
    let outcome: EnableOutcome = lifecycle::enable::<T>(EnableOpts { force: args.force })?;
    if args.json {
        let env = EnableEnvelope {
            command: format!("service.enable.{}", T::NAME),
            project_config: outcome.project_config.display().to_string(),
            credentials_path: outcome.credentials_path.display().to_string(),
            credentials_scope: outcome.credentials_scope,
        };
        println!("{}", serde_json::to_string_pretty(&env).unwrap());
    } else {
        println!("{} service enabled for this project.", T::DISPLAY);
        println!("  project config : {}", outcome.project_config.display());
        println!(
            "  credentials    : {} ({})",
            outcome.credentials_path.display(),
            outcome.credentials_scope
        );
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// CLI driver: disable
// ---------------------------------------------------------------------------

pub fn run_disable<T: LifecycleService>(args: DisableArgs) -> Result<()> {
    let outcome: DisableOutcome = lifecycle::disable::<T>(DisableOpts { force: args.force })?;
    if args.json {
        let env = DisableEnvelope {
            command: format!("service.disable.{}", T::NAME),
            project_config: outcome.project_config.display().to_string(),
            was_enabled: outcome.was_enabled,
        };
        println!("{}", serde_json::to_string_pretty(&env).unwrap());
    } else if outcome.was_enabled {
        println!("{} service disabled for this project.", T::DISPLAY);
        println!("  project config : {}", outcome.project_config.display());
    } else {
        println!(
            "{} service was not enabled for this project (nothing to do).",
            T::DISPLAY
        );
        println!("  project config : {}", outcome.project_config.display());
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// CLI driver: show
// ---------------------------------------------------------------------------

pub fn run_show<T: LifecycleService>(args: ShowArgs) -> Result<()> {
    let outcome: ShowOutcome = lifecycle::show::<T>(ShowOpts)?;

    if args.json {
        let env = ShowEnvelope {
            command: format!("service.show.{}", T::NAME),
            service: T::NAME,
            effective: outcome.effective,
            global: &outcome.global,
            local: &outcome.local,
            project: &outcome.project,
        };
        println!("{}", serde_json::to_string_pretty(&env).unwrap());
        return Ok(());
    }

    println!("Service: {}", T::NAME);
    println!();
    println!("## Credentials");
    if let Some(label) = outcome.effective {
        println!("  effective : {label}");
    } else {
        println!(
            "  effective : (none — run `zad service create {}`)",
            T::NAME
        );
    }

    print_scope_block::<T>("global", &outcome.global);
    print_scope_block::<T>("local", &outcome.local);

    println!();
    println!("## Project");
    if outcome.project.enabled {
        println!("  enabled : yes");
    } else {
        println!("  enabled : no");
    }
    println!("  config  : {}", outcome.project.config);
    Ok(())
}

fn print_scope_block<T: LifecycleService>(label: &str, block: &ScopeBlock) {
    println!();
    println!("  [{label}] {}", block.path);
    if !block.configured {
        println!("    status : not configured");
        return;
    }
    let scopes_owned: Vec<String> = block.scopes.clone().unwrap_or_default();
    let width = label_width(&block.human_lines, &scopes_owned, &block.secrets);
    for (lbl, value) in &block.human_lines {
        println!("    {lbl:width$} : {value}");
    }
    let scopes_label = "scopes";
    let scopes_value = if scopes_owned.is_empty() {
        "(none)".to_string()
    } else {
        scopes_owned.join(", ")
    };
    println!("    {scopes_label:width$} : {scopes_value}");
    for s in &block.secrets {
        let lbl = s.label;
        let state = if s.present { "stored" } else { "missing" };
        let account = &s.account;
        println!("    {lbl:width$} : {state} (service=\"zad\", account=\"{account}\")");
    }
    let _ = T::NAME; // keep the type bound live for parity
}

// ---------------------------------------------------------------------------
// CLI driver: status
// ---------------------------------------------------------------------------

/// Run `zad service status --service <svc>` for service `T`. Emits
/// JSON or human output, then exits the process with code 1 if the
/// effective scope failed its live ping.
pub async fn run_status<T: LifecycleService>(args: StatusArgs) -> Result<()> {
    let mut out = lifecycle::status_for::<T>().await?;
    out.command = Some(format!("service.status.{}", T::NAME));
    if args.json {
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
    } else {
        print_status_human(&out);
    }
    if !out.ok {
        std::process::exit(1);
    }
    Ok(())
}

pub(crate) fn print_status_human(out: &ServiceStatusOutput) {
    println!("Service: {}", out.service);
    println!();
    println!("## Credentials");
    match out.effective {
        Some(label) => println!("  effective : {label}"),
        None => println!(
            "  effective : (none — run `zad service create {}`)",
            out.service
        ),
    }
    println!("  overall   : {}", if out.ok { "ok" } else { "FAILED" });

    print_status_scope("global", &out.global);
    print_status_scope("local", &out.local);

    println!();
    println!("## Project");
    println!(
        "  enabled : {}",
        if out.project.enabled { "yes" } else { "no" }
    );
    println!("  config  : {}", out.project.config);
}

fn print_status_scope(label: &str, block: &StatusBlock) {
    println!();
    println!("  [{label}] {}", block.path);
    if !block.configured {
        println!("    status : not configured");
        return;
    }
    println!(
        "    credentials : {}",
        if block.credentials_present {
            "present"
        } else {
            "missing"
        }
    );
    match &block.check {
        None => println!("    check       : (not the effective scope)"),
        Some(c) if c.ok => {
            let name = c.authenticated_as.as_deref().unwrap_or("(unknown)");
            println!("    check       : ok (authenticated as `{name}`)");
        }
        Some(c) => {
            let err = c.error.as_deref().unwrap_or("(no error message)");
            println!("    check       : FAILED ({err})");
        }
    }
}

// ---------------------------------------------------------------------------
// CLI driver: delete
// ---------------------------------------------------------------------------

pub fn run_delete<T: LifecycleService>(args: DeleteArgs) -> Result<()> {
    let (config_path, scope_label, scope_machine, keychain_scope): (_, _, _, Scope<'_>) =
        if args.local {
            let slug = zad::config::path::project_slug()?;
            let p = zad::config::path::project_service_config_path_for(&slug, T::NAME)?;
            (
                p,
                "local (project-scoped)".to_string(),
                "local",
                Scope::Project(leak(slug)),
            )
        } else {
            (
                zad::config::path::global_service_config_path(T::NAME)?,
                "global".to_string(),
                "global",
                Scope::Global,
            )
        };

    let outcome: DeleteOutcome = lifecycle::delete::<T>(DeleteOpts {
        scope_label: scope_machine,
        scope: keychain_scope,
        config_path: config_path.clone(),
        force: args.force,
    })?;

    if args.json {
        let env = DeleteEnvelope {
            command: format!("service.delete.{}", T::NAME),
            scope: scope_machine,
            config_path: outcome.config_path.display().to_string(),
            config_removed: outcome.config_removed,
            secrets: &outcome.secrets,
            project_still_references: outcome.project_still_references,
        };
        println!("{}", serde_json::to_string_pretty(&env).unwrap());
        return Ok(());
    }

    println!("{} credentials deleted ({scope_label}).", T::DISPLAY);
    println!(
        "  config : {} ({})",
        outcome.config_path.display(),
        if outcome.config_removed {
            "removed"
        } else {
            "not present"
        }
    );
    for s in &outcome.secrets {
        println!("  {} : OS keychain entry `{}` cleared", s.label, s.account);
    }

    if outcome.project_still_references {
        let project_path = zad::config::path::project_config_path()?;
        println!();
        println!(
            "warning: this project still references the {} service ({}).",
            T::NAME,
            project_path.display()
        );
        println!(
            "         Run `zad service disable {}` to remove the `[service.{}]` entry.",
            T::NAME,
            T::NAME
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Shared prompt helpers (the only place this module touches dialoguer)
// ---------------------------------------------------------------------------

pub fn resolve_bot_token(
    flag: Option<&str>,
    env_flag: Option<&str>,
    non_interactive: bool,
    display: &str,
) -> Result<String> {
    if let Some(env) = env_flag {
        return std::env::var(env).map_err(|_| ZadError::MissingEnv(env.to_string()));
    }
    if let Some(v) = flag {
        return Ok(v.to_string());
    }
    if non_interactive {
        return Err(ZadError::MissingRequired("--bot-token or --bot-token-env"));
    }
    let v = dialoguer::Password::with_theme(&dialoguer::theme::ColorfulTheme::default())
        .with_prompt(format!("{display} bot token"))
        .interact()
        .into_zad()?;
    Ok(v)
}

pub fn resolve_scopes(
    flag: Option<&[String]>,
    default_scopes: &[&'static str],
    all_scopes: &[&'static str],
    non_interactive: bool,
) -> Result<Vec<String>> {
    if let Some(list) = flag {
        let cleaned: Vec<String> = list
            .iter()
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();
        for s in &cleaned {
            if !all_scopes.contains(&s.as_str()) {
                return Err(ZadError::Invalid(format!("unknown scope: {s}")));
            }
        }
        return Ok(cleaned);
    }
    if non_interactive {
        return Ok(default_scopes.iter().map(|s| s.to_string()).collect());
    }
    let defaults: Vec<bool> = all_scopes
        .iter()
        .map(|s| default_scopes.contains(s))
        .collect();
    let picks = dialoguer::MultiSelect::with_theme(&dialoguer::theme::ColorfulTheme::default())
        .with_prompt("Scopes (space to toggle, enter to confirm)")
        .items(all_scopes)
        .defaults(&defaults)
        .interact()
        .into_zad()?;
    Ok(picks
        .into_iter()
        .map(|i| all_scopes[i].to_string())
        .collect())
}

// ---------------------------------------------------------------------------
// Rendering helpers
// ---------------------------------------------------------------------------

fn label_width(
    cfg_lines: &[(&'static str, String)],
    scopes: &[String],
    secrets: &[SecretRef],
) -> usize {
    let mut w = "scopes".len();
    for (l, _) in cfg_lines {
        w = w.max(l.len());
    }
    for s in secrets {
        w = w.max(s.label.len());
    }
    let _ = scopes;
    w
}