Creating and Distributing CLI Applications with Golang and GitHub

Creating and Distributing CLI Applications with Golang and GitHub

Learn how to create a new release of your CLI application

ยท

6 min read

Introduction ๐Ÿƒโ€โ™‚๏ธ

Command-line interface (CLI) applications are a mainstay of the development world, favored for their simplicity, low overhead, and ease of integration with other software. These applications accept input directly from the terminal and are especially popular among developers who need to automate tasks, manipulate files, and configure systems.

One of the languages that have recently risen to prominence in CLI applications development is Go (or Golang), a statically-typed compiled language known for its simplicity, efficiency, and strong support for concurrent programming.

In this guide, we will walk you through creating a CLI application with Golang, specifically using the Cobra library, and then distributing it via GitHub releases. This guide assumes that you are already familiar with Go and the basic usage of Git and GitHub.

Creating a CLI Application with Cobra ๐Ÿ

Cobra is a library that provides a simple interface for creating powerful modern CLI applications. It's used in many popular projects, including Kubernetes and Hugo.

  1. Install Cobra: First, we need to install the Cobra library. Open your terminal and run the following command:

     go get -u github.com/spf13/cobra/cobra
    
  2. Initialize Your Application: Next, initialize your Cobra application by running:

     cobra init --pkg-name myapp
    

    Replace 'myapp' with the name of your application. This will create a new directory with the same name as your application and an initial file structure.

  3. Add Commands: With your application initialized, you can start adding commands. Let's add a 'hello' command:

     cobra add hello
    

    This will create a new 'hello' command. You can now edit this command in the hello.go file.

    After editing the hello.go file, you can run your application with:

     go run main.go
    

Now that you've created your CLI application, the next step is to distribute it to users. We will compile the application to a binary, which can be run on any system with the same operating system (OS) and architecture.

Compiling Your Application ๐Ÿ’ป

Go supports cross-compilation, which means you can create binaries for different operating systems on a single system. To compile your application, you'll need to specify the target OS and architecture.

For example, to build for Linux:

GOOS=linux GOARCH=amd64 go build -o myapp main.go

To build for Windows:

GOOS=windows GOARCH=amd64 go build -o myapp.exe main.go

Here, GOOS and GOARCH are environment variables that specify the target OS and architecture, respectively. The -o flag is followed by the output file name.

After running these commands, you'll have a binary (or .exe file for Windows) that can be run on the respective OS.

Making the Binary Accessible ๐Ÿ“‚

Once you've built your binary, you want it to be easily accessible from any command line prompt. For this, the binary needs to be located in a directory that's included in the system's PATH.

On Unix-like systems, you can move the binary to /usr/local/bin, which is included in the PATH by default:

mv myapp /usr/local/bin

On Windows, you might need to add the directory containing your binary to the PATH. Alternatively, you can move the .exe file to a directory that's already in the PATH.

With these changes, your users can run the myapp command directly from any command line prompt, without needing to specify the full path to the binary.

On Windows, you might need to add the directory containing your binary to the PATH. Alternatively, you can move the .exe file to a directory that's already in the PATH.

With these changes, your users can run the myapp command directly from any command line prompt, without needing to specify the full path to the binary.

Tagging a Release โŒš

In Git, tags are used to mark specific points in your project's history as being important, typically to indicate different versions of your project. GitHub uses these tags to associate commits with specific releases.

  1. Commit your changes: Before you can tag a release, make sure all changes are committed to your local repository:

     git add .
     git commit -m "Preparing for v1.0.0 release"
    
  2. Create a tag: Once your changes are committed, you can create a tag on that commit. Replace v1.0.0 with your desired tag name:

     git tag -a v1.0.0 -m "First stable release"
    
  3. Push the tag to your remote repository: Tags are not automatically pushed to the remote repository when you push your commits. You must explicitly push your tags with the git push --tags command:

     git push origin master --tags
    

Creating a Release on GitHub ๐Ÿ”ฅ

After you've pushed your tags to the remote repository, you can create a new release based on the tag through the GitHub website:

  1. Navigate to the "Releases" tab of your repository and click "Create a new release".

  2. In the "Tag version" field, select the tag that you've just pushed to GitHub.

  3. Write a title and description for your release, then attach your binary files for each platform.

  4. Click "Publish release" to make your binaries available for download.

With your release now published on GitHub, users can download the appropriate binary, place it in their system's PATH, and begin using your application. They can access any future updates by simply downloading the new binary from each subsequent release.

Distributing Your Binary ๐Ÿ“ค

With your binaries ready, it's time to distribute them. While there are many ways to distribute software, one of the easiest and most popular methods is through GitHub releases.

  1. Push Your Code to GitHub: If you haven't already, create a new repository on GitHub and push your code to it.

  2. Create a New Release: In your repository, go to the "Releases" tab, and click "Create a new release".

  3. Upload Your Binary: On the new release page, you can write your release notes and upload your binaries. To upload a binary, simply drag and drop the file into the provided field, or click the "choose a file" button to open a file picker.

  4. Publish Your Release: Once you've written your release notes and uploaded your binaries, click "Publish Release" to make your binaries available to the public.

Now, users can go to your GitHub release page, download the binary for their OS, and run it on their system.

Proctl release

Conclusion ๐Ÿค—

In this guide, we've shown you how to create a CLI application with Go and Cobra, compile it to a binary, and distribute it using GitHub releases. While we've covered a lot of ground, there's still much more to learn about building and distributing software.

CLI applications are a powerful tool in any developer's arsenal, and Go is an excellent choice for creating them. With a little practice, you'll be creating and distributing your own applications in no time.

So, why wait? Start creating your CLI application today!

That's it for now.

You can Buy Me a Coffee if you want to and please don't forget to follow me on YouTube, Twitter, and LinkedIn also.

If you have any questions or would like to share your own experiences, feel free to leave a comment below. I'm here to support and engage with you.

Happy coding!

ย