Using CLI Applications to Increase Efficiency in Work

Using CLI Applications to Increase Efficiency in Work

Daily short news for you
  • How I wish I had discovered this repository earlier. github/opensource.guide is a place that guides everyone on everything about Open Source. From how to contribute code, how to start your own open-source project, to the knowledge that anyone should know when stepping into this field 🤓

    Especially, this content is directly from Github.

    » Read more
  • Just the other day, I mentioned dokploy.com and today I came across coolify.io - another open-source project that can replace Heroku/Netlify/Vercel.

    From what I've read, Coolify operates based on Docker deployment, which allows it to run most applications.

    Coolify offers an interface and features that make application deployment simpler and easier.

    Could this be the trend for application deployment in the future? 🤔

    » Read more
  • One of the things I really like about command lines is their 'pipeline' nature. You can imagine each command as a pipe; when connected together, they create a flow of data. The output of one pipe becomes the input of another... and so on.

    In terms of application, there are many examples; you can refer to the article Practical Data Processing Using Commands on MTTQVN Statement File. By combining commands, we turn them into powerful data analysis tools.

    Recently, I combined the wrangler command with jq to make it easier to view logs from the worker. wrangler is Cloudflare's command line interface (CLI) that integrates many features. One of them helps us view logs from Worker using the command:

    $ wrangler tail --config /path/to/wrangler.toml --format json

    However, the logs from the above command contain a lot of extraneous information, spilling over the screen, while we only want to see a few important fields. So, what should we do?

    Let’s combine it with jq. jq is a very powerful JSON processing command. It makes working with JSON data in the terminal much easier. Therefore, to filter information from the logs, it’s quite simple:

    $ wrangler tail --config /path/to/wrangler.toml --format json | jq '{method: .event.request.method, url: .event.request.url, logs }'

    The above command returns structured JSON logs consisting of only 3 fields: method, url, and logs 🔥

    » 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

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? 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ống11 months 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 🙌
Scroll or click to go to the next page