代码拉取完成,页面将自动刷新
package clusterrolebinding
import (
"errors"
"fmt"
"io"
"strings"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/cli/elements/list"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
)
// InfoCommand defines new command to display detailed information about a
// cluster role binding
func InfoCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "info [NAME]",
Short: "show detailed information about a cluster role binding",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
_ = cmd.Help()
return errors.New("a cluster role binding name is required")
}
// Fetch roles from API
r, err := cli.Client.FetchClusterRoleBinding(args[0])
if err != nil {
return err
}
// Determine the format to use to output the data
flag := helpers.GetChangedStringValueFlag("format", cmd.Flags())
format := cli.Config.Format()
return helpers.PrintFormatted(flag, format, r, cmd.OutOrStdout(), printToList)
},
}
helpers.AddFormatFlag(cmd.Flags())
return cmd
}
func printToList(v interface{}, writer io.Writer) error {
clusterRoleBinding, ok := v.(*types.ClusterRoleBinding)
if !ok {
return fmt.Errorf("%t is not a cluster role binding", v)
}
userNames := []string{}
groupNames := []string{}
for _, subject := range clusterRoleBinding.Subjects {
switch subject.Type {
case types.GroupType:
groupNames = append(groupNames, subject.Name)
case types.UserType:
userNames = append(userNames, subject.Name)
}
}
cfg := &list.Config{
Title: clusterRoleBinding.Name,
Rows: []*list.Row{
{
Label: "Name",
Value: clusterRoleBinding.Name,
},
{
Label: "Cluster Role",
Value: clusterRoleBinding.RoleRef.Name,
},
{
Label: "Subjects",
},
},
}
if len(userNames) > 0 {
cfg.Rows = append(cfg.Rows, &list.Row{
Label: " Users",
Value: strings.Join(userNames, ", "),
})
}
if len(groupNames) > 0 {
cfg.Rows = append(cfg.Rows, &list.Row{
Label: " Groups",
Value: strings.Join(groupNames, ", "),
})
}
return list.Print(writer, cfg)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。