wme-cli 0.1.3

CLI tool for the Wikimedia Enterprise API
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
//! Wikimedia Enterprise CLI
//!
//! A command-line interface for interacting with the Wikimedia Enterprise API.
//!
//! # Overview
//!
//! This CLI provides access to:
//! - **Authentication**: Login, token refresh, and password management
//! - **Metadata**: Browse project codes, languages, projects, and namespaces
//! - **Articles**: Fetch individual articles and structured data
//! - **Snapshots**: List, inspect, and download snapshot files
//! - **Realtime**: Stream live updates and manage realtime batches
//!
//! # Architecture
//!
//! The CLI is organized into modules:
//! - `client`: HTTP client builder and error mapping
//! - `commands`: CLI command implementations (auth, meta, article, snapshot, realtime)
//! - `config`: Configuration management (tokens stored in ~/.wme/config.json)
//! - `output`: Output formatting (JSON, NDJSON, table, quiet)
//! - `util`: Utility functions (formatting, prompts, parameter building)
//!
//! # Usage
//!
//! ```bash
//! # Authenticate
//! wme auth login
//!
//! # List project codes
//! wme meta project-codes
//!
//! # Get an article
//! wme article get "Main_Page"
//!
//! # List snapshots
//! wme snapshot list
//! ```

use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use tracing::info;

mod client;
mod commands;
mod config;
mod output;
mod util;

use commands::{article, auth, meta, realtime, snapshot};
use config::Config;
use output::OutputFormat;

/// Wikimedia Enterprise CLI
///
/// Command-line interface for the Wikimedia Enterprise API.
/// Run `wme --help` to see available commands.
#[derive(Parser)]
#[command(name = "wme")]
#[command(about = "CLI for Wikimedia Enterprise API")]
#[command(version)]
#[command(propagate_version = true)]
struct Cli {
    /// Bearer access token (overrides stored token)
    #[arg(long, global = true, env = "WME_TOKEN")]
    token: Option<String>,

    /// Output format
    #[arg(short, long, global = true, default_value = "json")]
    output: OutputFormat,

    /// Comma-separated fields to include in response
    #[arg(long, global = true)]
    fields: Option<String>,

    /// Field filter as field=value (repeatable)
    #[arg(long, global = true)]
    filter: Vec<String>,

    /// Max results to return
    #[arg(long, global = true)]
    limit: Option<usize>,

    /// Path to config file
    #[arg(long, global = true, env = "WME_CONFIG")]
    config: Option<PathBuf>,

    /// Enable verbose logging
    #[arg(short, long, global = true)]
    verbose: bool,

    /// Subcommand to run
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Authentication commands
    Auth {
        #[command(subcommand)]
        command: AuthCommands,
    },

    /// Metadata commands
    Meta {
        #[command(subcommand)]
        command: MetaCommands,
    },

    /// Article on-demand API commands
    Article {
        #[command(subcommand)]
        command: ArticleCommands,
    },

    /// Snapshot API commands
    Snapshot {
        #[command(subcommand)]
        command: SnapshotCommands,
    },

    /// Realtime API commands
    Realtime {
        #[command(subcommand)]
        command: RealtimeCommands,
    },
}

#[derive(Subcommand)]
enum AuthCommands {
    /// Login and store tokens
    Login {
        /// API username
        #[arg(short, long, env = "WME_USERNAME")]
        username: Option<String>,

        /// API password
        #[arg(short, long, env = "WME_PASSWORD")]
        password: Option<String>,
    },

    /// Refresh access token
    Refresh {
        /// API username
        #[arg(short, long, env = "WME_USERNAME")]
        username: Option<String>,

        /// Refresh token
        #[arg(long)]
        refresh_token: Option<String>,
    },

    /// Revoke the refresh token
    Revoke {
        /// Refresh token to revoke
        #[arg(long)]
        refresh_token: Option<String>,
    },

    /// Trigger forgot password email
    ForgotPassword {
        /// Username
        #[arg(long)]
        username: String,
    },

    /// Confirm forgot password with code
    ForgotPasswordConfirm {
        /// Username
        #[arg(long)]
        username: String,

        /// New password
        #[arg(long)]
        password: String,

        /// Confirmation code from email
        #[arg(long)]
        code: String,
    },

    /// Change password (requires authentication)
    ChangePassword {
        /// Previous password
        #[arg(long)]
        previous_password: String,

        /// Proposed new password
        #[arg(long)]
        proposed_password: String,
    },

    /// Set new password for NEW_PASSWORD_REQUIRED challenge
    SetNewPassword {
        /// Username
        #[arg(long)]
        username: String,

        /// Session string
        #[arg(long)]
        session: String,

        /// New password
        #[arg(long)]
        new_password: String,
    },
}

#[derive(Subcommand)]
enum MetaCommands {
    /// List all project codes
    ProjectCodes,

    /// Get specific project code info
    #[command(name = "project-codes-get")]
    ProjectCodesGet {
        /// Project code
        #[arg(value_name = "CODE")]
        code: String,
    },

    /// List all languages
    Languages,

    /// Get specific language info
    #[command(name = "languages-get")]
    LanguagesGet {
        /// Language identifier
        #[arg(value_name = "IDENTIFIER")]
        identifier: String,
    },

    /// List all projects
    Projects,

    /// Get specific project info
    #[command(name = "projects-get")]
    ProjectsGet {
        /// Project identifier
        #[arg(value_name = "IDENTIFIER")]
        identifier: String,
    },

    /// List all namespaces
    Namespaces,

    /// Get specific namespace info
    #[command(name = "namespaces-get")]
    NamespacesGet {
        /// Namespace ID
        #[arg(value_name = "ID")]
        id: u32,
    },
}

#[derive(Subcommand)]
enum ArticleCommands {
    /// Get a single article
    Get {
        /// Article name
        #[arg(value_name = "NAME")]
        name: String,
    },

    /// Get structured article data (BETA)
    Structured {
        /// Article name
        #[arg(value_name = "NAME")]
        name: String,
    },
}

#[derive(Subcommand)]
enum SnapshotCommands {
    /// List available snapshots
    List,

    /// Get snapshot metadata
    Info {
        /// Snapshot identifier
        #[arg(value_name = "IDENTIFIER")]
        identifier: String,
    },

    /// Download a snapshot
    Download {
        /// Snapshot identifier
        #[arg(value_name = "IDENTIFIER")]
        identifier: String,

        /// Output file path
        #[arg(short = 'f', long = "output-file")]
        output_file: Option<PathBuf>,

        /// HTTP Range header for resumable download
        #[arg(long)]
        range: Option<String>,
    },

    /// Chunk commands
    Chunks {
        #[command(subcommand)]
        command: SnapshotChunkCommands,
    },

    /// Structured snapshot commands (BETA)
    Structured {
        #[command(subcommand)]
        command: StructuredSnapshotCommands,
    },
}

#[derive(Subcommand)]
enum SnapshotChunkCommands {
    /// List chunks for a snapshot
    List {
        /// Snapshot identifier
        #[arg(value_name = "SNAPSHOT_ID")]
        snapshot_id: String,
    },

    /// Get chunk metadata
    Info {
        /// Snapshot identifier
        #[arg(value_name = "SNAPSHOT_ID")]
        snapshot_id: String,

        /// Chunk identifier
        #[arg(value_name = "CHUNK_ID")]
        chunk_id: String,
    },

    /// Download a chunk
    Download {
        /// Snapshot identifier
        #[arg(value_name = "SNAPSHOT_ID")]
        snapshot_id: String,

        /// Chunk identifier
        #[arg(value_name = "CHUNK_ID")]
        chunk_id: String,

        /// Output file path
        #[arg(short = 'f', long = "output-file")]
        output_file: Option<PathBuf>,

        /// HTTP Range header for resumable download
        #[arg(long)]
        range: Option<String>,
    },
}

#[derive(Subcommand)]
enum StructuredSnapshotCommands {
    /// List structured snapshots
    List,

    /// Get structured snapshot metadata
    Info {
        /// Snapshot identifier
        #[arg(value_name = "IDENTIFIER")]
        identifier: String,
    },

    /// Download structured snapshot
    Download {
        /// Snapshot identifier
        #[arg(value_name = "IDENTIFIER")]
        identifier: String,

        /// Output file path
        #[arg(short = 'f', long = "output-file")]
        output_file: Option<PathBuf>,

        /// HTTP Range header for resumable download
        #[arg(long)]
        range: Option<String>,
    },
}

#[derive(Subcommand)]
enum RealtimeCommands {
    /// Connect to realtime SSE stream
    Stream {
        /// Resume from timestamp (RFC3339)
        #[arg(long)]
        since: Option<String>,

        /// Partition numbers (repeatable)
        #[arg(long)]
        parts: Vec<u32>,

        /// Partition:offset map as JSON
        #[arg(long)]
        offsets: Option<String>,

        /// Partition:timestamp map as JSON
        #[arg(long = "since-per-partition")]
        since_per_partition: Option<String>,

        /// Request NDJSON instead of SSE
        #[arg(long)]
        ndjson: bool,
    },

    /// Batch commands
    Batches {
        #[command(subcommand)]
        command: RealtimeBatchCommands,
    },
}

#[derive(Subcommand)]
enum RealtimeBatchCommands {
    /// List batches for a date/hour
    List {
        /// Date (YYYY-MM-DD)
        #[arg(value_name = "DATE")]
        date: String,

        /// Hour (00-23)
        #[arg(value_name = "HOUR")]
        hour: String,
    },

    /// Get batch metadata
    Info {
        /// Date (YYYY-MM-DD)
        #[arg(value_name = "DATE")]
        date: String,

        /// Hour (00-23)
        #[arg(value_name = "HOUR")]
        hour: String,

        /// Batch identifier
        #[arg(value_name = "IDENTIFIER")]
        identifier: String,
    },

    /// Download a batch
    Download {
        /// Date (YYYY-MM-DD)
        #[arg(value_name = "DATE")]
        date: String,

        /// Hour (00-23)
        #[arg(value_name = "HOUR")]
        hour: String,

        /// Batch identifier
        #[arg(value_name = "IDENTIFIER")]
        identifier: String,

        /// Output file path
        #[arg(short = 'f', long = "output-file")]
        output_file: Option<PathBuf>,

        /// HTTP Range header for resumable download
        #[arg(long)]
        range: Option<String>,
    },
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    // Setup logging
    let level = if cli.verbose { "debug" } else { "warn" };
    tracing_subscriber::fmt()
        .with_env_filter(format!("wme_cli={level},warn"))
        .init();

    // Load config
    let config = Config::load(cli.config.as_deref())?;

    // Parse global options
    let global_opts = commands::GlobalOpts {
        token: cli.token,
        output: cli.output,
        fields: cli.fields,
        filter: cli.filter,
        limit: cli.limit,
        config,
    };

    info!("Starting wme CLI");

    match cli.command {
        Commands::Auth { command } => match command {
            AuthCommands::Login { username, password } => {
                auth::login(username, password, &global_opts).await?;
            }
            AuthCommands::Refresh {
                username,
                refresh_token,
            } => {
                auth::refresh(username, refresh_token, &global_opts).await?;
            }
            AuthCommands::Revoke { refresh_token } => {
                auth::revoke(refresh_token, &global_opts).await?;
            }
            AuthCommands::ForgotPassword { username } => {
                auth::forgot_password(&username, &global_opts).await?;
            }
            AuthCommands::ForgotPasswordConfirm {
                username,
                password,
                code,
            } => {
                auth::forgot_password_confirm(&username, &password, &code, &global_opts).await?;
            }
            AuthCommands::ChangePassword {
                previous_password,
                proposed_password,
            } => {
                auth::change_password(&previous_password, &proposed_password, &global_opts).await?;
            }
            AuthCommands::SetNewPassword {
                username,
                session,
                new_password,
            } => {
                auth::set_new_password(&username, &session, &new_password, &global_opts).await?;
            }
        },
        Commands::Meta { command } => match command {
            MetaCommands::ProjectCodes => {
                meta::project_codes(&global_opts).await?;
            }
            MetaCommands::ProjectCodesGet { code } => {
                meta::project_codes_get(&code, &global_opts).await?;
            }
            MetaCommands::Languages => {
                meta::languages(&global_opts).await?;
            }
            MetaCommands::LanguagesGet { identifier } => {
                meta::languages_get(&identifier, &global_opts).await?;
            }
            MetaCommands::Projects => {
                meta::projects(&global_opts).await?;
            }
            MetaCommands::ProjectsGet { identifier } => {
                meta::projects_get(&identifier, &global_opts).await?;
            }
            MetaCommands::Namespaces => {
                meta::namespaces(&global_opts).await?;
            }
            MetaCommands::NamespacesGet { id } => {
                meta::namespaces_get(id, &global_opts).await?;
            }
        },
        Commands::Article { command } => match command {
            ArticleCommands::Get { name } => {
                article::get(&name, &global_opts).await?;
            }
            ArticleCommands::Structured { name } => {
                article::structured(&name, &global_opts).await?;
            }
        },
        Commands::Snapshot { command } => match command {
            SnapshotCommands::List => {
                snapshot::list(&global_opts).await?;
            }
            SnapshotCommands::Info { identifier } => {
                snapshot::info(&identifier, &global_opts).await?;
            }
            SnapshotCommands::Download {
                identifier,
                output_file,
                range,
            } => {
                snapshot::download(&identifier, output_file, range, &global_opts).await?;
            }
            SnapshotCommands::Chunks { command } => match command {
                SnapshotChunkCommands::List { snapshot_id } => {
                    snapshot::chunks_list(&snapshot_id, &global_opts).await?;
                }
                SnapshotChunkCommands::Info {
                    snapshot_id,
                    chunk_id,
                } => {
                    snapshot::chunks_info(&snapshot_id, &chunk_id, &global_opts).await?;
                }
                SnapshotChunkCommands::Download {
                    snapshot_id,
                    chunk_id,
                    output_file,
                    range,
                } => {
                    snapshot::chunks_download(
                        &snapshot_id,
                        &chunk_id,
                        output_file,
                        range,
                        &global_opts,
                    )
                    .await?;
                }
            },
            SnapshotCommands::Structured { command } => match command {
                StructuredSnapshotCommands::List => {
                    snapshot::structured_list(&global_opts).await?;
                }
                StructuredSnapshotCommands::Info { identifier } => {
                    snapshot::structured_info(&identifier, &global_opts).await?;
                }
                StructuredSnapshotCommands::Download {
                    identifier,
                    output_file,
                    range,
                } => {
                    snapshot::structured_download(&identifier, output_file, range, &global_opts)
                        .await?;
                }
            },
        },
        Commands::Realtime { command } => match command {
            RealtimeCommands::Stream {
                since,
                parts,
                offsets,
                since_per_partition,
                ndjson,
            } => {
                realtime::stream(
                    since,
                    parts,
                    offsets,
                    since_per_partition,
                    ndjson,
                    &global_opts,
                )
                .await?;
            }
            RealtimeCommands::Batches { command } => match command {
                RealtimeBatchCommands::List { date, hour } => {
                    realtime::batches_list(&date, &hour, &global_opts).await?;
                }
                RealtimeBatchCommands::Info {
                    date,
                    hour,
                    identifier,
                } => {
                    realtime::batches_info(&date, &hour, &identifier, &global_opts).await?;
                }
                RealtimeBatchCommands::Download {
                    date,
                    hour,
                    identifier,
                    output_file,
                    range,
                } => {
                    realtime::batches_download(
                        &date,
                        &hour,
                        &identifier,
                        output_file,
                        range,
                        &global_opts,
                    )
                    .await?;
                }
            },
        },
    }

    Ok(())
}