Skip to content

Commit 531215f

Browse files
authored
took the functionality of the third party subcommand from the list_commands function (#15075)
Hey there, I just moved the functionality of the third-party subcommand from the [list_commands](https://github.com/rust-lang/cargo/blob/master/src/bin/cargo/main.rs#L184) function to another new function by the name of third_party_subcommand name and called that function in the list_commands function. From my understanding regarding the third-party subcommand from this [tracking issue](#14520), following points should be performed. - The code that gathers third-party subcommands in list_commands should be moved into a separate function that I did. - This new function will be called both by list_commands and the code for adding subcommand completions. Although I called the function in the list_commands but didn't understand the second point. - Test the change that I made in the first place but it gave me this error: ![carbon (68)](https://github.com/user-attachments/assets/fb4c5a55-ea4b-4d22-b187-c2a417f5128d) <!-- Thanks for submitting a pull request 🎉! Here are some tips for you: * If this is your first contribution, read "Cargo Contribution Guide" first: https://doc.crates.io/contrib/ * Run `cargo fmt --all` to format your code changes. * Small commits and pull requests are always preferable and easy to review. * If your idea is large and needs feedback from the community, read how: https://doc.crates.io/contrib/process/#working-on-large-features * Cargo takes care of compatibility. Read our design principles: https://doc.crates.io/contrib/design.html * When changing help text of cargo commands, follow the steps to generate docs: https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages * If your PR is not finished, set it as "draft" PR or add "WIP" in its title. * It's ok to use the CI resources to test your PR, but please don't abuse them. ### What does this PR try to resolve? Explain the motivation behind this change. A clear overview along with an in-depth explanation are helpful. You can use `Fixes #<issue number>` to associate this PR to an existing issue. ### How should we test and review this PR? Demonstrate how you test this change and guide reviewers through your PR. With a smooth review process, a pull request usually gets reviewed quicker. If you don't know how to write and run your tests, please read the guide: https://doc.crates.io/contrib/tests ### Additional information Other information you want to mention in this PR, such as prior arts, future extensions, an unresolved problem, or a TODO list. -->
2 parents dc0d02a + ea83afd commit 531215f

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

src/bin/cargo/main.rs

+32-27
Original file line numberDiff line numberDiff line change
@@ -182,33 +182,7 @@ fn aliased_command(gctx: &GlobalContext, command: &str) -> CargoResult<Option<Ve
182182

183183
/// List all runnable commands
184184
fn list_commands(gctx: &GlobalContext) -> BTreeMap<String, CommandInfo> {
185-
let prefix = "cargo-";
186-
let suffix = env::consts::EXE_SUFFIX;
187-
let mut commands = BTreeMap::new();
188-
for dir in search_directories(gctx) {
189-
let entries = match fs::read_dir(dir) {
190-
Ok(entries) => entries,
191-
_ => continue,
192-
};
193-
for entry in entries.filter_map(|e| e.ok()) {
194-
let path = entry.path();
195-
let Some(filename) = path.file_name().and_then(|s| s.to_str()) else {
196-
continue;
197-
};
198-
let Some(name) = filename
199-
.strip_prefix(prefix)
200-
.and_then(|s| s.strip_suffix(suffix))
201-
else {
202-
continue;
203-
};
204-
if is_executable(entry.path()) {
205-
commands.insert(
206-
name.to_string(),
207-
CommandInfo::External { path: path.clone() },
208-
);
209-
}
210-
}
211-
}
185+
let mut commands = third_party_subcommands(gctx);
212186

213187
for cmd in commands::builtin() {
214188
commands.insert(
@@ -253,6 +227,37 @@ fn list_commands(gctx: &GlobalContext) -> BTreeMap<String, CommandInfo> {
253227
commands
254228
}
255229

230+
fn third_party_subcommands(gctx: &GlobalContext) -> BTreeMap<String, CommandInfo> {
231+
let prefix = "cargo-";
232+
let suffix = env::consts::EXE_SUFFIX;
233+
let mut commands = BTreeMap::new();
234+
for dir in search_directories(gctx) {
235+
let entries = match fs::read_dir(dir) {
236+
Ok(entries) => entries,
237+
_ => continue,
238+
};
239+
for entry in entries.filter_map(|e| e.ok()) {
240+
let path = entry.path();
241+
let Some(filename) = path.file_name().and_then(|s| s.to_str()) else {
242+
continue;
243+
};
244+
let Some(name) = filename
245+
.strip_prefix(prefix)
246+
.and_then(|s| s.strip_suffix(suffix))
247+
else {
248+
continue;
249+
};
250+
if is_executable(entry.path()) {
251+
commands.insert(
252+
name.to_string(),
253+
CommandInfo::External { path: path.clone() },
254+
);
255+
}
256+
}
257+
}
258+
commands
259+
}
260+
256261
fn find_external_subcommand(gctx: &GlobalContext, cmd: &str) -> Option<PathBuf> {
257262
let command_exe = format!("cargo-{}{}", cmd, env::consts::EXE_SUFFIX);
258263
search_directories(gctx)

0 commit comments

Comments
 (0)