我的Cobra项目需要类似于Kubectl的输出格式。

huangapple go评论71阅读模式
英文:

Kubectl like output format for my cobra project

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我是golang的新手,正在尝试使用cobra开发一个命令行工具。我想为用户提供类似kubectl的标志。作为第一步,我想实现kubectl的标准输出格式,像这样:

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

请问你能否指导我一个简单的示例项目(kubectl的GitHub项目对我来说太难理解了),或者提供一些代码,让我能够理解如何实现这种输出格式。

英文:

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.

答案1

得分: 2

你可以在Go语言中使用填充来格式化字符串,可以在字符串的两侧添加填充。请参考这篇文章中的示例。

下面的代码演示了如何对所有字符串进行右侧填充。第一列使用40个字符的填充,因为Pod名称可能很长。其他列只使用10个字符的填充。

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")
}

你也可以使用tabwriter来实现相同的效果,这可能是另一个更好的选择。

// io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
fmt.Fprintln(w, "NAME\tREADY\tSTATUS")
fmt.Fprintln(w, "my-deployment-5588bf7844-n2nw7\t1/1\tRunning")
fmt.Fprintln(w, "my-deployment-5588bf7844-n2nw7\t1/1\tRunning")
w.Flush()
英文:

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.

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/8ZZH5XmqTpR

That said, using tabwriter, may be another, potentially even better option.

// io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint
w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
fmt.Fprintln(w, "NAME\tREADY\tSTATUS")
fmt.Fprintln(w, "my-deployment-5588bf7844-n2nw7\t1/1\tRunning")
fmt.Fprintln(w, "my-deployment-5588bf7844-n2nw7\t1/1\tRunning")
w.Flush()

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

huangapple
  • 本文由 发表于 2022年2月1日 01:21:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/70930177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定