Using CLI Applications to Increase Efficiency in Work

Using CLI Applications to Increase Efficiency in Work

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » Read more

The Problem

As a programmer, I believe that everyone has thought about leveraging lines of code to automate tasks. For example, a JavaScript code snippet written by a programmer to calculate the total amount of money spent on an e-commerce platform, instead of manually adding up each order in a never-ending list.

Automation brings many benefits, the most obvious of which are time savings and reduced errors in repetitive processes. Moreover, if the code snippet I wrote can be shared with others, it's truly a win-win situation.

I also create such code snippets, but instead of calculating order values, they focus on solving common work-related issues. For instance, a code snippet that synchronizes data between two production servers and the development environment of this blog. Because sometimes it's necessary to test new features on real data for safety before deploying them to the "production" environment.

I usually store these snippets in a convenient location, such as the note-taking application of my operating system, so that they can be synchronized across all my workspaces. If I encounter a problem in the future, I can simply open the application, copy the code, and paste it to run. It's brilliant!

That's just a simple example of how to automate repetitive tasks. A few weeks ago, when I was focused on optimizing images for my blog, the problem became more complicated. Copying and pasting, then modifying the content for each task took more time. I realized that such code snippets are only suitable for low-frequency tasks, such as once a day.

At this point, you may already be imagining how to solve the problem for yourself. I know there are many ways to do it, but personally, I chose to create a CLI application to handle everything.

What is a CLI?

A Command-Line Interface (CLI) is a software mechanism that allows you to interact with your operating system through the keyboard. It is the opposite of a Graphical User Interface (GUI).

The strength of GUI lies in its intuitive navigation, such as clicking icons and images to use software applications. However, GUI is not efficient for system administration tasks, especially in virtual or remote environments.

With CLI, you can enter text commands to configure, navigate, or run programs on any server or computer system. CLI focuses on functionality and speed of use.

CLI is a broad topic, but in this article, I only want to discuss the application of CLI in software applications. In simple terms, CLI applications are command-line applications that we type daily in the Terminal. Commands like cd, ls, pwd, etc. can also be considered as software applications that help us perform specific functions.

When to Create a CLI Application?

Let's go back to the image optimization problem that I wrote an article about, Optimizing Image Display with Blur Placeholder and Lazyload There are many steps involved in creating an image suitable for the blog post, but in general, there are two main actions:

First, reformat the image to webp, adjust the image quality, and crop it to the desired size.

Second, upload all the newly created images to R2 and retrieve all the links to them.

If we apply the "copy-paste" method as mentioned earlier, it becomes cumbersome. Therefore, creating a CLI application in this case is reasonable to centralize management and save typing time.

Imagine, my application is called img, and when I want to create all the optimized images, I just need to run:

$ img all path/to/file

Where path/to/file is the path to the original image. After running, the newly created images will be saved in path/to/new/file. At this point, I add another command upload for the action of uploading images to R2:

$ img upload path/to/new/file

Perfect, everything works. But it's not over yet. One advantage of CLI is that it supports parameters (args) and flags. Based on the flags, we can create more options for richer processing.

For example, if I want to upload the images to R2 immediately after they are created instead of running an additional upload command, I can add code for the --upload flag or its shorthand -u:

$ img all path/to/file --upload
# or
$ img all path/to/file -u

Of course, the above is just a hypothesis. A CLI application can do much more depending on each person's approach. So, feel free to customize commands and flags according to your preferences, but make sure they are clear, easy to remember, and easy to use.

How to Create a Simple CLI Application

CLI applications are not limited to any specific programming language. If you are using Golang, search for libraries that support creating CLI applications in Go. Similarly, in Node.js, oclif can help with this. Besides oclif, there are many other libraries that can help you create CLI applications. Take the time to explore them and choose one with a friendly and easy-to-use syntax. Find more libraries at Command-line utilities | awesome-nodejs Public | Github.

oclif is relatively simple to get started with. Just follow the instructions in Introduction | oclif docs, and you will immediately have your own "command."

$ npx oclif generate mynewcli
? npm package name (mynewcli): mynewcli
$ cd mynewcli
$ ./bin/dev.js hello world
hello world! (./src/commands/hello/world.ts)

oclif supports automatic generation of new commands (CLI Generator), flag/argument parsing, testing, autocomplete, and many other features. For more information, refer to Features | oclif docs.

Conclusion

CLI is a software mechanism that allows you to interact with the operating system. While CLI has a wide scope, in this article, we focused on using CLI applications to create software for our own use.

Previously, I used to store commonly used code snippets and simply "copy-paste" them when needed. Creating a CLI application simplifies this process, saving time and effort.

oclif is a library that helps create CLI applications using Node.js. If you are using a different programming language, search for a suitable library.

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (1)

Leave a comment...
Avatar
Ẩn danh1 year ago
đúng là một bài viết hay tôi muốn đọc thêm nhiều bài viết như thế này ưnax
Reply
Avatar
Xuân Hoài Tống1 year ago
Bạn có thể cho phép blog "gửi thông báo" khi có bài viết mới để không bị bỏ lỡ nhiều bài hấp dẫn trong tương lai 🙌