-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli_schema.rs
More file actions
723 lines (652 loc) · 22.3 KB
/
cli_schema.rs
File metadata and controls
723 lines (652 loc) · 22.3 KB
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
//! Clap-based CLI schema for the Shared Context Engineering CLI.
//!
//! This module defines the complete command-line interface using clap derive macros.
use clap::{Parser, Subcommand, ValueEnum};
use std::path::PathBuf;
/// Shared Context Engineering CLI
#[derive(Parser, Debug)]
#[command(name = "sce", version, about, long_about = None)]
pub struct Cli {
/// The subcommand to run
#[command(subcommand)]
pub command: Option<Commands>,
}
impl Cli {
/// Parse arguments from an iterator of strings
pub fn parse_from<I, T>(args: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
{
<Self as Parser>::parse_from(args)
}
/// Try to parse arguments, returning an error on failure
pub fn try_parse_from<I, T>(args: I) -> Result<Self, clap::Error>
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
{
<Self as Parser>::try_parse_from(args)
}
}
#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub enum Commands {
/// Authenticate with WorkOS device authorization flow
Auth {
#[command(subcommand)]
subcommand: AuthSubcommand,
},
/// Inspect and validate resolved CLI configuration
Config {
#[command(subcommand)]
subcommand: ConfigSubcommand,
},
/// Prepare local repository/workspace prerequisites
#[command(about = "Prepare local repository/workspace prerequisites")]
Setup {
/// Install OpenCode configuration
#[arg(long, conflicts_with_all = ["claude", "both"])]
opencode: bool,
/// Install Claude configuration
#[arg(long, conflicts_with_all = ["opencode", "both"])]
claude: bool,
/// Install both OpenCode and Claude configuration
#[arg(long, conflicts_with_all = ["opencode", "claude"])]
both: bool,
/// Run without interactive prompts (requires a target flag when not using --hooks)
#[arg(long)]
non_interactive: bool,
/// Install required git hooks
#[arg(long)]
hooks: bool,
/// Repository path for hook installation (requires --hooks)
#[arg(long, requires = "hooks")]
repo: Option<PathBuf>,
},
/// Validate local git-hook installation readiness
#[command(about = "Validate local git-hook installation readiness")]
Doctor {
/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
/// Host MCP file-cache tooling commands (placeholder)
#[command(about = "Host MCP file-cache tooling commands (placeholder)")]
Mcp {
/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
/// Run git-hook runtime entrypoints for local Agent Trace flows
#[command(about = "Run git-hook runtime entrypoints for local Agent Trace flows")]
Hooks {
#[command(subcommand)]
subcommand: HooksSubcommand,
},
/// Coordinate future cloud sync workflows (placeholder)
#[command(about = "Coordinate future cloud sync workflows (placeholder)")]
Sync {
/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
/// Print deterministic runtime version metadata
#[command(about = "Print deterministic runtime version metadata")]
Version {
/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
/// Generate deterministic shell completion scripts
#[command(about = "Generate deterministic shell completion scripts")]
Completion {
/// Shell type for completion script
#[arg(long, value_enum)]
shell: CompletionShell,
},
}
/// Config subcommands
#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub enum AuthSubcommand {
/// Start login flow and store credentials
Login {
/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
/// Clear stored credentials
Logout {
/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
/// Show current authentication status
Status {
/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
}
/// Config subcommands
#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub enum ConfigSubcommand {
/// Show resolved configuration
Show {
/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
/// Path to configuration file
#[arg(long)]
config: Option<PathBuf>,
/// Override log level
#[arg(long, value_enum)]
log_level: Option<LogLevel>,
/// Override timeout in milliseconds
#[arg(long)]
timeout_ms: Option<u64>,
},
/// Validate configuration file
Validate {
/// Output format
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
/// Path to configuration file
#[arg(long)]
config: Option<PathBuf>,
/// Override log level
#[arg(long, value_enum)]
log_level: Option<LogLevel>,
/// Override timeout in milliseconds
#[arg(long)]
timeout_ms: Option<u64>,
},
}
/// Hooks subcommands
#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub enum HooksSubcommand {
/// Run pre-commit hook
#[command(about = "Run pre-commit hook")]
PreCommit,
/// Run commit-msg hook
#[command(about = "Run commit-msg hook")]
CommitMsg {
/// Path to the commit message file
message_file: PathBuf,
},
/// Run post-commit hook
#[command(about = "Run post-commit hook")]
PostCommit,
/// Run post-rewrite hook
#[command(about = "Run post-rewrite hook (reads pairs from STDIN)")]
PostRewrite {
/// Rewrite method (amend, rebase, or other)
rewrite_method: String,
},
}
/// Output format for commands that support multiple formats
#[derive(ValueEnum, Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum OutputFormat {
/// Plain text output
#[default]
Text,
/// JSON output
Json,
}
/// Shell types for completion generation
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
pub enum CompletionShell {
/// Bash shell completion
Bash,
/// Zsh shell completion
Zsh,
/// Fish shell completion
Fish,
}
/// Log level configuration
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
pub enum LogLevel {
/// Error level only
Error,
/// Warning and above
Warn,
/// Info and above
Info,
/// Debug and above
Debug,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_auth_login() {
let cli = Cli::try_parse_from(["sce", "auth", "login"]).expect("auth login should parse");
match cli.command {
Some(Commands::Auth { subcommand }) => match subcommand {
AuthSubcommand::Login { format } => {
assert_eq!(format, OutputFormat::Text);
}
_ => panic!("Expected Login subcommand"),
},
_ => panic!("Expected Auth command"),
}
}
#[test]
fn parse_auth_login_json() {
let cli = Cli::try_parse_from(["sce", "auth", "login", "--format", "json"])
.expect("auth login --format json should parse");
match cli.command {
Some(Commands::Auth { subcommand }) => match subcommand {
AuthSubcommand::Login { format } => {
assert_eq!(format, OutputFormat::Json);
}
_ => panic!("Expected Login subcommand"),
},
_ => panic!("Expected Auth command"),
}
}
#[test]
fn parse_auth_logout() {
let cli = Cli::try_parse_from(["sce", "auth", "logout"]).expect("auth logout should parse");
match cli.command {
Some(Commands::Auth { subcommand }) => match subcommand {
AuthSubcommand::Logout { format } => {
assert_eq!(format, OutputFormat::Text);
}
_ => panic!("Expected Logout subcommand"),
},
_ => panic!("Expected Auth command"),
}
}
#[test]
fn parse_auth_logout_json() {
let cli = Cli::try_parse_from(["sce", "auth", "logout", "--format", "json"])
.expect("auth logout --format json should parse");
match cli.command {
Some(Commands::Auth { subcommand }) => match subcommand {
AuthSubcommand::Logout { format } => {
assert_eq!(format, OutputFormat::Json);
}
_ => panic!("Expected Logout subcommand"),
},
_ => panic!("Expected Auth command"),
}
}
#[test]
fn parse_auth_status() {
let cli = Cli::try_parse_from(["sce", "auth", "status"]).expect("auth status should parse");
match cli.command {
Some(Commands::Auth { subcommand }) => match subcommand {
AuthSubcommand::Status { format } => {
assert_eq!(format, OutputFormat::Text);
}
_ => panic!("Expected Status subcommand"),
},
_ => panic!("Expected Auth command"),
}
}
#[test]
fn parse_auth_status_json() {
let cli = Cli::try_parse_from(["sce", "auth", "status", "--format", "json"])
.expect("auth status --format json should parse");
match cli.command {
Some(Commands::Auth { subcommand }) => match subcommand {
AuthSubcommand::Status { format } => {
assert_eq!(format, OutputFormat::Json);
}
_ => panic!("Expected Status subcommand"),
},
_ => panic!("Expected Auth command"),
}
}
#[test]
fn parse_version_command() {
let cli = Cli::try_parse_from(["sce", "version"]).expect("version should parse");
match cli.command {
Some(Commands::Version { format }) => {
assert_eq!(format, OutputFormat::Text);
}
_ => panic!("Expected Version command"),
}
}
#[test]
fn parse_version_json() {
let cli = Cli::try_parse_from(["sce", "version", "--format", "json"])
.expect("version --format json should parse");
match cli.command {
Some(Commands::Version { format }) => {
assert_eq!(format, OutputFormat::Json);
}
_ => panic!("Expected Version command"),
}
}
#[test]
fn parse_config_show() {
let cli = Cli::try_parse_from(["sce", "config", "show"]).expect("config show should parse");
match cli.command {
Some(Commands::Config { subcommand }) => match subcommand {
ConfigSubcommand::Show { format, .. } => {
assert_eq!(format, OutputFormat::Text);
}
_ => panic!("Expected Show subcommand"),
},
_ => panic!("Expected Config command"),
}
}
#[test]
fn parse_config_validate_json() {
let cli = Cli::try_parse_from(["sce", "config", "validate", "--format", "json"])
.expect("config validate --format json should parse");
match cli.command {
Some(Commands::Config { subcommand }) => match subcommand {
ConfigSubcommand::Validate { format, .. } => {
assert_eq!(format, OutputFormat::Json);
}
_ => panic!("Expected Validate subcommand"),
},
_ => panic!("Expected Config command"),
}
}
#[test]
fn parse_config_with_options() {
let cli = Cli::try_parse_from([
"sce",
"config",
"show",
"--config",
"/path/to/config.json",
"--log-level",
"debug",
"--timeout-ms",
"60000",
])
.expect("config show with options should parse");
match cli.command {
Some(Commands::Config { subcommand }) => match subcommand {
ConfigSubcommand::Show {
config,
log_level,
timeout_ms,
..
} => {
assert_eq!(config, Some(PathBuf::from("/path/to/config.json")));
assert_eq!(log_level, Some(LogLevel::Debug));
assert_eq!(timeout_ms, Some(60000));
}
_ => panic!("Expected Show subcommand"),
},
_ => panic!("Expected Config command"),
}
}
#[test]
fn parse_setup_opencode() {
let cli = Cli::try_parse_from(["sce", "setup", "--opencode"])
.expect("setup --opencode should parse");
match cli.command {
Some(Commands::Setup {
opencode,
claude,
both,
..
}) => {
assert!(opencode);
assert!(!claude);
assert!(!both);
}
_ => panic!("Expected Setup command"),
}
}
#[test]
fn parse_setup_claude() {
let cli =
Cli::try_parse_from(["sce", "setup", "--claude"]).expect("setup --claude should parse");
match cli.command {
Some(Commands::Setup {
opencode,
claude,
both,
..
}) => {
assert!(!opencode);
assert!(claude);
assert!(!both);
}
_ => panic!("Expected Setup command"),
}
}
#[test]
fn parse_setup_both() {
let cli =
Cli::try_parse_from(["sce", "setup", "--both"]).expect("setup --both should parse");
match cli.command {
Some(Commands::Setup {
opencode,
claude,
both,
..
}) => {
assert!(!opencode);
assert!(!claude);
assert!(both);
}
_ => panic!("Expected Setup command"),
}
}
#[test]
fn parse_setup_hooks() {
let cli =
Cli::try_parse_from(["sce", "setup", "--hooks"]).expect("setup --hooks should parse");
match cli.command {
Some(Commands::Setup { hooks, repo, .. }) => {
assert!(hooks);
assert!(repo.is_none());
}
_ => panic!("Expected Setup command"),
}
}
#[test]
fn parse_setup_hooks_with_repo() {
let cli = Cli::try_parse_from(["sce", "setup", "--hooks", "--repo", "../demo-repo"])
.expect("setup --hooks --repo should parse");
match cli.command {
Some(Commands::Setup { hooks, repo, .. }) => {
assert!(hooks);
assert_eq!(repo, Some(PathBuf::from("../demo-repo")));
}
_ => panic!("Expected Setup command"),
}
}
#[test]
fn parse_setup_opencode_with_hooks() {
let cli = Cli::try_parse_from(["sce", "setup", "--opencode", "--hooks"])
.expect("setup --opencode --hooks should parse");
match cli.command {
Some(Commands::Setup {
opencode, hooks, ..
}) => {
assert!(opencode);
assert!(hooks);
}
_ => panic!("Expected Setup command"),
}
}
#[test]
fn parse_setup_non_interactive_requires_target() {
// Note: This validation is now handled at runtime in resolve_setup_request,
// not at the clap parsing level. The parsing succeeds but runtime would fail.
let cli = Cli::try_parse_from(["sce", "setup", "--non-interactive"])
.expect("parsing should succeed (runtime validation handles this)");
match cli.command {
Some(Commands::Setup {
non_interactive, ..
}) => {
assert!(non_interactive);
}
_ => panic!("Expected Setup command"),
}
}
#[test]
fn parse_setup_non_interactive_with_target() {
let cli = Cli::try_parse_from(["sce", "setup", "--opencode", "--non-interactive"])
.expect("setup --opencode --non-interactive should parse");
match cli.command {
Some(Commands::Setup {
opencode,
non_interactive,
..
}) => {
assert!(opencode);
assert!(non_interactive);
}
_ => panic!("Expected Setup command"),
}
}
#[test]
fn parse_setup_mutually_exclusive_targets() {
// opencode and claude are mutually exclusive
let result = Cli::try_parse_from(["sce", "setup", "--opencode", "--claude"]);
assert!(
result.is_err(),
"mutually exclusive targets should fail to parse"
);
}
#[test]
fn parse_setup_repo_requires_hooks() {
// --repo requires --hooks
let result = Cli::try_parse_from(["sce", "setup", "--repo", "../demo-repo"]);
assert!(result.is_err(), "--repo without --hooks should fail");
}
#[test]
fn parse_doctor() {
let cli = Cli::try_parse_from(["sce", "doctor"]).expect("doctor should parse");
match cli.command {
Some(Commands::Doctor { format }) => {
assert_eq!(format, OutputFormat::Text);
}
_ => panic!("Expected Doctor command"),
}
}
#[test]
fn parse_doctor_json() {
let cli = Cli::try_parse_from(["sce", "doctor", "--format", "json"])
.expect("doctor json should parse");
match cli.command {
Some(Commands::Doctor { format }) => {
assert_eq!(format, OutputFormat::Json);
}
_ => panic!("Expected Doctor command"),
}
}
#[test]
fn parse_hooks_pre_commit() {
let cli = Cli::try_parse_from(["sce", "hooks", "pre-commit"])
.expect("hooks pre-commit should parse");
match cli.command {
Some(Commands::Hooks { subcommand }) => {
assert_eq!(subcommand, HooksSubcommand::PreCommit);
}
_ => panic!("Expected Hooks command"),
}
}
#[test]
fn parse_hooks_commit_msg() {
let cli = Cli::try_parse_from(["sce", "hooks", "commit-msg", ".git/COMMIT_EDITMSG"])
.expect("hooks commit-msg should parse");
match cli.command {
Some(Commands::Hooks { subcommand }) => match subcommand {
HooksSubcommand::CommitMsg { message_file } => {
assert_eq!(message_file, PathBuf::from(".git/COMMIT_EDITMSG"));
}
_ => panic!("Expected CommitMsg subcommand"),
},
_ => panic!("Expected Hooks command"),
}
}
#[test]
fn parse_hooks_post_commit() {
let cli = Cli::try_parse_from(["sce", "hooks", "post-commit"])
.expect("hooks post-commit should parse");
match cli.command {
Some(Commands::Hooks { subcommand }) => {
assert_eq!(subcommand, HooksSubcommand::PostCommit);
}
_ => panic!("Expected Hooks command"),
}
}
#[test]
fn parse_hooks_post_rewrite() {
let cli = Cli::try_parse_from(["sce", "hooks", "post-rewrite", "amend"])
.expect("hooks post-rewrite should parse");
match cli.command {
Some(Commands::Hooks { subcommand }) => match subcommand {
HooksSubcommand::PostRewrite { rewrite_method } => {
assert_eq!(rewrite_method, "amend");
}
_ => panic!("Expected PostRewrite subcommand"),
},
_ => panic!("Expected Hooks command"),
}
}
#[test]
fn parse_sync() {
let cli = Cli::try_parse_from(["sce", "sync"]).expect("sync should parse");
match cli.command {
Some(Commands::Sync { format }) => {
assert_eq!(format, OutputFormat::Text);
}
_ => panic!("Expected Sync command"),
}
}
#[test]
fn parse_mcp() {
let cli = Cli::try_parse_from(["sce", "mcp"]).expect("mcp should parse");
match cli.command {
Some(Commands::Mcp { format }) => {
assert_eq!(format, OutputFormat::Text);
}
_ => panic!("Expected Mcp command"),
}
}
#[test]
fn parse_completion_bash() {
let cli = Cli::try_parse_from(["sce", "completion", "--shell", "bash"])
.expect("completion bash should parse");
match cli.command {
Some(Commands::Completion { shell }) => {
assert_eq!(shell, CompletionShell::Bash);
}
_ => panic!("Expected Completion command"),
}
}
#[test]
fn parse_completion_zsh() {
let cli = Cli::try_parse_from(["sce", "completion", "--shell", "zsh"])
.expect("completion zsh should parse");
match cli.command {
Some(Commands::Completion { shell }) => {
assert_eq!(shell, CompletionShell::Zsh);
}
_ => panic!("Expected Completion command"),
}
}
#[test]
fn parse_completion_fish() {
let cli = Cli::try_parse_from(["sce", "completion", "--shell", "fish"])
.expect("completion fish should parse");
match cli.command {
Some(Commands::Completion { shell }) => {
assert_eq!(shell, CompletionShell::Fish);
}
_ => panic!("Expected Completion command"),
}
}
#[test]
fn completion_requires_shell() {
let result = Cli::try_parse_from(["sce", "completion"]);
assert!(result.is_err(), "completion without --shell should fail");
}
#[test]
fn no_command_defaults_to_none() {
let cli = Cli::try_parse_from(["sce"]).expect("no command should parse");
assert_eq!(cli.command, None);
}
}