1 Star 0 Fork 0

ryancartoon/sensu-go

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
print.go 2.03 KB
一键复制 编辑 原始数据 按行查看 历史
package helpers
import (
"fmt"
"io"
"github.com/sensu/sensu-go/cli/client/config"
"github.com/sensu/sensu-go/cli/commands/flags"
"github.com/sensu/sensu-go/cli/elements/list"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
)
type printTableFunc func(interface{}, io.Writer)
// Print displays
func Print(cmd *cobra.Command, format string, printTable printTableFunc, objects []types.Resource, v interface{}) error {
if f := GetChangedStringValueFlag(flags.Format, cmd.Flags()); f != "" {
format = f
}
switch format {
case config.FormatJSON:
return PrintJSON(v, cmd.OutOrStdout())
case config.FormatWrappedJSON:
if objects == nil {
return PrintJSON(v, cmd.OutOrStdout())
}
return PrintWrappedJSONList(objects, cmd.OutOrStdout())
case config.FormatYAML:
if objects == nil {
return PrintYAML(v, cmd.OutOrStdout())
}
return PrintYAML(objects, cmd.OutOrStdout())
default:
printTable(v, cmd.OutOrStdout())
}
return nil
}
// PrintFormatted prints the provided interface in the specified format.
// flag overrides the cli config format if set
func PrintFormatted(flag string, format string, v interface{}, w io.Writer, printToList func(interface{}, io.Writer) error) error {
if flag != "" {
format = flag
}
switch format {
case config.FormatJSON:
return PrintJSON(v, w)
case config.FormatWrappedJSON:
r, ok := v.(types.Resource)
if !ok {
return fmt.Errorf("%t is not a Resource", v)
}
return PrintWrappedJSON(r, w)
case config.FormatYAML:
return PrintYAML(v, w)
default:
return printToList(v, w)
}
}
// PrintTitle prints a title for tabular format only.
// Flag overrides the cli config format, if set.
func PrintTitle(flag string, format string, title string, w io.Writer) error {
if flag != "" {
format = flag
}
// checking the formats exclusively to cover invalid formats
// that get defaulted to tabular
if format != config.FormatJSON && format != config.FormatWrappedJSON && format != config.FormatYAML {
cfg := &list.Config{
Title: title,
}
return list.Print(w, cfg)
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ryancartoon/sensu-go.git
git@gitee.com:ryancartoon/sensu-go.git
ryancartoon
sensu-go
sensu-go
v5.10.1

搜索帮助