-
-
Notifications
You must be signed in to change notification settings - Fork 829
Expand file tree
/
Copy pathexternal_cli.rs
More file actions
625 lines (552 loc) · 18 KB
/
Copy pathexternal_cli.rs
File metadata and controls
625 lines (552 loc) · 18 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
use std::collections::{BTreeMap, HashMap};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
use anyhow::{Context, Result, bail};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use crate::cli::{CliToolAction, CliToolCommand};
use crate::config;
const MANIFEST_NAME: &str = "flow-tool.toml";
const LINK_RECORD_VERSION: u32 = 1;
#[derive(Debug, Clone, Deserialize)]
pub struct ExternalCliManifest {
pub version: u32,
pub id: String,
pub language: String,
pub binary_name: String,
pub description: Option<String>,
pub exec: ExternalCliExec,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ExternalCliExec {
pub run: Vec<String>,
pub build: Option<Vec<String>>,
pub env: Option<HashMap<String, String>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ExternalCliLinkRecord {
version: u32,
id: String,
source_root: PathBuf,
manifest_path: PathBuf,
installed_at: String,
description: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExternalCliResolutionKind {
InstalledLink,
DevRoot,
}
impl ExternalCliResolutionKind {
fn label(self) -> &'static str {
match self {
Self::InstalledLink => "installed-link",
Self::DevRoot => "dev-root",
}
}
}
#[derive(Debug, Clone)]
pub struct ResolvedExternalCliTool {
pub source_root: PathBuf,
pub manifest_path: PathBuf,
pub manifest: ExternalCliManifest,
pub resolution: ExternalCliResolutionKind,
pub registration_path: Option<PathBuf>,
}
pub fn run_command(cmd: CliToolCommand) -> Result<()> {
match cmd.action {
Some(CliToolAction::List) => print_cli_list(),
Some(CliToolAction::Which { id }) => print_cli_which(&id),
Some(CliToolAction::Doctor { id }) => print_cli_doctor(&id),
None => {
if let Some(id) = cmd
.id
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
{
run_external_cli(id, cmd.args.iter().map(String::as_str))
} else {
print_cli_list()
}
}
}
}
pub fn resolve_external_cli_tool(id: &str) -> Result<ResolvedExternalCliTool> {
if let Some(installed) = resolve_installed_external_cli_tool(id)? {
return Ok(installed);
}
let roots = default_external_cli_roots()?;
resolve_external_cli_tool_in_roots(id, &roots)
}
pub fn list_external_cli_tools() -> Result<Vec<ResolvedExternalCliTool>> {
let mut tools = BTreeMap::new();
for (record, record_path) in load_link_records()? {
let tool = resolved_from_link_record(&record, Some(record_path))?;
tools.insert(tool.manifest.id.clone(), tool);
}
for root in default_external_cli_roots()? {
for candidate in candidate_manifest_paths(&root) {
let manifest = read_manifest(&candidate)?;
if tools.contains_key(&manifest.id) {
continue;
}
let tool = resolved_from_manifest(
candidate,
manifest,
ExternalCliResolutionKind::DevRoot,
None,
)?;
tools.insert(tool.manifest.id.clone(), tool);
}
}
Ok(tools.into_values().collect())
}
pub fn install_external_cli_link(path: &Path, force: bool) -> Result<ResolvedExternalCliTool> {
let (source_root, manifest_path) = resolve_install_source_paths(path)?;
let source_root = fs::canonicalize(&source_root)
.with_context(|| format!("failed to resolve {}", source_root.display()))?;
let manifest_path = fs::canonicalize(&manifest_path)
.with_context(|| format!("failed to resolve {}", manifest_path.display()))?;
let manifest = read_manifest(&manifest_path)?;
let record = ExternalCliLinkRecord {
version: LINK_RECORD_VERSION,
id: manifest.id.clone(),
source_root,
manifest_path,
installed_at: Utc::now().to_rfc3339(),
description: manifest.description.clone(),
};
let links_dir = ensure_link_records_dir()?;
let record_path = links_dir.join(format!("{}.toml", record.id));
if record_path.is_file() {
let existing = read_link_record(&record_path)?;
if !same_link_target(&existing, &record) && !force {
bail!(
"external CLI {} is already linked to {} (use --force to replace it)",
record.id,
existing.source_root.display()
);
}
}
let content = toml::to_string_pretty(&record)
.with_context(|| format!("failed to serialize {}", record.id))?;
fs::write(&record_path, content)
.with_context(|| format!("failed to write {}", record_path.display()))?;
resolved_from_link_record(&record, Some(record_path))
}
pub fn command_for_external_cli<I, S>(
id: &str,
args: I,
) -> Result<(ResolvedExternalCliTool, Command)>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let tool = resolve_external_cli_tool(id)?;
let command = tool.command(args)?;
Ok((tool, command))
}
pub fn run_external_cli<I, S>(id: &str, args: I) -> Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let (tool, mut command) = command_for_external_cli(id, args)?;
command
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
let status = command.status().with_context(|| {
format!(
"failed to launch external CLI {} from {}",
id,
tool.manifest_path.display()
)
})?;
ensure_success(id, status)
}
impl ResolvedExternalCliTool {
pub fn command<I, S>(&self, args: I) -> Result<Command>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
if self.manifest.exec.run.is_empty() {
bail!(
"{} has no exec.run argv in {}",
self.manifest.id,
self.manifest_path.display()
);
}
let mut argv = self.manifest.exec.run.iter();
let Some(program) = argv.next() else {
bail!(
"{} has no exec.run program in {}",
self.manifest.id,
self.manifest_path.display()
);
};
let mut command = Command::new(program);
command.current_dir(&self.source_root);
command.args(argv);
for arg in args {
command.arg(arg.as_ref());
}
if let Some(env_map) = &self.manifest.exec.env {
command.envs(env_map);
}
Ok(command)
}
}
fn print_cli_list() -> Result<()> {
let tools = list_external_cli_tools()?;
if tools.is_empty() {
println!("No external CLIs found.");
return Ok(());
}
for tool in tools {
println!(
"{}\t{}\t{}",
tool.manifest.id,
tool.resolution.label(),
tool.source_root.display()
);
}
Ok(())
}
fn print_cli_which(id: &str) -> Result<()> {
let tool = resolve_external_cli_tool(id)?;
println!("{}", tool.source_root.display());
Ok(())
}
fn print_cli_doctor(id: &str) -> Result<()> {
let tool = resolve_external_cli_tool(id)?;
println!("id: {}", tool.manifest.id);
println!("resolution: {}", tool.resolution.label());
println!("source_root: {}", tool.source_root.display());
println!("manifest_path: {}", tool.manifest_path.display());
if let Some(path) = &tool.registration_path {
println!("registration_path: {}", path.display());
}
println!("language: {}", tool.manifest.language);
println!("binary_name: {}", tool.manifest.binary_name);
if let Some(description) = &tool.manifest.description {
println!("description: {}", description);
}
println!("run: {}", render_argv(&tool.manifest.exec.run));
if let Some(build) = &tool.manifest.exec.build {
println!("build: {}", render_argv(build));
}
if let Some(env_map) = &tool.manifest.exec.env {
if env_map.is_empty() {
println!("env: none");
} else {
let mut keys = env_map.keys().cloned().collect::<Vec<_>>();
keys.sort();
println!("env: {}", keys.join(", "));
}
} else {
println!("env: none");
}
Ok(())
}
fn ensure_success(id: &str, status: ExitStatus) -> Result<()> {
if status.success() {
Ok(())
} else {
bail!(
"external CLI {} exited unsuccessfully with status {}",
id,
status
)
}
}
fn render_argv(argv: &[String]) -> String {
argv.iter()
.map(|arg| {
if arg.chars().any(char::is_whitespace) {
format!("{arg:?}")
} else {
arg.clone()
}
})
.collect::<Vec<_>>()
.join(" ")
}
fn same_link_target(left: &ExternalCliLinkRecord, right: &ExternalCliLinkRecord) -> bool {
left.source_root == right.source_root && left.manifest_path == right.manifest_path
}
fn resolve_installed_external_cli_tool(id: &str) -> Result<Option<ResolvedExternalCliTool>> {
let record_path = link_record_path(id);
if !record_path.is_file() {
return Ok(None);
}
let record = read_link_record(&record_path)?;
Ok(Some(resolved_from_link_record(&record, Some(record_path))?))
}
fn load_link_records() -> Result<Vec<(ExternalCliLinkRecord, PathBuf)>> {
let links_dir = link_records_dir();
if !links_dir.is_dir() {
return Ok(Vec::new());
}
let mut records = Vec::new();
for path in candidate_link_record_paths(&links_dir) {
let record = read_link_record(&path)?;
records.push((record, path));
}
Ok(records)
}
fn default_external_cli_roots() -> Result<Vec<PathBuf>> {
let home =
dirs::home_dir().context("could not resolve home directory for external CLI roots")?;
Ok(vec![
home.join("code/lang/go/cli"),
home.join("code/lang/rust/cli"),
])
}
fn resolve_external_cli_tool_in_roots(
id: &str,
roots: &[PathBuf],
) -> Result<ResolvedExternalCliTool> {
let mut matches = Vec::new();
for root in roots {
for candidate in candidate_manifest_paths(root) {
let manifest = read_manifest(&candidate)?;
if manifest.id == id {
matches.push(resolved_from_manifest(
candidate,
manifest,
ExternalCliResolutionKind::DevRoot,
None,
)?);
}
}
}
match matches.len() {
0 => bail!(
"external CLI tool {} not found under {}",
id,
roots
.iter()
.map(|path| path.display().to_string())
.collect::<Vec<_>>()
.join(", ")
),
1 => Ok(matches.remove(0)),
_ => {
let locations = matches
.iter()
.map(|tool| tool.manifest_path.display().to_string())
.collect::<Vec<_>>()
.join(", ");
bail!(
"multiple external CLI manifests matched {}: {}",
id,
locations
)
}
}
}
fn resolved_from_link_record(
record: &ExternalCliLinkRecord,
registration_path: Option<PathBuf>,
) -> Result<ResolvedExternalCliTool> {
let manifest = read_manifest(&record.manifest_path)?;
if manifest.id != record.id {
bail!(
"external CLI link {} points to manifest with mismatched id {}",
record.id,
manifest.id
);
}
resolved_from_manifest(
record.manifest_path.clone(),
manifest,
ExternalCliResolutionKind::InstalledLink,
registration_path,
)
}
fn resolved_from_manifest(
manifest_path: PathBuf,
manifest: ExternalCliManifest,
resolution: ExternalCliResolutionKind,
registration_path: Option<PathBuf>,
) -> Result<ResolvedExternalCliTool> {
let source_root = manifest_path
.parent()
.unwrap_or_else(|| Path::new("."))
.to_path_buf();
Ok(ResolvedExternalCliTool {
source_root,
manifest_path,
manifest,
resolution,
registration_path,
})
}
fn resolve_install_source_paths(path: &Path) -> Result<(PathBuf, PathBuf)> {
let meta = fs::metadata(path)
.with_context(|| format!("failed to read external CLI source {}", path.display()))?;
if meta.is_dir() {
let manifest_path = path.join(MANIFEST_NAME);
if !manifest_path.is_file() {
bail!("{} does not contain {}", path.display(), MANIFEST_NAME);
}
return Ok((path.to_path_buf(), manifest_path));
}
if meta.is_file() && path.file_name().and_then(|name| name.to_str()) == Some(MANIFEST_NAME) {
let source_root = path
.parent()
.unwrap_or_else(|| Path::new("."))
.to_path_buf();
return Ok((source_root, path.to_path_buf()));
}
bail!(
"{} must be an external CLI source directory or {} file",
path.display(),
MANIFEST_NAME
)
}
fn link_record_path(id: &str) -> PathBuf {
link_records_dir().join(format!("{}.toml", id))
}
fn link_records_dir() -> PathBuf {
config::global_config_dir().join("cli").join("links")
}
fn ensure_link_records_dir() -> Result<PathBuf> {
let dir = link_records_dir();
fs::create_dir_all(&dir).with_context(|| format!("failed to create {}", dir.display()))?;
Ok(dir)
}
fn candidate_link_record_paths(root: &Path) -> Vec<PathBuf> {
let mut paths = Vec::new();
let Ok(entries) = fs::read_dir(root) else {
return paths;
};
for entry in entries.flatten() {
let path = entry.path();
if path.extension().and_then(|value| value.to_str()) != Some("toml") {
continue;
}
if path.is_file() {
paths.push(path);
}
}
paths
}
fn candidate_manifest_paths(root: &Path) -> Vec<PathBuf> {
let mut manifests = Vec::new();
let Ok(entries) = fs::read_dir(root) else {
return manifests;
};
for entry in entries.flatten() {
let path = entry.path();
if !path.is_dir() {
continue;
}
let manifest = path.join(MANIFEST_NAME);
if manifest.is_file() {
manifests.push(manifest);
}
}
manifests
}
fn read_link_record(path: &Path) -> Result<ExternalCliLinkRecord> {
let content =
fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;
let record: ExternalCliLinkRecord =
toml::from_str(&content).with_context(|| format!("failed to parse {}", path.display()))?;
if record.version != LINK_RECORD_VERSION {
bail!(
"unsupported external CLI link record version {} in {}",
record.version,
path.display()
);
}
Ok(record)
}
fn read_manifest(path: &Path) -> Result<ExternalCliManifest> {
let content =
fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;
let manifest: ExternalCliManifest =
toml::from_str(&content).with_context(|| format!("failed to parse {}", path.display()))?;
if manifest.version != 1 {
bail!(
"unsupported external CLI manifest version {} in {}",
manifest.version,
path.display()
);
}
if manifest.exec.run.is_empty() {
bail!("missing exec.run in {}", path.display());
}
Ok(manifest)
}
#[cfg(test)]
mod tests {
use super::*;
fn write_tool(root: &Path, id: &str) -> PathBuf {
let tool_root = root.join(id);
fs::create_dir_all(&tool_root).expect("create tool root");
fs::write(
tool_root.join(MANIFEST_NAME),
format!(
r#"
version = 1
id = "{id}"
language = "go"
binary_name = "{id}"
[exec]
run = ["go", "run", "."]
"#
),
)
.expect("write manifest");
tool_root
}
#[test]
fn resolves_manifest_from_roots() {
let temp = tempfile::tempdir().expect("tempdir");
let go_root = temp.path().join("go");
let tool_root = write_tool(&go_root, "codex-session-browser");
let resolved =
resolve_external_cli_tool_in_roots("codex-session-browser", &[go_root.clone()])
.expect("resolve tool");
assert_eq!(resolved.manifest.id, "codex-session-browser");
assert_eq!(resolved.source_root, tool_root);
assert_eq!(resolved.resolution, ExternalCliResolutionKind::DevRoot);
assert_eq!(resolved.manifest.exec.run[0], "go");
}
#[test]
fn install_link_writes_record_and_resolves() {
let temp = tempfile::tempdir().expect("tempdir");
let tool_root = write_tool(temp.path(), "codex-session-browser");
let links_dir = temp.path().join("links");
fs::create_dir_all(&links_dir).expect("create links dir");
let manifest_path = tool_root.join(MANIFEST_NAME);
let record = ExternalCliLinkRecord {
version: LINK_RECORD_VERSION,
id: "codex-session-browser".to_string(),
source_root: tool_root.clone(),
manifest_path: manifest_path.clone(),
installed_at: Utc::now().to_rfc3339(),
description: None,
};
let record_path = links_dir.join("codex-session-browser.toml");
let content = toml::to_string_pretty(&record).expect("serialize record");
fs::write(&record_path, content).expect("write record");
let loaded = read_link_record(&record_path).expect("read record");
let resolved =
resolved_from_link_record(&loaded, Some(record_path.clone())).expect("resolve record");
assert_eq!(resolved.manifest.id, "codex-session-browser");
assert_eq!(resolved.source_root, tool_root);
assert_eq!(resolved.manifest_path, manifest_path);
assert_eq!(resolved.registration_path, Some(record_path));
assert_eq!(
resolved.resolution,
ExternalCliResolutionKind::InstalledLink
);
}
}