zoom-cli 0.0.1

Agent-friendly Zoom CLI with JSON output, structured exit codes, and schema introspection
Documentation
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
use std::future::Future;
use std::io::{BufRead, IsTerminal, Write};
use std::path::Path;

use owo_colors::OwoColorize;

use crate::api::ApiError;
use crate::config;

const CORE_SCOPES: &[&str] = &[
    "meeting:read:list_meetings:master",
    "meeting:read:meeting:master",
    "meeting:write:meeting:master",
    "recording:read:list_user_recordings:master",
    "user:read:user:master",
    "user:read:list_users:master",
];

const OPTIONAL_SCOPES: &[&str] = &[
    "meeting:write:meeting:admin",
    "meeting:read:list_past_meeting_participants:admin",
    "report:read:user:admin",
    "recording:write:recording:master",
];

fn sym_q() -> String {
    "?".green().to_string()
}

fn sym_ok() -> String {
    "".green().to_string()
}

fn sym_fail() -> String {
    "".red().to_string()
}

fn mask_credential(s: &str) -> String {
    if s.len() <= 10 {
        return "".repeat(s.len());
    }
    format!("{}{}", &s[..6], &s[s.len() - 4..])
}

fn prompt_optional<R: BufRead, W: Write>(
    reader: &mut R,
    writer: &mut W,
    label: &str,
    default: &str,
) -> String {
    write!(writer, "{} {} ({}): ", sym_q(), label, default.dimmed()).unwrap();
    writer.flush().unwrap();

    let mut input = String::new();
    reader.read_line(&mut input).unwrap_or(0);
    let trimmed = input.trim().to_owned();
    if trimmed.is_empty() { default.to_owned() } else { trimmed }
}

fn prompt_required<R: BufRead, W: Write>(
    reader: &mut R,
    writer: &mut W,
    label: &str,
    hint: &str,
) -> String {
    loop {
        write!(writer, "{} {} [{}]: ", sym_q(), label, hint.dimmed()).unwrap();
        writer.flush().unwrap();

        let mut input = String::new();
        reader.read_line(&mut input).unwrap_or(0);
        let trimmed = input.trim().to_owned();
        if !trimmed.is_empty() {
            return trimmed;
        }
        writeln!(writer, "  {} {} is required.", sym_fail(), label).unwrap();
    }
}

fn prompt_confirm<R: BufRead, W: Write>(
    reader: &mut R,
    writer: &mut W,
    label: &str,
    default_yes: bool,
) -> bool {
    let hint = if default_yes { "Y/n" } else { "y/N" };
    write!(writer, "{} {} [{}]: ", sym_q(), label, hint.dimmed()).unwrap();
    writer.flush().unwrap();

    let mut input = String::new();
    reader.read_line(&mut input).unwrap_or(0);
    match input.trim().to_lowercase().as_str() {
        "y" | "yes" => true,
        "n" | "no" => false,
        _ => default_yes,
    }
}

fn print_json_schema(config_path: &Path) {
    let path_str = config_path.to_string_lossy();
    let schema = serde_json::json!({
        "configPath": path_str,
        "tokenInstructions": {
            "steps": [
                "Go to https://marketplace.zoom.us/develop/create",
                "Click 'Build App', choose 'Server-to-Server OAuth'",
                "Add the required scopes (see requiredScopes)",
                "Activate the app",
                "Copy Account ID, Client ID, and Client Secret from the app credentials page"
            ]
        },
        "requiredCredentials": ["account_id", "client_id", "client_secret"],
        "requiredScopes": CORE_SCOPES,
        "optionalScopes": OPTIONAL_SCOPES,
        "example": {
            "configFile": path_str,
            "format": "[default]\naccount_id = \"YOUR_ACCOUNT_ID\"\nclient_id = \"YOUR_CLIENT_ID\"\nclient_secret = \"YOUR_CLIENT_SECRET\""
        }
    });
    println!("{}", serde_json::to_string_pretty(&schema).expect("serialize"));
}

fn load_existing_profile_names(config_path: &Path) -> Vec<String> {
    let content = match std::fs::read_to_string(config_path) {
        Ok(c) => c,
        Err(_) => return Vec::new(),
    };
    let table: toml::Table = match toml::from_str(&content) {
        Ok(t) => t,
        Err(_) => return Vec::new(),
    };
    table.keys().cloned().collect()
}

/// Interactive init flow with injectable IO and validator for testing.
///
/// `validate` receives (account_id, client_id, client_secret) and returns
/// `Some(display_name)` on success or `None` on auth failure.
pub async fn run_init<R, W, Fut>(
    reader: &mut R,
    writer: &mut W,
    config_path: &Path,
    profile_arg: Option<&str>,
    validate: impl Fn(String, String, String) -> Fut,
) -> Result<(), ApiError>
where
    R: BufRead,
    W: Write,
    Fut: Future<Output = Option<String>>,
{
    let existing = load_existing_profile_names(config_path);

    if existing.is_empty() {
        writeln!(writer, "\nWelcome to zoom-cli!\n").unwrap();
        writeln!(
            writer,
            "This tool authenticates via a Zoom Server-to-Server OAuth app."
        )
        .unwrap();
        writeln!(writer, "You'll need to create one at https://marketplace.zoom.us\n").unwrap();
    } else {
        writeln!(
            writer,
            "\nUpdating zoom-cli config — existing profiles: {}\n",
            existing.join(", ")
        )
        .unwrap();
    }

    writeln!(writer, "Set up your Zoom Server-to-Server OAuth app:").unwrap();
    writeln!(writer, "  1. https://marketplace.zoom.us/develop/create").unwrap();
    writeln!(writer, "  2. Build App → Server-to-Server OAuth").unwrap();
    writeln!(writer, "  3. Add scopes:\n").unwrap();
    writeln!(writer, "     Core (required):").unwrap();
    for scope in CORE_SCOPES {
        writeln!(writer, "{scope}").unwrap();
    }
    writeln!(writer, "\n     Optional (end/participants/reports/recording-control):").unwrap();
    for scope in OPTIONAL_SCOPES {
        writeln!(writer, "{scope}").unwrap();
    }
    writeln!(writer, "\n  4. Activate the app\n").unwrap();

    write!(writer, "Press Enter when your app is ready (Ctrl+C to abort)... ").unwrap();
    writer.flush().unwrap();
    let mut _buf = String::new();
    reader.read_line(&mut _buf).unwrap_or(0);
    writeln!(writer).unwrap();

    let profile_name = if let Some(p) = profile_arg {
        p.to_owned()
    } else {
        prompt_optional(reader, writer, "Profile name", "default")
    };

    let account_id = prompt_required(reader, writer, "Account ID", "from app credentials");
    let client_id = prompt_required(reader, writer, "Client ID", "from app credentials");
    let client_secret = prompt_required(reader, writer, "Client Secret", "from app credentials");

    writeln!(writer).unwrap();
    writeln!(writer, "  Profile:       {}", profile_name.bold()).unwrap();
    writeln!(writer, "  Account ID:    {}", mask_credential(&account_id)).unwrap();
    writeln!(writer, "  Client ID:     {}", mask_credential(&client_id)).unwrap();
    writeln!(writer, "  Client Secret: {}\n", mask_credential(&client_secret)).unwrap();

    write!(writer, "{} Validating credentials... ", sym_q()).unwrap();
    writer.flush().unwrap();
    let validation = validate(account_id.clone(), client_id.clone(), client_secret.clone()).await;

    let save = match validation {
        Some(display_name) => {
            writeln!(writer, "{} Connected as {}", sym_ok(), display_name.bold()).unwrap();
            true
        }
        None => {
            writeln!(writer, "{} Could not validate credentials.", sym_fail()).unwrap();
            prompt_confirm(reader, writer, "Save anyway?", false)
        }
    };

    writeln!(writer).unwrap();

    if !save {
        writeln!(writer, "Aborted. Config not saved.").unwrap();
        writer.flush().unwrap();
        return Ok(());
    }

    config::write_profile(
        config_path,
        &profile_name,
        &account_id,
        &client_id,
        &client_secret,
    )?;

    writeln!(
        writer,
        "{} Config saved to {}\n",
        sym_ok(),
        config_path.display().to_string().bold()
    )
    .unwrap();
    writeln!(writer, "Run: {}", "zoom users me".bold()).unwrap();
    writer.flush().unwrap();

    Ok(())
}

/// Entry point from main — uses real stdin/stdout and live API validation.
pub async fn init(profile_arg: Option<String>) -> Result<(), ApiError> {
    let config_path = config::config_path();

    if !std::io::stdout().is_terminal() {
        print_json_schema(&config_path);
        return Ok(());
    }

    let stdin = std::io::stdin();
    let stdout = std::io::stdout();
    let mut reader = std::io::BufReader::new(stdin.lock());
    let mut writer = std::io::BufWriter::new(stdout.lock());

    run_init(
        &mut reader,
        &mut writer,
        &config_path,
        profile_arg.as_deref(),
        |account_id, client_id, client_secret| async move {
            let mut client =
                crate::api::ZoomClient::new(account_id, client_id, client_secret);
            match client.get_user("me").await {
                Ok(user) => Some(user.display_name.unwrap_or(user.email)),
                Err(_) => None,
            }
        },
    )
    .await
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use tempfile::TempDir;

    use super::*;

    fn fake_path(dir: &TempDir) -> std::path::PathBuf {
        dir.path().join("config.toml")
    }

    #[tokio::test]
    async fn init_writes_config_on_valid_credentials() {
        let dir = TempDir::new().unwrap();
        let path = fake_path(&dir);

        // Enter: ready, default profile, account, client_id, secret
        let input = b"\n\ntest-account-id\ntest-client-id\ntest-client-secret\n";
        let mut reader = Cursor::new(input.as_ref());
        let mut writer = Vec::<u8>::new();

        run_init(
            &mut reader,
            &mut writer,
            &path,
            None,
            |a, b, c| async move {
                let _ = (a, b, c);
                Some("Alice Smith".into())
            },
        )
        .await
        .unwrap();

        let saved = std::fs::read_to_string(&path).unwrap();
        assert!(saved.contains("account_id"));
        assert!(saved.contains("test-account-id"));
        assert!(saved.contains("test-client-id"));
        assert!(saved.contains("test-client-secret"));
    }

    #[tokio::test]
    async fn init_uses_default_profile_name_when_empty() {
        let dir = TempDir::new().unwrap();
        let path = fake_path(&dir);

        let input = b"\n\ntest-acct\ntest-cid\ntest-csec\n";
        let mut reader = Cursor::new(input.as_ref());
        let mut writer = Vec::<u8>::new();

        run_init(
            &mut reader,
            &mut writer,
            &path,
            None,
            |a, b, c| async move {
                let _ = (a, b, c);
                Some("Test User".into())
            },
        )
        .await
        .unwrap();

        let saved = std::fs::read_to_string(&path).unwrap();
        assert!(saved.contains("[default]"), "should use 'default' profile name");
    }

    #[tokio::test]
    async fn init_with_profile_arg_skips_profile_prompt() {
        let dir = TempDir::new().unwrap();
        let path = fake_path(&dir);

        // One fewer line (no profile name prompt)
        let input = b"\ntest-acct\ntest-cid\ntest-csec\n";
        let mut reader = Cursor::new(input.as_ref());
        let mut writer = Vec::<u8>::new();

        run_init(
            &mut reader,
            &mut writer,
            &path,
            Some("work"),
            |a, b, c| async move {
                let _ = (a, b, c);
                Some("Alice".into())
            },
        )
        .await
        .unwrap();

        let saved = std::fs::read_to_string(&path).unwrap();
        assert!(saved.contains("[work]"));
    }

    #[tokio::test]
    async fn init_aborts_when_validation_fails_and_user_declines_save() {
        let dir = TempDir::new().unwrap();
        let path = fake_path(&dir);

        let input = b"\n\ntest-acct\ntest-cid\ntest-csec\nn\n";
        let mut reader = Cursor::new(input.as_ref());
        let mut writer = Vec::<u8>::new();

        run_init(
            &mut reader,
            &mut writer,
            &path,
            None,
            |a, b, c| async move {
                let _ = (a, b, c);
                None
            },
        )
        .await
        .unwrap();

        assert!(!path.exists(), "config should not be written after abort");
    }

    #[tokio::test]
    async fn init_saves_when_validation_fails_but_user_forces_save() {
        let dir = TempDir::new().unwrap();
        let path = fake_path(&dir);

        let input = b"\n\ntest-acct\ntest-cid\ntest-csec\ny\n";
        let mut reader = Cursor::new(input.as_ref());
        let mut writer = Vec::<u8>::new();

        run_init(
            &mut reader,
            &mut writer,
            &path,
            None,
            |a, b, c| async move {
                let _ = (a, b, c);
                None
            },
        )
        .await
        .unwrap();

        assert!(path.exists(), "config should be saved when user chooses to save anyway");
    }

    #[tokio::test]
    async fn init_overwrites_existing_profile() {
        let dir = TempDir::new().unwrap();
        let path = fake_path(&dir);
        std::fs::write(
            &path,
            "[default]\naccount_id = \"old\"\nclient_id = \"old\"\nclient_secret = \"old\"\n",
        )
        .unwrap();

        let input = b"\n\nnew-account\nnew-client\nnew-secret\n";
        let mut reader = Cursor::new(input.as_ref());
        let mut writer = Vec::<u8>::new();

        run_init(
            &mut reader,
            &mut writer,
            &path,
            None,
            |a, b, c| async move {
                let _ = (a, b, c);
                Some("Alice".into())
            },
        )
        .await
        .unwrap();

        let saved = std::fs::read_to_string(&path).unwrap();
        assert!(saved.contains("new-account"));
        assert!(!saved.contains("\"old\""), "old values should be overwritten");
    }

    #[test]
    fn mask_credential_masks_long_values() {
        assert_eq!(mask_credential("abcdefghijklmnop"), "abcdef…mnop");
    }

    #[test]
    fn mask_credential_dots_short_values() {
        assert_eq!(mask_credential("short"), "•••••");
        assert_eq!(mask_credential(""), "");
    }
}