The Go cobra framework is a great tool that can be used to write command-line interfaces in golang. It is used by many different organisations and projects because of how easy and simple it is to use.

Link to the Cobra framework: **https://cobra.dev/**. Cobra framework documentation: **https://pkg.go.dev/github.com/spf13/cobra**

Using it is simple. You just need to run

go get -u github.com/spf13/cobra/cobra

in your terminal. This'll install the cobra library and all the associated dependencies. The -u flag will make sure that the dependencies are taken care of.

In your go code, you'll need to import the library by writing:

import "github.com/spf13/cobra"

Throughout this blog, I'll be walking you through the basics of working with the cobra framework to build a CLI tool that generates deployment files based on the image name, docker image and other options.

Context regarding the Kubernetes deployment file and its various fields is beneficial to have.

Setting up the basics

This here is the GitHub repo link if you intend to follow along: **https://github.com/abs007/kubewrite**

The go cobra framework comes with the library itself and also the cobra generator. The generator can be used for setting up all the required basic files. Setting up your project this way also helps ensure a proper structure. We'll be using the generator for this blog. Link for the generator: **https://github.com/spf13/cobra#usage**

Let's first download the generator:

go install github.com/spf13/cobra-cli@latest

Now, run the command: cobra-cli to see all the flags and sub-commands that you can use.

If it shows that the command can't be found, then you should consider adding the location to your GOBIN location (~/go/bin) to the $PATH variable. More info here: **https://phoenixnap.com/kb/linux-add-to-path**

Let's set up the directory and the project structure now

COPY