<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community:  LIU ZHE YOU</title>
    <description>The latest articles on DEV Community by  LIU ZHE YOU (@zhu424).</description>
    <link>https://dev.to/zhu424</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2921715%2F59359c48-60b1-4839-b0ce-b399a879ed27.jpeg</url>
      <title>DEV Community:  LIU ZHE YOU</title>
      <link>https://dev.to/zhu424</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhu424"/>
    <language>en</language>
    <item>
      <title>Push-based CLI Workflow on MacOS</title>
      <dc:creator> LIU ZHE YOU</dc:creator>
      <pubDate>Sat, 08 Mar 2025 03:57:53 +0000</pubDate>
      <link>https://dev.to/zhu424/push-based-cli-workflow-on-macos-1g76</link>
      <guid>https://dev.to/zhu424/push-based-cli-workflow-on-macos-1g76</guid>
      <description>&lt;h2&gt;
  
  
  Push-based vs. Pull-based in System Design
&lt;/h2&gt;

&lt;p&gt;In system design, when you need to monitor the state of data—such as whether an entity has been updated or if a long-running job has completed—there are generally two models: &lt;strong&gt;Push-based&lt;/strong&gt; and &lt;strong&gt;Pull-based&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi8c6xogslrnswz9d8c3j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi8c6xogslrnswz9d8c3j.png" alt="Push-based vs. Pull-based in System Design" width="800" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The upper part represents Pull-based, while the lower part represents Push-based.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is clear that:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pull-based&lt;/strong&gt; requires the client to continuously query the server for the current state.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Push-based&lt;/strong&gt; has the server proactively notify the client &lt;strong&gt;only when the state changes&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Context Switching in CLI Workflows
&lt;/h2&gt;

&lt;p&gt;When using the CLI, you often run commands that take a long time to execute—for instance, a test that runs for several minutes or a build command.&lt;/p&gt;

&lt;p&gt;In these situations, you might switch to other tasks &lt;strong&gt;but still need to repeatedly check the command's execution status&lt;/strong&gt;, resulting in unnecessary context switching.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reducing Context Switching with Push-based Notifications
&lt;/h2&gt;

&lt;p&gt;Imagine if the CLI could actively notify you when a command finishes. Just like a push-based system, you wouldn’t need to repeatedly check the command’s status.&lt;/p&gt;

&lt;p&gt;That’s why there should be a simple script to actively notify you once a CLI command completes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/jason810496/dotfiles/blob/ee0772e86bf881878b1e3aa3f5b3df6ad36b40f2/zsh/.zshrc#L144-L156" rel="noopener noreferrer"&gt;dotfiles/.zshrc at ee0772 · jason810496/dotfiles&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;notify &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt; &lt;span class="nt"&gt;-eq&lt;/span&gt; 0 &lt;span class="o"&gt;]]&lt;/span&gt;
        &lt;span class="k"&gt;then
                &lt;/span&gt;osascript &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'display notification "✅ Success!" with title "Command Completed"'&lt;/span&gt;
                afplay /System/Library/Sounds/Glass.aiff
        &lt;span class="k"&gt;else
                &lt;/span&gt;osascript &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'display notification "❌ Failed!" with title "Command Failed"'&lt;/span&gt;
                afplay /System/Library/Sounds/Basso.aiff
        &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Currently, I add the &lt;code&gt;notify&lt;/code&gt; script to my &lt;code&gt;.zshrc&lt;/code&gt; (which uses macOS’s &lt;code&gt;osascript&lt;/code&gt; to invoke &lt;code&gt;display notification&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use
&lt;/h2&gt;

&lt;p&gt;For example, when building a project, you might chain commands using &lt;code&gt;;&lt;/code&gt; rather than &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; or &lt;code&gt;||&lt;/code&gt; so that you can capture the build result (i.e., to know whether the build succeeded or failed):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make build &lt;span class="p"&gt;;&lt;/span&gt; notify &lt;span class="s2"&gt;"Build done"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After completion, the following notifications will appear in the top right corner:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;On success:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmu7clx0bfjbaelk237z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmu7clx0bfjbaelk237z.png" alt="notify-success" width="800" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On failure:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb3ttaskcrbe27zucsoy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffb3ttaskcrbe27zucsoy.png" alt="notify-failed" width="800" height="337"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By using push-based notifications, you can reduce context switching in your CLI workflows, allowing you to concentrate more on other tasks. If you have similar needs, consider implementing a solution based on the script above!&lt;/p&gt;

&lt;p&gt;I often use push-based notifications when contributing to Apache Airflow. For instance, when rebuilding the Kubernetes Docker image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;breeze k8s build-k8s-image &lt;span class="nt"&gt;--rebuild-base-image&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; notify
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For long-running commands like these, you can focus on other work without constantly monitoring the terminal.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example, while writing this article, I was actually building the k8s image xD&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>cli</category>
      <category>productivity</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
