Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selecting toolchains to uninstall with glob pattern #2540

Closed
wants to merge 9 commits into from

Conversation

camelid
Copy link
Member

@camelid camelid commented Oct 27, 2020

Closes #2530.

Examples

Correct usage

$ rustup toolchain list
stable-x86_64-apple-darwin (default)
nightly-2020-01-01-x86_64-apple-darwin
nightly-2020-06-01-x86_64-apple-darwin
nightly-x86_64-apple-darwin

$ rustup toolchain uninstall 'nightly-20*'
info: uninstalling toolchain 'nightly-2020-01-01-x86_64-apple-darwin'
info: toolchain 'nightly-2020-01-01-x86_64-apple-darwin' uninstalled
info: uninstalling toolchain 'nightly-2020-06-01-x86_64-apple-darwin'
info: toolchain 'nightly-2020-06-01-x86_64-apple-darwin' uninstalled

$ rustup toolchain list
stable-x86_64-apple-darwin (default)
nightly-x86_64-apple-darwin

Invalid pattern

$ rustup toolchain uninstall '***'
error: Pattern syntax error near position 2: wildcards are either regular `*` or recursive `**`

Work-in-progress:

  • Errors need improvement (instead of just panicking)
  • Needs test
  • Needs CLI docs

    $ rustup toolchain list
    stable-x86_64-apple-darwin (default)
    nightly-2020-04-18-x86_64-apple-darwin
    nightly-2020-04-20-x86_64-apple-darwin
    nightly-x86_64-apple-darwin

    $ rustup toolchain uninstall --regex 'nightly-*'
    info: uninstalling toolchain 'nightly-2020-04-18-x86_64-apple-darwin'
    info: toolchain 'nightly-2020-04-18-x86_64-apple-darwin' uninstalled
    info: uninstalling toolchain 'nightly-2020-04-20-x86_64-apple-darwin'
    info: toolchain 'nightly-2020-04-20-x86_64-apple-darwin' uninstalled
    info: uninstalling toolchain 'nightly-x86_64-apple-darwin'
    info: toolchain 'nightly-x86_64-apple-darwin' uninstalled

    $ rustup toolchain list
    stable-x86_64-apple-darwin (default)
src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
@kinnison
Copy link
Contributor

One comment, before I even look at the code too deeply, I'm not sure regular expressions are ideal for this, instead perhaps it's best to advertise this as shell-glob style patterns. i.e. stable-* rather than stable-.* being needed?

@camelid
Copy link
Member Author

camelid commented Oct 28, 2020

I'm not fully sure what you mean by "advertise as shell-glob style patterns": It takes a regex, not a glob pattern. I realized that I confused myself with the example I tried because I used the regex nightly-* which actually matches the substring nightly, nightly-, nightly--, etc.

src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
src/config.rs Outdated Show resolved Hide resolved
src/config.rs Show resolved Hide resolved
@kinnison
Copy link
Contributor

Most "mortals" are not regex savvy, but they understand shell glob patterns (i.e. * not .*)

@camelid
Copy link
Member Author

camelid commented Oct 28, 2020

So are you suggesting that we only allow the metacharacter *, escape all other characters, replace the * with .*, and then pass it to regex? I think I agree that we should have it be a shell glob pattern, but I'm not sure what the best way to implement it is.

@camelid
Copy link
Member Author

camelid commented Oct 28, 2020

Oh, I guess we could use the glob crate to do this. Is that what you were thinking?

@kinnison
Copy link
Contributor

If I were working in C I'd just use the fnmatch() function so if there's a similar crate then that might be best. Otherwise as you suggest, the transformation of escaping metacharacters other than * and then changing * for .* would work.

@camelid camelid force-pushed the toolchain-uninstall-regex branch from c39c730 to 7ce4203 Compare October 28, 2020 20:51
@camelid camelid changed the title Allow selecting toolchains to uninstall with regex Allow selecting toolchains to uninstall with glob pattern Oct 28, 2020
src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
src/cli/rustup_mode.rs Outdated Show resolved Hide resolved
src/config.rs Show resolved Hide resolved
Comment on lines 1329 to 1330
info!("no toolchains matched pattern '{}'", pattern_str);
return Ok(utils::ExitCode(0));
Copy link
Member Author

@camelid camelid Oct 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should return a non-zero exit code, but I modelled this after rustup toolchain uninstall some-toolchain, which just shows a message with a successful exit code. Should I change both of them to return an error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it has always been "safe" in the shell scripting sense to "remove" a toolchain which isn't present, then we wouldn't want to break peoples' scripting by making them check first before trying. Better to be informational but consistent with past behaviour here I think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure that this makes sense. Patterns are new; failure to match a pattern is a new situation. We can detect a toolchain string that has no pattern characters and do the prior behaviour, if we want to.

That means, we can take a first principles design here if appropriate.

First treat the toolchain as a pattern; if it doesn't match anything,
then treat it as a partial toolchain specifier.
Copy link
Member Author

@camelid camelid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all this needs is some documentation, and then it's ready to go! Where should I put the docs?

Comment on lines +1227 to +1240
#[test]
fn toolchain_uninstall_pattern() {
setup(&|config| {
expect_ok(config, &["rustup", "uninstall", "stable-*"]);
expect_ok(config, &["rustup", "uninstall", "nightly-*"]);
let mut cmd = clitools::cmd(config, "rustup", &["show"]);
clitools::env(config, &mut cmd);
let out = cmd.output().unwrap();
assert!(out.status.success());
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(!stdout.contains(for_host!("'stable-{}'")));
assert!(!stdout.contains(for_host!("'nightly-2015-01-01-{}'")));
});
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied this test code from other tests and modified it. Is the test correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to start the test by installing and asserting that stable and nightly are there. Otherwise I think this is fine.

@camelid
Copy link
Member Author

camelid commented Nov 18, 2020

@kinnison Could you review this again? Thanks!

src/config.rs Show resolved Hide resolved
src/config.rs Show resolved Hide resolved
src/config.rs Show resolved Hide resolved
Comment on lines +1227 to +1240
#[test]
fn toolchain_uninstall_pattern() {
setup(&|config| {
expect_ok(config, &["rustup", "uninstall", "stable-*"]);
expect_ok(config, &["rustup", "uninstall", "nightly-*"]);
let mut cmd = clitools::cmd(config, "rustup", &["show"]);
clitools::env(config, &mut cmd);
let out = cmd.output().unwrap();
assert!(out.status.success());
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(!stdout.contains(for_host!("'stable-{}'")));
assert!(!stdout.contains(for_host!("'nightly-2015-01-01-{}'")));
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to start the test by installing and asserting that stable and nightly are there. Otherwise I think this is fine.

tests/mock/clitools.rs Show resolved Hide resolved
@rami3l
Copy link
Member

rami3l commented Jul 10, 2024

Closing in favor of #3862, as discussed in #3519 (comment). Thanks a lot for helping out nonetheless!

@rami3l rami3l closed this Jul 10, 2024
@djc
Copy link
Contributor

djc commented Jul 10, 2024

@rami3l can you elaborate on your rationale for linking this to #3862? Seems like this is still a reasonable request?

@rami3l
Copy link
Member

rami3l commented Jul 10, 2024

@rami3l can you elaborate on your rationale for linking this to #3862? Seems like this is still a reasonable request?

@djc The idea is that one would be able to do rustup toolchain list --quiet | grep nightly-2023- | xargs rustup uninstall after #3862. Not sure if this addition is still worth it 🤔

If the demand is still there we can make another PR with @camelid as the co-author, I have no problem with that.

@est31
Copy link
Member

est31 commented Jul 10, 2024

It might still make sense to have glob support. At the least one should print the command you suggested when someone tries to use globs with rustup: it's not trivial to come up with it, nor is it guaranteed to always work in the future.

@rami3l
Copy link
Member

rami3l commented Jul 10, 2024

It might still make sense to have glob support. At the least one should print the command you suggested when someone tries to use globs with rustup: it's not trivial to come up with it, nor is it guaranteed to always work in the future.

@est31 Indeed, I've added #2530 back to our roadmap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Glob support for rustup toolchain remove
6 participants