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
create.go 2.63 KB
Copy Edit Raw Blame History
Cyril Cressent authored 2018-11-24 02:19 +08:00 . Complete redesign of RBAC (#2338)
package user
import (
"errors"
"fmt"
"github.com/AlecAivazis/survey"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/commands/flags"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type createOpts struct {
Username string `survey:"username"`
Password string `survey:"password"`
Groups string `survey:"group"`
}
// CreateCommand adds command that allows user to create new users
func CreateCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "create [NAME]",
Short: "create new users",
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
isInteractive, _ := cmd.Flags().GetBool(flags.Interactive)
if !isInteractive {
// Mark flags are required for bash-completions
_ = cmd.MarkFlagRequired("password")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
_ = cmd.Help()
return errors.New("invalid argument(s) received")
}
isInteractive, _ := cmd.Flags().GetBool(flags.Interactive)
opts := &createOpts{}
if len(args) > 0 {
opts.Username = args[0]
}
if isInteractive {
if err := opts.administerQuestionnaire(); err != nil {
return err
}
} else {
opts.withFlags(cmd.Flags())
}
user := opts.toUser()
if err := user.Validate(); err != nil {
if !isInteractive {
cmd.SilenceUsage = false
}
return err
}
err := cli.Client.CreateUser(user)
if err != nil {
return err
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), "Created")
return err
},
}
_ = cmd.Flags().StringP("password", "p", "", "Password")
_ = cmd.Flags().StringP("groups", "g", "", "Comma separated list of the groups to assign")
helpers.AddInteractiveFlag(cmd.Flags())
return cmd
}
func (opts *createOpts) withFlags(flags *pflag.FlagSet) {
opts.Password, _ = flags.GetString("password")
opts.Groups, _ = flags.GetString("groups")
}
func (opts *createOpts) administerQuestionnaire() error {
var qs = []*survey.Question{
{
Name: "username",
Prompt: &survey.Input{
Message: "Username:",
Default: opts.Username,
},
Validate: survey.Required,
},
{
Name: "password",
Prompt: &survey.Password{
Message: "Password:",
},
Validate: survey.Required,
},
{
Name: "groups",
Prompt: &survey.Input{
Message: "Groups:",
},
},
}
return survey.Ask(qs, opts)
}
func (opts *createOpts) toUser() *types.User {
groups := helpers.SafeSplitCSV(opts.Groups)
return &types.User{
Username: opts.Username,
Password: opts.Password,
Groups: groups,
}
}
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