ngdp-client 0.4.3

Command-line interface for Blizzard's NGDP with product queries, certificate management, and key operations
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
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! NGDP client library
//!
//! This library provides the core functionality for the ngdp CLI tool.

pub mod cached_client;
pub mod cdn_config;
pub mod commands;
pub mod config_manager;
pub mod fallback_client;
pub mod manifest;
pub mod output;
pub mod pattern_extraction;
pub mod wago_api;

/// Common test constants
pub mod test_constants {
    /// Example certificate hash used throughout tests and examples
    /// SKI: 5168ff90af0207753cccd9656462a212b859723b
    pub const EXAMPLE_CERT_HASH: &str = "5168ff90af0207753cccd9656462a212b859723b";
}

// Re-export command handlers
pub use crate::commands::{
    certs::handle as handle_certs, config::handle as handle_config,
    download::handle as handle_download, inspect::handle as handle_inspect,
    install::handle as handle_install, listfile::handle as handle_listfile,
    products::handle as handle_products, storage::handle as handle_storage,
};

use clap::Subcommand;
use std::path::PathBuf;

#[derive(Subcommand)]
pub enum ProductsCommands {
    /// List all available products
    List {
        /// Filter by product name pattern
        #[arg(short, long)]
        filter: Option<String>,

        /// Region to query
        #[arg(short, long, default_value = "us")]
        region: String,
    },

    /// Show versions for a specific product
    Versions {
        /// Product name (e.g., wow, d3, agent)
        product: String,

        /// Region to query
        #[arg(short, long, default_value = "us")]
        region: String,

        /// Show all regions
        #[arg(short, long)]
        all_regions: bool,

        /// Parse and show build configuration details
        #[arg(long)]
        parse_config: bool,
    },

    /// Show CDN configuration for a product
    Cdns {
        /// Product name
        product: String,

        /// Region to query
        #[arg(short, long, default_value = "us")]
        region: String,
    },

    /// Get detailed information about a product
    Info {
        /// Product name
        product: String,

        /// Region to query (omit to show all regions)
        #[arg(short, long)]
        region: Option<String>,
    },

    /// Show all historical builds for a product
    Builds {
        /// Product name (e.g., wow, wowt, wowxptr)
        product: String,

        /// Filter by version pattern
        #[arg(short, long)]
        filter: Option<String>,

        /// Show only builds from the last N days
        #[arg(long)]
        days: Option<u32>,

        /// Limit number of results (default: show all)
        #[arg(long)]
        limit: Option<usize>,

        /// Show only background download builds
        #[arg(long)]
        bgdl_only: bool,
    },
}

#[derive(Subcommand)]
pub enum StorageCommands {
    /// Initialize a new CASC storage
    Init {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Product to initialize for
        #[arg(short, long)]
        product: Option<String>,
    },

    /// Show storage information
    Info {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,
    },

    /// Show NGDP configuration information from WoW installation
    Config {
        /// Path to WoW installation directory
        #[arg(default_value = ".")]
        path: PathBuf,
    },

    /// Show detailed storage statistics
    Stats {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,
    },

    /// Verify storage integrity
    Verify {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Fix corrupted files
        #[arg(short, long)]
        fix: bool,
    },

    /// Read a file by EKey
    Read {
        /// Path to storage directory
        path: PathBuf,

        /// Encoding key (hex)
        ekey: String,

        /// Output file (defaults to stdout)
        #[arg(short = 'O', long)]
        output: Option<PathBuf>,
    },

    /// Write a file to storage
    Write {
        /// Path to storage directory
        path: PathBuf,

        /// Encoding key (hex)
        ekey: String,

        /// Input file (defaults to stdin)
        #[arg(short = 'I', long)]
        input: Option<PathBuf>,
    },

    /// List all files in storage
    List {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Show detailed information
        #[arg(short, long)]
        detailed: bool,

        /// Limit number of results
        #[arg(short = 'n', long)]
        limit: Option<usize>,
    },

    /// Rebuild storage indices
    Rebuild {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Force rebuild even if indices seem valid
        #[arg(short, long)]
        force: bool,
    },

    /// Optimize storage for performance
    Optimize {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,
    },

    /// Repair corrupted storage
    Repair {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Dry run (don't actually repair)
        #[arg(short = 'n', long)]
        dry_run: bool,
    },

    /// Clean up unused data
    Clean {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Dry run (don't actually delete)
        #[arg(short = 'n', long)]
        dry_run: bool,
    },

    /// Extract a file by EKey with optional filename resolution
    Extract {
        /// Encoding key (hex)
        ekey: String,

        /// Path to storage directory
        #[arg(long, default_value = ".")]
        path: PathBuf,

        /// Output file path (optional)
        #[arg(short = 'O', long)]
        output: Option<PathBuf>,

        /// Path to community listfile for filename resolution
        #[arg(long)]
        listfile: Option<PathBuf>,

        /// Resolve filename using listfile and TACT manifests
        #[arg(long)]
        resolve_filename: bool,
    },

    /// Extract a file by FileDataID (requires TACT manifests)
    ExtractById {
        /// FileDataID to extract
        fdid: u32,

        /// Path to storage directory
        #[arg(long, default_value = ".")]
        path: PathBuf,

        /// Output file path (optional)
        #[arg(short = 'O', long)]
        output: Option<PathBuf>,

        /// Path to root manifest file
        #[arg(long)]
        root_manifest: Option<PathBuf>,

        /// Path to encoding manifest file
        #[arg(long)]
        encoding_manifest: Option<PathBuf>,
    },

    /// Extract a file by filename (requires TACT manifests and listfile)
    ExtractByName {
        /// Filename to extract
        filename: String,

        /// Path to storage directory
        #[arg(long, default_value = ".")]
        path: PathBuf,

        /// Output file path (optional)
        #[arg(short = 'O', long)]
        output: Option<PathBuf>,

        /// Path to root manifest file
        #[arg(long)]
        root_manifest: Option<PathBuf>,

        /// Path to encoding manifest file
        #[arg(long)]
        encoding_manifest: Option<PathBuf>,

        /// Path to community listfile for filename resolution
        #[arg(long)]
        listfile: Option<PathBuf>,
    },

    /// Load TACT manifests for enhanced operations
    LoadManifests {
        /// Path to storage directory
        #[arg(default_value = ".")]
        path: PathBuf,

        /// Path to root manifest file
        #[arg(long)]
        root_manifest: Option<PathBuf>,

        /// Path to encoding manifest file
        #[arg(long)]
        encoding_manifest: Option<PathBuf>,

        /// Path to community listfile for filename resolution
        #[arg(long)]
        listfile: Option<PathBuf>,

        /// Locale to use for filtering (default: all)
        #[arg(long, default_value = "all")]
        locale: String,

        /// Only show info, don't persist
        #[arg(long)]
        info_only: bool,
    },
}

#[derive(Subcommand)]
pub enum ListfileCommands {
    /// Download the latest community listfile
    Download {
        /// Output directory for listfile
        #[arg(long, default_value = ".")]
        output: PathBuf,

        /// Force download even if file exists
        #[arg(short, long)]
        force: bool,
    },

    /// Show listfile information
    Info {
        /// Path to listfile
        #[arg(default_value = "community-listfile.csv")]
        path: PathBuf,
    },

    /// Search for files in listfile
    Search {
        /// Search pattern (regex)
        pattern: String,

        /// Path to listfile
        #[arg(default_value = "community-listfile.csv")]
        path: PathBuf,

        /// Case-insensitive search
        #[arg(short, long)]
        ignore_case: bool,

        /// Limit results
        #[arg(short, long, default_value = "50")]
        limit: usize,
    },
}

#[derive(Subcommand)]
pub enum DownloadCommands {
    /// Download a specific build
    Build {
        /// Product name
        product: String,

        /// Build ID or version
        build: String,

        /// Output directory
        #[arg(long, default_value = ".")]
        output: PathBuf,

        /// Region
        #[arg(short, long, default_value = "us")]
        region: String,

        /// Dry run - show what would be downloaded without actually downloading
        #[arg(long)]
        dry_run: bool,

        /// Filter by tags (comma-separated)
        #[arg(long)]
        tags: Option<String>,
    },

    /// Download specific files
    Files {
        /// Product name
        product: String,

        /// File patterns to download
        patterns: Vec<String>,

        /// Output directory
        #[arg(long, default_value = ".")]
        output: PathBuf,

        /// Build ID or version
        #[arg(short, long)]
        build: Option<String>,

        /// Dry run - show what would be downloaded without actually downloading
        #[arg(long)]
        dry_run: bool,

        /// Filter by tags (comma-separated)
        #[arg(long)]
        tags: Option<String>,

        /// Limit number of files to download
        #[arg(long)]
        limit: Option<usize>,
    },

    /// Resume an interrupted download
    Resume {
        /// Session ID or path
        session: String,
    },

    /// Test resumable download with a known file (for testing)
    TestResume {
        /// File hash to download (32 hex chars)
        hash: String,

        /// CDN host
        #[arg(short = 'H', long, default_value = "blzddist1-a.akamaihd.net")]
        host: String,

        /// Output file path
        #[arg(long, default_value = "test_download.bin")]
        output: PathBuf,

        /// Enable resumable mode
        #[arg(short, long)]
        resumable: bool,
    },
}

#[derive(Subcommand)]
pub enum InstallCommands {
    /// Install a game or product
    Game {
        /// Product name (e.g., wow, wow_classic)
        product: String,

        /// Installation directory
        #[arg(long, default_value = ".")]
        path: PathBuf,

        /// Specific build to install (defaults to latest)
        #[arg(short, long)]
        build: Option<String>,

        /// Region
        #[arg(short, long, default_value = "us")]
        region: String,

        /// Installation type
        #[arg(short = 't', long, value_enum, default_value = "minimal")]
        install_type: InstallType,

        /// Resume existing installation (detects .build.info and missing files)
        #[arg(long)]
        resume: bool,

        /// Verify installation after completion
        #[arg(short = 'v', long)]
        verify: bool,

        /// Dry run - show what would be installed without downloading
        #[arg(long)]
        dry_run: bool,

        /// Maximum concurrent downloads
        #[arg(long, default_value = "5")]
        max_concurrent: usize,

        /// Filter by tags (comma-separated, e.g., "Windows,enUS")
        #[arg(long)]
        tags: Option<String>,
    },

    /// Repair an existing installation by verifying and re-downloading corrupted files
    Repair {
        /// Installation directory containing .build.info
        #[arg(long, default_value = ".")]
        path: PathBuf,

        /// Verify checksums of existing files
        #[arg(short = 'v', long)]
        verify_checksums: bool,

        /// Dry run - show what would be repaired without downloading
        #[arg(long)]
        dry_run: bool,

        /// Maximum concurrent downloads
        #[arg(long, default_value = "5")]
        max_concurrent: usize,
    },
}

#[derive(clap::ValueEnum, Clone, Copy, Debug, PartialEq)]
pub enum InstallType {
    /// Only required files for basic functionality
    Minimal,
    /// All available content
    Full,
    /// Custom selection based on tags
    Custom,
    /// Only create .build.info and Data/config structure (no downloads)
    MetadataOnly,
}

#[derive(Subcommand)]
pub enum InspectCommands {
    /// Parse and display BPSV data
    Bpsv {
        /// Input file or URL
        input: String,

        /// Show raw data
        #[arg(short, long)]
        raw: bool,
    },

    /// Inspect build configuration
    BuildConfig {
        /// Product name
        product: String,

        /// Build ID
        build: String,

        /// Region
        #[arg(short, long, default_value = "us")]
        region: String,
    },

    /// Inspect CDN configuration
    CdnConfig {
        /// Product name
        product: String,

        /// Region
        #[arg(short, long, default_value = "us")]
        region: String,
    },

    /// Inspect encoding file
    Encoding {
        /// Product name
        product: String,

        /// Region
        #[arg(short, long, default_value = "us")]
        region: String,

        /// Show statistics
        #[arg(short, long)]
        stats: bool,

        /// Search for specific key (hex string)
        #[arg(long)]
        search: Option<String>,

        /// Limit number of entries shown
        #[arg(long, default_value = "20")]
        limit: usize,
    },

    /// Inspect install manifest
    Install {
        /// Product name
        product: String,

        /// Region
        #[arg(short, long, default_value = "us")]
        region: String,

        /// Filter by tags (comma-separated)
        #[arg(long)]
        tags: Option<String>,

        /// Show all entries (not just summary)
        #[arg(long)]
        all: bool,
    },

    /// Inspect download manifest
    DownloadManifest {
        /// Product name
        product: String,

        /// Region
        #[arg(short, long, default_value = "us")]
        region: String,

        /// Show priority files
        #[arg(long, default_value = "10")]
        priority_limit: usize,

        /// Filter by tags (comma-separated)
        #[arg(long)]
        tags: Option<String>,
    },

    /// Inspect size file
    Size {
        /// Product name
        product: String,

        /// Region
        #[arg(short, long, default_value = "us")]
        region: String,

        /// Show largest files
        #[arg(long, default_value = "10")]
        largest: usize,

        /// Calculate size for tags (comma-separated)
        #[arg(long)]
        tags: Option<String>,
    },
}

#[derive(Subcommand)]
pub enum ConfigCommands {
    /// Show current configuration
    Show,

    /// Set a configuration value
    Set {
        /// Configuration key
        key: String,

        /// Configuration value
        value: String,
    },

    /// Get a configuration value
    Get {
        /// Configuration key
        key: String,
    },

    /// Reset configuration to defaults
    Reset {
        /// Confirm reset
        #[arg(short, long)]
        yes: bool,
    },
}

#[derive(Subcommand)]
pub enum CertsCommands {
    /// Download a certificate by its SKI/hash
    Download {
        /// Subject Key Identifier or certificate hash
        ski: String,

        /// Output file (defaults to stdout)
        #[arg(long)]
        output: Option<PathBuf>,

        /// Region to query
        #[arg(short, long, default_value = "us")]
        region: String,

        /// Certificate format (pem or der)
        #[arg(short = 'F', long = "cert-format", value_enum, default_value = "pem")]
        cert_format: CertFormat,

        /// Show certificate details
        #[arg(short, long)]
        details: bool,
    },
}

#[derive(clap::ValueEnum, Clone, Copy, Debug, PartialEq)]
pub enum CertFormat {
    /// PEM format (text)
    Pem,
    /// DER format (binary)
    Der,
}

/// Output format options for the CLI
#[derive(clap::ValueEnum, Clone, Copy, Debug, PartialEq)]
pub enum OutputFormat {
    /// Plain text output
    Text,
    /// JSON output
    Json,
    /// Pretty-printed JSON
    JsonPretty,
    /// Raw BPSV format
    Bpsv,
}

/// Context for command execution
#[derive(Clone, Debug)]
pub struct CommandContext {
    /// Output format
    pub format: OutputFormat,
    /// Whether to disable colors
    pub no_color: bool,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_output_format_debug() {
        assert_eq!(format!("{:?}", OutputFormat::Text), "Text");
        assert_eq!(format!("{:?}", OutputFormat::Json), "Json");
        assert_eq!(format!("{:?}", OutputFormat::JsonPretty), "JsonPretty");
        assert_eq!(format!("{:?}", OutputFormat::Bpsv), "Bpsv");
    }
}