Repository navigation
Expand file tree
/
Copy pathcli.rs
More file actions
448 lines (421 loc) · 15.6 KB
/
Copy pathcli.rs
File metadata and controls
448 lines (421 loc) · 15.6 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
//! Command line interface for devcap.
use std::io::Write;
use crate::argparse::{Exit, Kind, Namespace, OptAction, Parser, Val};
use crate::formatters::format_by_name;
use crate::process::COMMAND_TIMEOUT_SECONDS;
use crate::profile::{
MAX_PROFILE_DEPTH, ProfileError, list_builtin_profiles, load_builtin_profile,
load_custom_profile,
};
use crate::pycompat::{PyInt, backslash_escapes, format_g, fsencode, parse_float, parse_int};
use crate::scanner::{
DEFAULT_SCAN_WORKERS, MAX_COMMAND_TIMEOUT_SECONDS, MAX_SCAN_WORKERS, redact_scan, scan_tools,
};
const MAIN_HELP: &str = include_str!("help/main.txt");
const MAIN_USAGE: &str = include_str!("help/main.usage");
const SCAN_HELP: &str = include_str!("help/scan.txt");
const SCAN_USAGE: &str = include_str!("help/scan.usage");
const CHECK_HELP: &str = include_str!("help/check.txt");
const CHECK_USAGE: &str = include_str!("help/check.usage");
const LIST_HELP: &str = include_str!("help/list-profiles.txt");
const LIST_USAGE: &str = include_str!("help/list-profiles.usage");
const FORMATS: &[&str] = &["text", "json", "markdown"];
fn finite_positive_timeout(value: &str) -> Result<Val, String> {
let parsed = parse_float(value).ok_or_else(|| "must be a number".to_string())?;
if !parsed.is_finite() || parsed <= 0.0 || parsed > MAX_COMMAND_TIMEOUT_SECONDS {
return Err(format!(
"must be greater than 0 and at most {}",
format_g(MAX_COMMAND_TIMEOUT_SECONDS)
));
}
Ok(Val::Float(parsed))
}
fn bounded_positive_integer(value: &str, maximum: i64) -> Result<Val, String> {
let parsed = parse_int(value).ok_or_else(|| "must be an integer".to_string())?;
match parsed {
PyInt::Value(v) if (1..=maximum).contains(&v) => Ok(Val::Int(v)),
_ => Err(format!("must be between 1 and {maximum}")),
}
}
fn max_depth_value(value: &str) -> Result<Val, String> {
bounded_positive_integer(value, MAX_PROFILE_DEPTH)
}
fn max_workers_value(value: &str) -> Result<Val, String> {
bounded_positive_integer(value, MAX_SCAN_WORKERS as i64)
}
fn help_action() -> OptAction {
OptAction {
strings: &["-h", "--help"],
dest: "help",
kind: Kind::Help,
default: None,
}
}
fn scan_like_parser(prog: &'static str, usage: &'static str, help: &'static str) -> Parser {
let store = |strings, dest, choices, convert, default| OptAction {
strings,
dest,
kind: Kind::Store { choices, convert },
default,
};
let flag = |strings, dest| OptAction {
strings,
dest,
kind: Kind::StoreTrue,
default: Some(Val::Bool(false)),
};
Parser {
prog,
usage,
help,
actions: vec![
help_action(),
store(
&["--format"],
"format",
Some(FORMATS),
None,
Some(Val::Str("text".to_string())),
),
store(&["--profile"], "profile", None, None, None),
store(&["--config"], "config", None, None, None),
store(
&["--timeout"],
"timeout",
None,
Some(finite_positive_timeout),
Some(Val::Float(COMMAND_TIMEOUT_SECONDS)),
),
store(
&["--max-depth"],
"max_depth",
None,
Some(max_depth_value),
Some(Val::Int(MAX_PROFILE_DEPTH)),
),
store(
&["--max-workers"],
"max_workers",
None,
Some(max_workers_value),
Some(Val::Int(DEFAULT_SCAN_WORKERS as i64)),
),
flag(&["--no-parallel"], "no_parallel"),
flag(&["--include-vendored"], "include_vendored"),
flag(&["--redact"], "redact"),
],
mutex_groups: vec![vec![2, 3]],
subparsers: Vec::new(),
}
}
/// Build the top-level parser.
pub fn build_parser() -> Parser {
Parser {
prog: "devcap",
usage: MAIN_USAGE,
help: MAIN_HELP,
actions: vec![help_action()],
mutex_groups: Vec::new(),
subparsers: vec![
(
"scan",
scan_like_parser("devcap scan", SCAN_USAGE, SCAN_HELP),
),
(
"check",
scan_like_parser("devcap check", CHECK_USAGE, CHECK_HELP),
),
(
"list-profiles",
Parser {
prog: "devcap list-profiles",
usage: LIST_USAGE,
help: LIST_HELP,
actions: vec![help_action()],
mutex_groups: Vec::new(),
subparsers: Vec::new(),
},
),
],
}
}
/// Exit status used when stdout cannot be written (CPython exits 120 when
/// flushing stdout fails at shutdown).
const WRITE_FAILURE: i32 = 120;
/// Run the CLI with explicit output streams; returns the exit status.
pub fn main_with(argv: &[String], out: &mut dyn Write, err: &mut dyn Write) -> i32 {
let parser = build_parser();
let args = match parser.parse_args(argv) {
Ok(args) => args,
Err(Exit {
code,
stdout,
stderr,
}) => {
let _ = out.write_all(stdout.as_bytes());
let _ = err.write_all(backslash_escapes(&stderr).as_bytes());
return code;
}
};
let code = match args.command.as_deref() {
Some("list-profiles") => cmd_list_profiles(out),
Some("scan") | Some("check") => cmd_scan(&args, out, err),
_ => write_or_fail(out, MAIN_HELP, 0),
};
if out.flush().is_err() {
return WRITE_FAILURE;
}
code
}
fn write_or_fail(out: &mut dyn Write, text: &str, code: i32) -> i32 {
match out.write_all(text.as_bytes()) {
Ok(()) => code,
Err(_) => WRITE_FAILURE,
}
}
fn cmd_list_profiles(out: &mut dyn Write) -> i32 {
let mut text = String::from("Available profiles:\n");
for name in list_builtin_profiles() {
let profile = match load_builtin_profile(name, MAX_PROFILE_DEPTH) {
Ok(p) => p,
Err(e) => unreachable!("built-in profile {name} is invalid: {e}"),
};
text.push_str(&format!(
" {name:<16} {} ({} tools)\n",
profile.description,
profile.tools.len()
));
}
write_or_fail(out, &text, 0)
}
fn cmd_scan(args: &Namespace, out: &mut dyn Write, err: &mut dyn Write) -> i32 {
let max_depth = args.int("max_depth").unwrap_or(MAX_PROFILE_DEPTH);
let profile_arg = args.str("profile").filter(|p| !p.is_empty());
let config_arg = args.str("config").filter(|c| !c.is_empty());
let loaded = if let Some(config) = config_arg {
load_custom_profile(config, max_depth)
} else if let Some(profile) = profile_arg {
load_builtin_profile(profile, max_depth)
} else {
load_builtin_profile("full", max_depth)
};
let profile = match loaded {
Ok(profile) => profile,
Err(ProfileError::NotFound(_)) => {
let message = match profile_arg {
Some(name) => format!(
"Error: unknown profile '{name}'\nAvailable: {}\n",
list_builtin_profiles().join(", ")
),
None => format!(
"Error: profile not found: {}\n",
args.str("config").unwrap_or_default()
),
};
let _ = err.write_all(backslash_escapes(&message).as_bytes());
return 2;
}
Err(ProfileError::Invalid(msg)) => {
let message = format!("Error: invalid profile: {msg}\n");
let _ = err.write_all(backslash_escapes(&message).as_bytes());
return 2;
}
};
let max_workers = args
.int("max_workers")
.unwrap_or(DEFAULT_SCAN_WORKERS as i64) as usize;
let result = scan_tools(
Some(&profile.tools),
Some(&profile.services),
!args.flag("no_parallel"),
max_workers,
args.flag("include_vendored"),
args.float("timeout").unwrap_or(COMMAND_TIMEOUT_SECONDS),
);
let mut result = match result {
Ok(r) => r,
Err(msg) => {
// Unreachable from the CLI: arguments are validated above.
let _ = err.write_all(backslash_escapes(&format!("Error: {msg}\n")).as_bytes());
return 2;
}
};
if args.flag("redact") {
result = redact_scan(&result, true, true);
}
let format = args.str("format").unwrap_or("text");
let mut rendered = format_by_name(format, &result);
rendered.push('\n');
// Text and Markdown write undecodable path bytes back out unchanged
// (CPython's UTF-8 mode); JSON output is pure ASCII.
if out.write_all(&fsencode(&rendered)).is_err() {
return WRITE_FAILURE;
}
if args.command.as_deref() == Some("check") && !profile.required_tools.is_empty() {
let missing: Vec<&str> = result
.results
.iter()
.filter(|r| profile.required_tools.contains(&r.name) && !r.found)
.map(|r| r.name.as_str())
.collect();
if !missing.is_empty() {
if out.flush().is_err() {
return WRITE_FAILURE;
}
let _ = err.write_all(
format!("\nMissing required tools: {}\n", missing.join(", ")).as_bytes(),
);
return 1;
}
}
0
}
#[cfg(test)]
mod tests {
use super::*;
fn run(args: &[&str]) -> (i32, String, String) {
let argv: Vec<String> = args.iter().map(|s| s.to_string()).collect();
let mut out = Vec::new();
let mut err = Vec::new();
let code = main_with(&argv, &mut out, &mut err);
(
code,
String::from_utf8(out).unwrap(),
String::from_utf8(err).unwrap(),
)
}
fn scan_error(args: &[&str]) -> String {
let (code, out, err) = run(args);
assert_eq!(code, 2, "{args:?}");
assert!(out.is_empty());
err.lines().last().unwrap_or_default().to_string()
}
#[test]
fn help_and_no_args() {
let (code, out, err) = run(&[]);
assert_eq!((code, out.as_str(), err.as_str()), (0, MAIN_HELP, ""));
assert_eq!(run(&["--he"]).1, MAIN_HELP);
assert_eq!(run(&["scan", "-h", "--timeout", "0"]).1, SCAN_HELP);
assert_eq!(run(&["scan", "-hx"]).1, SCAN_HELP);
assert_eq!(run(&["scan", "-hhh"]).1, SCAN_HELP);
assert_eq!(run(&["check", "--help"]).1, CHECK_HELP);
assert_eq!(run(&["list-profiles", "-h"]).1, LIST_HELP);
}
#[test]
fn argparse_error_messages() {
assert_eq!(
scan_error(&["scan", "--max", "3"]),
"devcap scan: error: ambiguous option: --max could match --max-depth, --max-workers"
);
assert_eq!(
scan_error(&["scan", "--max=3"]),
"devcap scan: error: ambiguous option: --max=3 could match --max-depth, --max-workers"
);
assert_eq!(
scan_error(&["scan", "--timeout", "-1x"]),
"devcap scan: error: argument --timeout: must be a number"
);
assert_eq!(
scan_error(&["scan", "--timeout", "-1e5"]),
"devcap scan: error: argument --timeout: must be greater than 0 and at most 60"
);
assert_eq!(
scan_error(&["scan", "--profile", "-x"]),
"devcap scan: error: argument --profile: expected one argument"
);
assert_eq!(
scan_error(&["scan", "--no-parallel=1"]),
"devcap scan: error: argument --no-parallel: ignored explicit argument '1'"
);
assert_eq!(
scan_error(&["scan", "--help=x"]),
"devcap scan: error: argument -h/--help: ignored explicit argument 'x'"
);
assert_eq!(
scan_error(&["scan", "-h=x"]),
"devcap scan: error: argument -h/--help: ignored explicit argument 'x'"
);
assert_eq!(
scan_error(&["-x"]),
"devcap: error: unrecognized arguments: -x"
);
assert_eq!(
scan_error(&["-x", "scan", "--profile", "nope"]),
"devcap: error: unrecognized arguments: -x"
);
assert_eq!(
scan_error(&["scan", "--", "--profile", "nope"]),
"devcap: error: unrecognized arguments: -- --profile nope"
);
assert_eq!(
scan_error(&["--format", "json"]),
"devcap: error: argument command: invalid choice: 'json' (choose from 'scan', 'check', 'list-profiles')"
);
assert_eq!(
scan_error(&["scan", "--profile", "a", "--config", ""]),
"devcap scan: error: argument --config: not allowed with argument --profile"
);
assert_eq!(
scan_error(&["scan", "--format="]),
"devcap scan: error: argument --format: invalid choice: '' (choose from 'text', 'json', 'markdown')"
);
assert_eq!(
scan_error(&["scan", "--max-depth", "99999999999999999999999"]),
"devcap scan: error: argument --max-depth: must be between 1 and 16"
);
assert_eq!(
scan_error(&["scan", "-x", "-y"]),
"devcap: error: unrecognized arguments: -x -y"
);
assert_eq!(
scan_error(&["--version"]),
"devcap: error: unrecognized arguments: --version"
);
assert_eq!(
scan_error(&["scan", "-=x"]),
"devcap scan: error: ambiguous option: -=x could match -h, --help, --format, --profile, --config, --timeout, --max-depth, --max-workers, --no-parallel, --include-vendored, --redact"
);
}
#[test]
fn usage_precedes_error() {
let (_, _, err) = run(&["scan", "--timeout", "0"]);
assert_eq!(
err,
format!(
"{SCAN_USAGE}devcap scan: error: argument --timeout: must be greater than 0 and at most 60\n"
)
);
let (_, _, err) = run(&["bogus"]);
assert!(err.starts_with(MAIN_USAGE));
}
#[test]
fn profile_errors() {
let (code, _, err) = run(&["scan", "--profile", "nope"]);
assert_eq!(code, 2);
assert_eq!(
err,
"Error: unknown profile 'nope'\nAvailable: devops, full, node-dev, python-dev, rust-dev, sysadmin\n"
);
let (code, _, err) = run(&["scan", "--profile", "../x"]);
assert_eq!(code, 2);
assert_eq!(
err,
"Error: invalid profile: profile name must be a command name, not a path or shell expression\n"
);
let (code, _, err) = run(&["scan", "--config", "/nonexist.toml"]);
assert_eq!(code, 2);
assert_eq!(err, "Error: profile not found: /nonexist.toml\n");
let (code, _, err) = run(&["--", "scan", "--max-depth", " +0_8 ", "--profile", "nope"]);
assert_eq!(code, 2);
assert!(err.starts_with("Error: unknown profile 'nope'"));
}
#[test]
fn list_profiles_output() {
let (code, out, _) = run(&["list-profiles"]);
assert_eq!(code, 0);
assert_eq!(
out,
"Available profiles:\n devops DevOps and infrastructure toolchain (20 tools)\n full Complete scan of all known tools and services (103 tools)\n node-dev Node.js / JavaScript development environment (13 tools)\n python-dev Python development environment (12 tools)\n rust-dev Rust development environment (11 tools)\n sysadmin Linux system administration toolkit (22 tools)\n"
);
}
}