1 Star 0 Fork 0

ryancartoon/sensu-go

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
list.go 2.05 KB
Copy Edit Raw Blame History
package user
import (
"errors"
"io"
"strings"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/cli/elements/globals"
"github.com/sensu/sensu-go/cli/elements/table"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
)
// ListCommand defines new list events command
func ListCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list users",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
_ = cmd.Help()
return errors.New("invalid argument(s) received")
}
opts, err := helpers.ListOptionsFromFlags(cmd.Flags())
if err != nil {
return err
}
// Fetch users from API
results, err := cli.Client.ListUsers(&opts)
if err != nil {
return err
}
resources := []types.Resource{}
for i := range results {
resources = append(resources, &results[i])
}
// Print the results based on the user preferences
return helpers.Print(cmd, cli.Config.Format(), printToTable, resources, results)
},
}
helpers.AddFormatFlag(cmd.Flags())
helpers.AddFieldSelectorFlag(cmd.Flags())
helpers.AddLabelSelectorFlag(cmd.Flags())
helpers.AddChunkSizeFlag(cmd.Flags())
return cmd
}
func printToTable(results interface{}, writer io.Writer) {
table := table.New([]*table.Column{
{
Title: "Username",
ColumnStyle: table.PrimaryTextStyle,
CellTransformer: func(data interface{}) string {
user, ok := data.(types.User)
if !ok {
return cli.TypeError
}
return user.Username
},
},
{
Title: "Groups",
CellTransformer: func(data interface{}) string {
user, ok := data.(types.User)
if !ok {
return cli.TypeError
}
return strings.Join(user.Groups, ",")
},
},
{
Title: "Enabled",
CellTransformer: func(data interface{}) string {
user, ok := data.(types.User)
if !ok {
return cli.TypeError
}
return globals.BooleanStyleP(!user.Disabled)
},
},
})
table.Render(writer, results)
}
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

Search