kagi-app 0.1.6

CLI application for Kagi secrets manager
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
use clap::{ColorChoice, Parser, Subcommand};

#[derive(Parser)]
#[command(
    name = "kagi",
    about = "Manage encrypted environment variables",
    version,
    color = ColorChoice::Auto
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,

    /// Allow insecure HTTP remotes for non-localhost addresses
    #[arg(long, global = true)]
    pub allow_insecure_http: bool,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Initialize a new kagi repository in the current directory
    Init {
        /// Default environments for services (comma-separated, e.g., development,test)
        #[arg(short, long, value_delimiter = ',', num_args = 0..=1, default_missing_value = "")]
        envs: Option<Vec<String>>,

        /// Enable nested service inference from subdirectories
        #[arg(long)]
        nested: bool,

        /// Overwrite existing .kagi/ directory
        #[arg(long)]
        force: bool,

        /// Skip .env migration wizard
        #[arg(long)]
        no_migrate: bool,
    },

    /// Store an encrypted secret for an environment
    Set {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// Description of the secret (shown in exports and lists)
        #[arg(short, long)]
        desc: Option<String>,

        /// Service or environment name
        first: Option<String>,

        /// Environment name or secret key
        second: Option<String>,

        /// Secret key or value
        third: Option<String>,

        /// Value to store (will be encrypted), when service and env are both provided positionally
        fourth: Option<String>,
    },

    /// Get service/env/key information or decrypt one secret value
    Get {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// Show decrypted values when listing. Requires an interactive terminal.
        #[arg(long = "show")]
        show_values: bool,

        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,

        /// Service or environment name
        first: Option<String>,

        /// Environment name or secret key
        second: Option<String>,

        /// Secret key, when service and env are both provided positionally
        third: Option<String>,
    },

    /// Delete a single secret key from a scope
    Unset {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// Service or environment name
        first: Option<String>,

        /// Environment name or secret key
        second: Option<String>,

        /// Secret key, when service and env are both provided positionally
        third: Option<String>,
    },

    /// Search secret keys across services
    Search {
        /// Search query (case-insensitive)
        query: String,

        /// Also search decrypted values (requires interactive terminal)
        #[arg(long)]
        values: bool,

        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,
    },

    /// Run a command with injected environment variables
    Run {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// <service> <command>... or <command>... when the scope is inferred from nested directory.
        #[arg(required = false, trailing_var_arg = true)]
        args: Vec<String>,
    },

    /// Diagnose local project health and check for common issues
    Doctor {
        /// Attempt to fix recoverable issues (e.g., pending rotation journal)
        #[arg(long)]
        fix: bool,

        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,
    },

    /// Show current repository, inferred service, environments, and remote hints
    Status,

    /// Export secrets as KEY=value lines (suitable for shell sourcing)
    Export {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// Directory to write one .env file per environment when exporting a service
        #[arg(short, long)]
        out: Option<String>,

        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,

        /// Service or environment name
        first: Option<String>,

        /// Environment name, when service is provided positionally
        second: Option<String>,
    },

    /// Import secrets from a .env file
    Import {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// Service or environment name
        first: Option<String>,

        /// Environment name, when service is provided positionally
        second: Option<String>,

        /// Path to the env file to import
        #[arg(short, long, default_value = ".env")]
        file: String,

        /// Overwrite existing keys without prompting
        #[arg(long)]
        force: bool,

        /// Preview import changes without writing secrets
        #[arg(long)]
        dry_run: bool,

        /// Normalize imported keys to UPPER_SNAKE_CASE
        #[arg(long)]
        upper_snake: bool,
    },

    /// Synchronize keys from .env.example (and optional sources) across environments
    Sync {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory when present.
        #[arg(short, long)]
        service: Option<String>,

        /// Path to the .env.example template file
        #[arg(short, long, default_value = ".env.example")]
        example: String,

        /// Additional source files to merge (comma-separated, later overrides earlier)
        #[arg(long, value_delimiter = ',')]
        sources: Vec<String>,

        /// Environments to sync (comma-separated)
        #[arg(
            long,
            value_delimiter = ',',
            default_value = "development,test,staging,production"
        )]
        envs: Vec<String>,

        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,
    },

    /// Manage encrypted files scoped like env secrets
    File {
        #[command(subcommand)]
        command: FileCommands,
    },

    /// Manage default environments
    Env {
        #[command(subcommand)]
        command: EnvCommands,
    },

    /// Manage project members
    Member {
        #[command(subcommand)]
        command: MemberCommands,
    },

    #[cfg(feature = "server")]
    /// Start the Kagi remote sync server
    Serve {
        /// Database file path
        #[arg(long, default_value = "")]
        db: String,

        /// Server key file path
        #[arg(long, default_value = "")]
        key_file: String,

        /// Bind address
        #[arg(long, default_value = "127.0.0.1:13816")]
        bind: String,

        /// Max body size (e.g. 10mb)
        #[arg(long, default_value = "10mb")]
        max_body: String,

        /// Allow serving HTTP on non-localhost addresses (unsafe for production)
        #[arg(long)]
        allow_insecure_http: bool,
    },

    #[cfg(feature = "server")]
    /// Manage remote server credentials and connections
    Remote {
        #[command(subcommand)]
        command: RemoteCommands,
    },

    /// Generate shell completions for kagi
    Completions {
        /// Shell to generate completions for (bash, zsh, fish, elvish, powershell)
        shell: String,
    },

    /// Create a backup of the project and local credentials
    Backup {
        /// Output directory for the backup
        #[arg(short, long)]
        out: String,
    },

    /// Restore a project from a backup
    Restore {
        /// Backup directory to restore from
        #[arg(short, long)]
        from: String,

        /// Overwrite existing files without prompting
        #[arg(long)]
        force: bool,
    },
}

#[derive(Subcommand)]
pub enum FileCommands {
    /// Add a small plaintext file to encrypted storage
    Add {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// Logical file name to use inside kagi
        #[arg(long)]
        name: Option<String>,

        /// Replace an existing file with the same name in the selected scope
        #[arg(long)]
        force: bool,

        /// Allow files up to 5 MiB instead of the default 1 MiB
        #[arg(long)]
        allow_large: bool,

        /// Manage a file outside the repository, limited to the current user's home directory
        #[arg(long)]
        external: bool,

        /// [service] [env] <path>, or [env] <path> when service is inferred
        #[arg(required = true)]
        args: Vec<String>,
    },

    /// List encrypted files in a scope
    List {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// List files from every scope
        #[arg(long)]
        all: bool,

        /// [service] [env], or [env] when service is inferred
        args: Vec<String>,
    },

    /// Print one decrypted file to stdout. Requires an interactive terminal.
    Show {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// [service] [env] <name>, or [env] <name> when service is inferred
        #[arg(required = true)]
        args: Vec<String>,
    },

    /// Restore one decrypted file to its original path or --out path
    Restore {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// Output path. Defaults to the path captured when the file was added.
        #[arg(short, long)]
        out: Option<String>,

        /// Overwrite an existing plaintext output file
        #[arg(long)]
        force: bool,

        /// Restore every encrypted file in the selected scope
        #[arg(long)]
        all: bool,

        /// Preview restore targets without writing files
        #[arg(long)]
        dry_run: bool,

        /// [service] [env] <name>, or [env] <name> when service is inferred
        args: Vec<String>,
    },

    /// Remove one encrypted file from the selected scope
    Remove {
        /// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
        #[arg(short, long)]
        service: Option<String>,

        /// Remove without an interactive confirmation prompt
        #[arg(long)]
        force: bool,

        /// [service] [env] <name>, or [env] <name> when service is inferred
        #[arg(required = true)]
        args: Vec<String>,
    },
}

#[derive(Subcommand)]
pub enum EnvCommands {
    /// List configured default environments
    List {
        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,
    },

    /// Add an environment to every service
    Add {
        /// Environment name to add
        env: String,
    },

    /// Rename an environment across every service
    Rename {
        /// Existing environment name
        old: String,

        /// New environment name
        new: String,
    },

    /// Delete an environment from every service
    Remove {
        /// Environment name to delete
        env: String,

        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,
    },
}

#[derive(Subcommand)]
pub enum MemberCommands {
    /// List active members and pending member requests
    List {
        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,
    },

    /// Request to join this project from a new device
    Request {
        /// Display name for the member requesting access
        #[arg(short, long)]
        name: Option<String>,
    },

    /// Approve a pending member request
    Approve {
        /// Member id from `kagi member list`.
        member_id: Option<String>,
    },

    /// Remove a member's access wrapper
    Remove {
        /// Member id from `kagi member list`.
        member_id: Option<String>,
    },
}

#[cfg(feature = "server")]
#[derive(Subcommand)]
pub enum RemoteCommands {
    /// Save an admin token for a remote server (stored in OS keychain)
    Login {
        /// Remote server URL
        #[arg(long)]
        remote: String,

        /// Admin token from server first startup
        #[arg(long)]
        token: String,
    },

    /// Register this local project with a remote server
    Register {
        /// Remote server URL
        #[arg(long)]
        remote: String,
    },

    /// Upload local encrypted project state to the remote server
    Push,

    /// Download encrypted project state from the remote server
    Pull {
        /// Optional project token for pulling without local project
        token: Option<String>,
    },

    /// Compare local and remote revisions
    Status,

    /// List remote projects and pending registration requests (admin only)
    Projects {
        /// Remote server URL (optional if saved via `kagi remote login`)
        #[arg(long)]
        remote: Option<String>,

        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,
    },

    /// Approve a pending project registration request (admin only)
    Approve {
        /// Remote server URL (optional if saved via `kagi remote login`)
        #[arg(long)]
        remote: Option<String>,

        /// Project ID to approve
        project_id: String,
    },

    /// Delete a project from the remote server (admin or project admin)
    Remove {
        /// Remote server URL (optional if saved via `kagi remote login`)
        #[arg(long)]
        remote: Option<String>,

        /// Project ID to delete
        project_id: String,
    },

    /// List project tokens on the remote server
    Tokens {
        /// Remote server URL (optional if saved via `kagi remote login`)
        #[arg(long)]
        remote: Option<String>,

        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,
    },

    /// Revoke a project token
    RevokeToken {
        /// Remote server URL (optional if saved via `kagi remote login`)
        #[arg(long)]
        remote: Option<String>,

        /// Token ID to revoke
        token_id: String,
    },

    /// Query server audit logs (admin only)
    Audit {
        /// Remote server URL (optional if saved via `kagi remote login`)
        #[arg(long)]
        remote: Option<String>,

        /// Filter by project ID
        #[arg(long)]
        project_id: Option<String>,

        /// Maximum number of events to return (1-500)
        #[arg(long, default_value = "50")]
        limit: i64,

        /// Use plain text output
        #[arg(long)]
        #[cfg_attr(not(feature = "tui"), arg(hide = true))]
        plain: bool,
    },
}

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

    #[test]
    #[cfg(feature = "server")]
    fn test_server_commands_available_with_server_feature() {
        let cmd = Cli::command();
        let names: Vec<_> = cmd.get_subcommands().map(|c| c.get_name()).collect();
        assert!(names.contains(&"serve"), "serve should be present");
        assert!(names.contains(&"remote"), "remote should be present");
        assert!(names.contains(&"status"), "status should be present");

        for removed in ["push", "pull", "project", "token"] {
            assert!(
                !names.contains(&removed),
                "{removed} should not be present as a top-level command"
            );
        }

        let remote = cmd
            .get_subcommands()
            .find(|c| c.get_name() == "remote")
            .expect("remote command should be present");
        let remote_names: Vec<_> = remote.get_subcommands().map(|c| c.get_name()).collect();
        for expected in [
            "login",
            "register",
            "push",
            "pull",
            "status",
            "projects",
            "approve",
            "remove",
            "tokens",
            "revoke-token",
            "audit",
        ] {
            assert!(
                remote_names.contains(&expected),
                "remote {expected} should be present"
            );
        }
    }

    #[test]
    #[cfg(not(feature = "server"))]
    fn test_server_commands_not_available_without_server_feature() {
        let cmd = Cli::command();
        let names: Vec<_> = cmd.get_subcommands().map(|c| c.get_name()).collect();
        assert!(
            !names.contains(&"serve"),
            "serve should NOT be present when server feature is disabled"
        );
        assert!(
            !names.contains(&"push"),
            "push should NOT be present when server feature is disabled"
        );
        assert!(
            !names.contains(&"pull"),
            "pull should NOT be present when server feature is disabled"
        );
        assert!(
            !names.contains(&"status"),
            "status should NOT be present when server feature is disabled"
        );
        assert!(
            !names.contains(&"project"),
            "project should NOT be present when server feature is disabled"
        );
        assert!(
            !names.contains(&"remote"),
            "remote should NOT be present when server feature is disabled"
        );
    }

    #[test]
    fn test_git_backed_command_names_stay_available() {
        let cmd = Cli::command();

        let env = cmd
            .get_subcommands()
            .find(|c| c.get_name() == "env")
            .expect("env command should be present");
        let env_names: Vec<_> = env.get_subcommands().map(|c| c.get_name()).collect();
        assert!(env_names.contains(&"remove"));
        assert!(!env_names.contains(&"del"));

        let member = cmd
            .get_subcommands()
            .find(|c| c.get_name() == "member")
            .expect("member command should be present");
        let member_names: Vec<_> = member.get_subcommands().map(|c| c.get_name()).collect();
        assert!(member_names.contains(&"request"));
        assert!(member_names.contains(&"approve"));
        assert!(member_names.contains(&"remove"));
        assert!(!member_names.contains(&"join"));
        assert!(!member_names.contains(&"del"));
    }

    #[test]
    fn test_completions_command_exists() {
        let cmd = Cli::command();
        let names: Vec<_> = cmd.get_subcommands().map(|c| c.get_name()).collect();
        assert!(
            names.contains(&"completions"),
            "completions should be present"
        );
    }

    #[test]
    fn test_completions_accepts_supported_shells() {
        for shell in ["bash", "zsh", "fish", "elvish", "powershell"] {
            let args = vec!["kagi", "completions", shell];
            let result = Cli::try_parse_from(&args);
            assert!(result.is_ok(), "completions {shell} should parse");
        }
    }

    #[test]
    fn test_completions_rejects_unknown_shell() {
        let args = vec!["kagi", "completions", "unknown_shell"];
        let result = Cli::try_parse_from(&args);
        assert!(
            result.is_ok(),
            "unknown shell should parse at CLI level (error handled at runtime)"
        );
    }
}