赞美 – 预演
In praise of –dry-run

原始链接: https://henrikwarne.com/2026/01/31/in-praise-of-dry-run/

## `--dry-run` 选项的价值 在开发一个生成、压缩、上传和通知的报告应用程序时,作者借鉴Subversion等工具,实现了一个 `--dry-run` 选项。这个标志模拟应用程序的流程——列出哪些报告*将会*生成,文件*将会*移动/上传等等——而实际上不进行任何更改。 作者发现这个功能非常有用,每天都用它来快速检查可访问性、配置和预期状态,*在*运行完整流程之前进行验证。它也加快了测试速度;报告计划的更改可以立即验证,而无需等待报告生成。 虽然承认它为代码增加了一些条件逻辑,但快速反馈和安全测试的好处超过了轻微的复杂性。作者总结说,`--dry-run` 对于执行更改的命令行应用程序尤其有用,并建议在开发早期考虑实现它。

这个Hacker News讨论赞扬了在命令行界面(CLI)工具中使用`--dry-run`标志。用户认为它们对本地开发和生产部署都非常有价值。 核心思想是设计应用程序,使其能够轻松地在实际执行操作(如写入数据库)和仅仅记录*会发生什么*之间切换。这通常通过Go和Rust等语言中的选项/构建器模式来实现。 虽然高测试覆盖率很重要,但dry run提供了一层额外的信心,尤其是在引入修改数据的特性时。一位用户甚至使用`--really`标志,默认使工具变为只读模式以提高安全性。最终,dry run有助于发现使用生产数据和流量时出现的意外边缘情况,从而可能防止代价高昂的错误。
相关文章

原文

For the last few months, I have been developing a new reporting application. Early on, I decided to add a –dry-run option to the run command. This turned out to be quite useful – I have used it many times a day while developing and testing the application.

Background

The application will generate a set of reports every weekday. It has a loop that checks periodically if it is time to generate new reports. If so, it will read data from a database, apply some logic to create the reports, zip the reports, upload them to an sftp server, check for error responses on the sftp server, parse the error responses, and send out notification mails. The files (the generated reports, and the downloaded feedback files) are moved to different directories depending on the step in the process. A simple and straightforward application.

Early in the development process, when testing the incomplete application, I remembered that Subversion (the version control system after CVS, before Git) had a –dry-run option. Other linux commands have this option too. If a command is run with the argument –dry-run, the output will print what will happen when the command is run, but no changes will be made. This lets the user see what will happen if the command is run without the –dry-run argument.

I remembered how helpful that was, so I decided to add it to my command as well. When I run the command with –dry-run, it prints out the steps that will be taken in each phase: which reports that will be generated (and which will not be), which files will be zipped and moved, which files will be uploaded to the sftp server, and which files will be downloaded from it (it logs on and lists the files).

Looking back at the project, I realized that I ended up using the –dry-run option pretty much every day.

Benefits

I am surprised how useful I found it to be. I often used it as a check before getting started. Since I know –dry-run will not change anything, it is safe to run without thinking. I can immediately see that everything is accessible, that the configuration is correct, and that the state is as expected. It is a quick and easy sanity check.

I also used it quite a bit when testing the complete system. For example, if I changed a date in the report state file (the date for the last successful report of a given type), I could immediately see from the output whether it would now be generated or not. Without –dry-run, the actual report would also be generated, which takes some time. So I can test the behavior, and receive very quick feedback.

Downside

The downside is that the dryRun-flag pollutes the code a bit. In all the major phases, I need to check if the flag is set, and only print the action that will be taken, but not actually doing it. However, this doesn’t go very deep. For example, none of the code that actually generates the report needs to check it. I only need to check if that code should be invoked in the first place.

Conclusion

The type of application I have been writing is ideal for –dry-run. It is invoked by a command, and it may create some changes, for example generating new reports. More reactive applications (that wait for messages before acting) don’t seem to be a good fit.

I added –dry-run on a whim early on in the project. I was surprised at how useful I found it to be. Adding it early was also good, since I got the benefit of it while developing more functionality.

The –dry-run flag is not for every situation, but when it fits, it can be quite useful.

联系我们 contact @ memedata.com