Ai
1 Star 0 Fork 0

litian/kontainer-engine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
writer.go 2.58 KB
一键复制 编辑 原始数据 按行查看 历史
Daishan Peng 提交于 2017-11-02 00:45 +08:00 . add env command and rename to kontainer-engine
package utils
import (
"encoding/json"
"html/template"
"os"
"text/tabwriter"
"bytes"
"io"
"strings"
"github.com/urfave/cli"
)
type TableWriter struct {
quite bool
HeaderFormat string
ValueFormat string
err error
headerPrinted bool
Writer *tabwriter.Writer
}
func NewTableWriter(values [][]string, ctx *cli.Context) *TableWriter {
t := &TableWriter{
Writer: tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0),
}
t.HeaderFormat, t.ValueFormat = SimpleFormat(values)
if ctx.Bool("quiet") {
t.HeaderFormat = ""
t.ValueFormat = "{{.ID}}\n"
}
customFormat := ctx.String("format")
if customFormat == "json" {
t.HeaderFormat = ""
t.ValueFormat = "json"
} else if customFormat != "" {
t.ValueFormat = customFormat + "\n"
t.HeaderFormat = ""
}
return t
}
func (t *TableWriter) Err() error {
return t.err
}
func (t *TableWriter) writeHeader() {
if t.HeaderFormat != "" && !t.headerPrinted {
t.headerPrinted = true
t.err = printTemplate(t.Writer, t.HeaderFormat, struct{}{})
if t.err != nil {
return
}
}
}
func (t *TableWriter) Write(obj interface{}) {
if t.err != nil {
return
}
t.writeHeader()
if t.err != nil {
return
}
if t.ValueFormat == "json" {
content, err := json.Marshal(obj)
t.err = err
if t.err != nil {
return
}
_, t.err = t.Writer.Write(append(content, byte('\n')))
} else {
t.err = printTemplate(t.Writer, t.ValueFormat, obj)
}
}
func (t *TableWriter) Close() error {
if t.err != nil {
return t.err
}
t.writeHeader()
if t.err != nil {
return t.err
}
return t.Writer.Flush()
}
func SimpleFormat(values [][]string) (string, string) {
headerBuffer := bytes.Buffer{}
valueBuffer := bytes.Buffer{}
for _, v := range values {
appendTabDelim(&headerBuffer, v[0])
if strings.Contains(v[1], "{{") {
appendTabDelim(&valueBuffer, v[1])
} else {
appendTabDelim(&valueBuffer, "{{."+v[1]+"}}")
}
}
headerBuffer.WriteString("\n")
valueBuffer.WriteString("\n")
return headerBuffer.String(), valueBuffer.String()
}
func appendTabDelim(buf *bytes.Buffer, value string) {
if buf.Len() == 0 {
buf.WriteString(value)
} else {
buf.WriteString("\t")
buf.WriteString(value)
}
}
func printTemplate(out io.Writer, templateContent string, obj interface{}) error {
funcMap := map[string]interface{}{
"json": FormatJSON,
}
tmpl, err := template.New("").Funcs(funcMap).Parse(templateContent)
if err != nil {
return err
}
return tmpl.Execute(out, obj)
}
func FormatJSON(data interface{}) (string, error) {
bytes, err := json.MarshalIndent(data, "", " ")
return string(bytes) + "\n", err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/litian33/kontainer-engine.git
git@gitee.com:litian33/kontainer-engine.git
litian33
kontainer-engine
kontainer-engine
v0.0.4-dev

搜索帮助