DEV Community

Cover image for Push-based CLI Workflow on MacOS
 LIU ZHE YOU
LIU ZHE YOU

Posted on • Originally published at blog.zhu424.dev

Push-based CLI Workflow on MacOS

Push-based vs. Pull-based in System Design

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: Push-based and Pull-based.

Push-based vs. Pull-based in System Design

The upper part represents Pull-based, while the lower part represents Push-based.

It is clear that:

  • Pull-based requires the client to continuously query the server for the current state.
  • Push-based has the server proactively notify the client only when the state changes.

Context Switching in CLI Workflows

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.

In these situations, you might switch to other tasks but still need to repeatedly check the command's execution status, resulting in unnecessary context switching.

Reducing Context Switching with Push-based Notifications

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.

That’s why there should be a simple script to actively notify you once a CLI command completes.

dotfiles/.zshrc at ee0772 · jason810496/dotfiles

notify () {
        if [[ $? -eq 0 ]]
        then
                osascript -e 'display notification "✅ Success!" with title "Command Completed"'
                afplay /System/Library/Sounds/Glass.aiff
        else
                osascript -e 'display notification "❌ Failed!" with title "Command Failed"'
                afplay /System/Library/Sounds/Basso.aiff
        fi
}
Enter fullscreen mode Exit fullscreen mode

Currently, I add the notify script to my .zshrc (which uses macOS’s osascript to invoke display notification).

How to Use

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

make build ; notify "Build done"
Enter fullscreen mode Exit fullscreen mode

After completion, the following notifications will appear in the top right corner:

On success:

notify-success

On failure:

notify-failed

Conclusion

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!

I often use push-based notifications when contributing to Apache Airflow. For instance, when rebuilding the Kubernetes Docker image:

breeze k8s build-k8s-image --rebuild-base-image ; notify
Enter fullscreen mode Exit fullscreen mode

For long-running commands like these, you can focus on other work without constantly monitoring the terminal.

For example, while writing this article, I was actually building the k8s image xD

Top comments (0)