Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Kubectl like output format for my cobra project

I am new to golang and trying to develop a commandline tool using cobra. I would like to provide flags like kubectl offers to it’s users. As a first step i would like to implement kubectl standard format like this

kubectl get pods -n mpo
NAME                                                    READY   STATUS      RESTARTS   AGE
my-deployment-5588bf7844-n2nw7                        1/1     Running     0          3d
my-deployment-5588bf7844-qcpsl                        1/1     Running     0          3d

Could please direct me to a simple example project (kubectl github project is too difficult for me to understand) or code where i could understand how to implement this output format.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can format strings in Go with padding on either side. See this post for example.

Below, I format all strings with right padding. The first column gets 40 char padding as the pod name might be long. The other columns get only 10 chars padding.

func main() {
    fmt.Printf("%-40s %-10s %-10s\n", "NAME", "READY", "STATUS")
    fmt.Printf("%-40s %-10s %-10s\n", "my-deployment-5588bf7844-n2nw7", " 1/1", "Running")
    fmt.Printf("%-40s %-10s %-10s\n", "my-deployment-5588bf7844-n2nw7", " 1/1", "Running")
}

https://play.golang.com/p/ueoqfqUtKNJ

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading