-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_schema.rs
More file actions
219 lines (175 loc) · 5.74 KB
/
cli_schema.rs
File metadata and controls
219 lines (175 loc) · 5.74 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
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;
use crate::services::style;
#[derive(Parser, Debug)]
#[command(name = "sce", version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
}
impl Cli {
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)
}
}
pub fn render_help_for_path(path: &[&str]) -> Option<String> {
let mut command = Cli::command();
for segment in path {
// Clone required: find_subcommand_mut returns a mutable reference that cannot
// be kept alive across loop iterations, so we must clone to get an owned value
command = command.find_subcommand_mut(segment)?.clone();
}
let mut buffer = Vec::new();
command
.write_long_help(&mut buffer)
.expect("help rendering should write to memory");
let help = String::from_utf8(buffer).expect("help output should be valid UTF-8");
Some(style::clap_help(&help))
}
pub fn auth_help_text() -> String {
use crate::services::style::{command_name, heading};
let base = render_help_for_path(&["auth"]).expect("auth help should be renderable");
format!(
"{}\n{}:\n {}\n {}\n {}\n {}\n",
base,
heading("Examples"),
command_name("sce auth login"),
command_name("sce auth renew"),
command_name("sce auth status"),
command_name("sce auth logout")
)
}
#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub enum Commands {
#[command(about = "Authenticate with `WorkOS` device authorization flow")]
Auth {
#[command(subcommand)]
subcommand: AuthSubcommand,
},
#[command(about = "Inspect or validate runtime config and observability resolution")]
Config {
#[command(subcommand)]
subcommand: ConfigSubcommand,
},
#[command(about = "Prepare local repository/workspace prerequisites")]
Setup {
#[arg(long, conflicts_with_all = ["claude", "both"])]
opencode: bool,
#[arg(long, conflicts_with_all = ["opencode", "both"])]
claude: bool,
#[arg(long, conflicts_with_all = ["opencode", "claude"])]
both: bool,
#[arg(long)]
non_interactive: bool,
#[arg(long)]
hooks: bool,
#[arg(long, requires = "hooks")]
repo: Option<PathBuf>,
},
#[command(about = "Inspect and repair SCE operator environment health")]
Doctor {
#[arg(long)]
fix: bool,
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
#[command(about = "Run attribution-only git hooks (disabled by default)")]
Hooks {
#[command(subcommand)]
subcommand: HooksSubcommand,
},
#[command(about = "Print deterministic runtime version metadata")]
Version {
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
#[command(about = "Generate deterministic shell completion scripts")]
Completion {
#[arg(long, value_enum)]
shell: CompletionShell,
},
}
#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub enum AuthSubcommand {
#[command(about = "Start login flow and store credentials")]
Login {
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
#[command(about = "Renew stored credentials when they are expired or near expiry")]
Renew {
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
#[arg(long)]
force: bool,
},
#[command(about = "Remove stored credentials from the local machine")]
Logout {
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
#[command(about = "Show current authentication status from stored credentials")]
Status {
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
},
}
#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub enum ConfigSubcommand {
#[command(about = "Show resolved runtime config, including observability sources")]
Show {
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
#[arg(long)]
config: Option<PathBuf>,
#[arg(long, value_enum)]
log_level: Option<LogLevel>,
#[arg(long)]
timeout_ms: Option<u64>,
},
#[command(about = "Validate config files and report pass/fail with errors or warnings")]
Validate {
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
#[arg(long)]
config: Option<PathBuf>,
#[arg(long, value_enum)]
log_level: Option<LogLevel>,
#[arg(long)]
timeout_ms: Option<u64>,
},
}
#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub enum HooksSubcommand {
#[command(about = "Run pre-commit hook")]
PreCommit,
#[command(about = "Run commit-msg hook")]
CommitMsg { message_file: PathBuf },
#[command(about = "Run post-commit hook")]
PostCommit,
#[command(about = "Run post-rewrite hook (reads pairs from STDIN)")]
PostRewrite { rewrite_method: String },
}
#[derive(ValueEnum, Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum OutputFormat {
#[default]
Text,
Json,
}
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
pub enum CompletionShell {
Bash,
Zsh,
Fish,
}
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
pub enum LogLevel {
Error,
Warn,
Info,
Debug,
}