1 Star 0 Fork 0

ryancartoon/sensu-go

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
info.go 2.16 KB
一键复制 编辑 原始数据 按行查看 历史
Cyril Cressent 提交于 2018-11-24 02:19 +08:00 . Complete redesign of RBAC (#2338)
package role
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/table"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
)
// InfoCommand defines new command to display detailed information about a role
func InfoCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "info [NAME]",
Aliases: []string{"list-rules"}, // backward compatibility
Short: "show detailed information about a role",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
_ = cmd.Help()
return errors.New("a role name is required")
}
// Fetch roles from API
r, err := cli.Client.FetchRole(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(), printRulesToTable)
},
}
helpers.AddFormatFlag(cmd.Flags())
return cmd
}
func printRulesToTable(v interface{}, io io.Writer) error {
queryResults, ok := v.(*types.Role)
if !ok {
return fmt.Errorf("%t is not a role", v)
}
table := table.New([]*table.Column{
{
Title: "Namespace",
ColumnStyle: table.PrimaryTextStyle,
CellTransformer: func(data interface{}) string {
return queryResults.Namespace
},
},
{
Title: "Verbs",
CellTransformer: func(data interface{}) string {
rule, ok := data.(types.Rule)
if !ok {
return cli.TypeError
}
return strings.Join(rule.Verbs, ",")
},
},
{
Title: "Resources",
CellTransformer: func(data interface{}) string {
rule, ok := data.(types.Rule)
if !ok {
return cli.TypeError
}
return strings.Join(rule.Resources, ",")
},
},
{
Title: "Resource Names",
CellTransformer: func(data interface{}) string {
rule, ok := data.(types.Rule)
if !ok {
return cli.TypeError
}
return strings.Join(rule.ResourceNames, ",")
},
},
})
table.Render(io, queryResults.Rules)
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

搜索帮助