返回 CodeWhale
legacy.rs
根目录 / crates / tui / src / commands / groups / plugins / legacy.rs
1 //! Legacy executable plugin-tool inventory (`[tools].plugin_dir`).
2 //!
3 //! These are scripts, not declarative bundles: they are discovered by
4 //! scanning a directory, they carry their own approval requirement, and
5 //! they never share bundle trust state. `/plugin tools` reports them
6 //! read-only — nothing here installs, trusts, or executes anything.
7 //!
8 //! FEAT-020: this module consumes the portable `PluginLegacyScan` from the
9 //! plugin facet; no concrete `App` or `PluginMetadata` crosses the boundary.
10
11 use super::render::ReviewTextContext;
12 use codewhale_command_contract::facets::{CommandPluginContext, CommandPresentationContext};
13 use std::fmt::Write as _;
14
15 use crate::commands::CommandResult;
16
17 pub(super) fn legacy_tools(
18 presentation: &mut dyn CommandPresentationContext,
19 plugin: &dyn CommandPluginContext,
20 name: Option<&str>,
21 ) -> CommandResult {
22 let scan = match plugin.legacy_scan() {
23 Ok(Some(scan)) => scan,
24 Ok(None) | Err(_) => {
25 return super::action_error(
26 presentation,
27 "Could not resolve the legacy executable plugin-tool directory",
28 );
29 }
30 };
31 match name {
32 Some(name) => show_legacy_tool_detail(presentation, name, &scan),
33 None => list_legacy_tools(presentation, &scan),
34 }
35 }
36
37 fn list_legacy_tools(
38 presentation: &mut dyn CommandPresentationContext,
39 scan: &codewhale_command_contract::facets::PluginLegacyScan,
40 ) -> CommandResult {
41 if scan.tools.is_empty() {
42 return CommandResult::message(
43 presentation
44 .translate(
45 "cmd_plugin_none_found",
46 &[("dir", &scan.dir.display().to_string())],
47 )
48 .unwrap_or_default(),
49 );
50 }
51 let mut output = presentation
52 .translate(
53 "cmd_plugin_legacy_list_header",
54 &[
55 ("count", &scan.tools.len().to_string()),
56 ("dir", &scan.dir.display().to_string()),
57 ],
58 )
59 .unwrap_or_default();
60 output.push('\n');
61 for tool in &scan.tools {
62 let _ = writeln!(
63 output,
64 "• {} — {}\n {}",
65 tool.name,
66 tool.description,
67 tool.path.display()
68 );
69 }
70 super::append_diagnostics(
71 presentation,
72 &mut output,
73 &scan.diagnostics,
74 ReviewTextContext::Prose,
75 );
76 CommandResult::message(output)
77 }
78
79 fn show_legacy_tool_detail(
80 presentation: &mut dyn CommandPresentationContext,
81 name: &str,
82 scan: &codewhale_command_contract::facets::PluginLegacyScan,
83 ) -> CommandResult {
84 let Some(tool) = scan.tools.iter().find(|tool| tool.name == name) else {
85 return CommandResult::error(
86 presentation
87 .translate("cmd_plugin_not_found", &[("name", name)])
88 .unwrap_or_default(),
89 );
90 };
91 let schema = tool
92 .input_schema
93 .clone()
94 .unwrap_or_else(|| "{}".to_string());
95 let mut output = format!("{}\n{:=<40}\n", tool.name, "");
96 let _ = writeln!(
97 output,
98 "{}",
99 presentation
100 .translate(
101 "cmd_plugin_detail_description",
102 &[("description", &tool.description)],
103 )
104 .unwrap_or_default()
105 );
106 let _ = writeln!(
107 output,
108 "{}",
109 presentation
110 .translate("cmd_plugin_detail_schema", &[("schema", &schema)])
111 .unwrap_or_default()
112 );
113 let _ = writeln!(
114 output,
115 "{}",
116 presentation
117 .translate(
118 "cmd_plugin_detail_approval",
119 &[("approval", &tool.approval)]
120 )
121 .unwrap_or_default()
122 );
123 let _ = writeln!(
124 output,
125 "{}",
126 presentation
127 .translate(
128 "cmd_plugin_detail_path",
129 &[("path", &tool.path.display().to_string())]
130 )
131 .unwrap_or_default()
132 );
133 let diagnostics: Vec<_> = scan
134 .diagnostics
135 .iter()
136 .filter(|diagnostic| diagnostic.path.as_deref() == Some(tool.path.as_path()))
137 .cloned()
138 .collect();
139 super::append_diagnostics(
140 presentation,
141 &mut output,
142 &diagnostics,
143 ReviewTextContext::Prose,
144 );
145 CommandResult::message(output)
146 }
147
147 lines RUST