{"id":2266,"date":"2022-01-03T22:46:44","date_gmt":"2022-01-03T20:46:44","guid":{"rendered":"https:\/\/guven.atbakan.com\/blog\/?p=2266"},"modified":"2024-04-14T00:24:37","modified_gmt":"2024-04-13T21:24:37","slug":"writing-command-line-applications-tips-and-tricks-dry-run","status":"publish","type":"post","link":"https:\/\/guven.atbakan.com\/blog\/writing-command-line-applications-tips-and-tricks-dry-run\/","title":{"rendered":"Writing Command Line Applications &#8211; Tips &#038; Tricks &#8211; dry-run"},"content":{"rendered":"<p>Testing command line applications is not easy. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Dry_run_(testing)\">Dry Run<\/a>, is a testing process usually used for command line applications. Also it allows you to simulate what will happen when you run the command.<\/p>\n<blockquote>\n<p>It&#8217;s kinda like running SELECT statement on SQL before running DELETE\/UPDATE statements and see which records will be affected.<\/p>\n<\/blockquote>\n<p>I&#8217;m going to share an example use case. Assume that you have a command which deletes unused files from your S3 bucket.<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\n$files = File::all();\n\n$this-&gt;output-&gt;writeln(&#039;There are &#039;.count($files).&#039; file to be checked&#039;);\n\nforeach ($files as $file) {\n    if (!$this-&gt;fileService-&gt;isFileInUse($file)) {\n        $this-&gt;fileService-&gt;deleteFile($file);\n        $this-&gt;output-&gt;writeln(&#039;File has been deleted from storage: file_id_&#039;.$file-&gt;id);\n    }\n}<\/code><\/pre>\n<p>This command is a bit risky! If there is an unexpected behavior on <code>isFileInUse<\/code> method, then we can delete wrong files. I prefer to simulate data before actually running these kind of commands.<\/p>\n<p>So <code>--dry-run<\/code> option is with us!<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\n\/\/ In Laravel, we use `option` method to get an option.\n$dryRun = $this-&gt;option(&#039;dry-run&#039;);\n\n$files = File::all();\n\n$this-&gt;output-&gt;writeln(&#039;There are &#039;.count($files).&#039; file to be checked&#039;);\n\nforeach ($files as $file) {\n    if (!$this-&gt;fileService-&gt;isFileInUse($file)) {\n        if ($dryRun) {\n            $this-&gt;output-&gt;writeln(&#039;File will be deleted from storage: file_id_&#039;.$file-&gt;id. &#039; URL: &#039;.$file-&gt;url);\n            continue;\n        }\n\n        $this-&gt;fileService-&gt;deleteFile($file);\n        $this-&gt;output-&gt;writeln(&#039;File has been deleted from storage: file_id_&#039;.$file-&gt;id);\n    }\n}<\/code><\/pre>\n<pre><code>php artisan s3:delete-unused-files --dry-run<\/code><\/pre>\n<p>Simple! We&#8217;re just passing <code>--dry-run<\/code> option to command line and it&#8217;s telling us which files will be deleted. In the example, I&#8217;ve also added the url to output. You can add whatever you want to make spot-check. If everything seems good, you can run actual command without dry-run.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Testing command line applications is not easy. Dry Run, is a testing process usually used for command line applications. Also it allows you to simulate what will happen when you run the command. It&#8217;s kinda like running SELECT statement on SQL before running DELETE\/UPDATE statements and see which records will be affected. I&#8217;m going to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[763],"tags":[765,767],"class_list":["post-2266","post","type-post","status-publish","format-standard","hentry","category-development-and-internet","tag-programming-en","tag-writing-command-line-applications-en"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/posts\/2266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/comments?post=2266"}],"version-history":[{"count":11,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/posts\/2266\/revisions"}],"predecessor-version":[{"id":2393,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/posts\/2266\/revisions\/2393"}],"wp:attachment":[{"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/media?parent=2266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/categories?post=2266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/tags?post=2266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}