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

add --dry-run option to cli and implement it #946

Closed
wants to merge 2 commits into from
Closed

add --dry-run option to cli and implement it #946

wants to merge 2 commits into from

Conversation

savente93
Copy link

I've implemented a --dry-run since it is a need that I've found myself having when using fd. I'm aware that I should have created an issue first for discussion, which I only realised after implementing the feature. However, because it is somewhat related to #943 as well as being a relatively minor/simple change I decided to open a PR anyway. If there are things that will need discussion I'm happy to close this and open another PR once all the discussions are resolved.

Otherwise, there is not that much to say about this change. All it does is take the dry_run option from the CLI and pass it down to the commands being executed so they can be printed instead of executed. For this I've mostly just repurposed existing code and tests with slight adjustments for the use case. Hoping this will be an easy merge :)

@savente93
Copy link
Author

I wasn't aware of the minimum rust version. The API used was stabilized in 1.57.0 so there are 3 options:

  1. reject this PR/implementation
  2. bump the minimal rust version to 1.57.0
  3. add a #[feature(command_access)]
    Obviously I'd prefer one of the latter two options, but I'll leave it up to a maintainer to comment on which one is best.

@tmccombs
Copy link
Collaborator

What benefit does this have over just prepending an echo in front of the command?

@savente93
Copy link
Author

nothing that I can think of actually, I just didn't think of that. Made the change and ensured it runs and clears tests on 1.54 so should be good now. sorry for that!

@tavianator
Copy link
Collaborator

I think the question is more like, why is --dry-run necessary if the user can just add echo themselves?

@savente93
Copy link
Author

ah, like that. Gotcha.

Well honestly, not all that much. When I implemented this feature I didn't realise that prepending an echo would still capture most arguments as being associated with the correct command nor that it would still expand the placeholders.

I had tried the echo method before but in my cases I found that very cumbersome due to the nested " and ' I thought were necessary to associate the commands correctly and actually lost some files due to doing that incorrectly while using mv. Having a --dry-run option would give (people like) me more security that commands won't be executed which I consider just a QoL feature when I'm working with (critical) files. So I would personally still appreciate the feature, but if you don't think that is worth adopting the additional code over I'd understand too. In that latter case it might be worth considering adding that to the documentation.

@tmccombs
Copy link
Collaborator

FWIW, find doesn't have a --dry-run option, and the recommended way to do a dry-run with -exec is to use an echo. I'd lean towards mentioning that you can use an echo in the documentation.

@sharkdp
Copy link
Owner

sharkdp commented Jan 24, 2022

@savente93 Thank you very much for this contribution. I really appreciate the effort and I definitely think we should do something about the problem you mentioned (commands being executed that you didn't intend to).

I usually also use echo when using fd with rm or mv. Sometimes you can also use the interactive -i option, if there are not too many search results. I agree with the other maintainers that this new option therefore doesn't add too much value (it's also a bit longer to write). But I'm certainly in favor of updating the documentation with this respect.

There is also another problem here: an official --dry-run option might suggest that the shown commands are guaranteed to be executed. It might even suggest that the commands are executed in the shown order. But both assumptions are not true. The execution order is not defined. And the search results could also have changed between running --dry-run --exec <cmd> and --exec <cmd>. So a official --dry-run flag would have to come with a big warning (the echo method has the same problem of course).

A way to potentially solve this (although I'm not convinced this is a good idea) would be to add a flag similar to the --interactive flags of rm/mv, where we would ask for confirmation before running each command. Or maybe show a list of to-be-executed commands first and then get the user confirmation.

@savente93
Copy link
Author

Thanks for the kind words, that really means a lot to me :) I think all the points made are valid. Shall I close this PR and open an issue to discuss this further, or would you like to continue the conversation here?

@tavianator
Copy link
Collaborator

A way to potentially solve this (although I'm not convinced this is a good idea) would be to add a flag similar to the --interactive flags of rm/mv, where we would ask for confirmation before running each command.

find supports this as -ok/-okdir

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.

4 participants