Skip to content

Commit

Permalink
Merge pull request #85 from auth0/feature/users-interactivity
Browse files Browse the repository at this point in the history
[A0CLI-38] feat: add prompts to users
  • Loading branch information
Widcket authored Jan 27, 2021
2 parents 895677f + a448af9 commit befc44c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
26 changes: 14 additions & 12 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ func enableRuleCmd(cli *cli) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
if shouldPrompt(cmd, ruleName) {
input := prompt.TextInput(
ruleName, "Name:",
"Name of the rule.",
true)
input := prompt.TextInput(ruleName, "Name:", "Name of the rule.", true)

if err := prompt.AskOne(input, &flags); err != nil {
return err
Expand Down Expand Up @@ -141,10 +138,7 @@ func disableRuleCmd(cli *cli) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
if shouldPrompt(cmd, ruleName) {
input := prompt.TextInput(
ruleName, "Name:",
"Name of the rule.",
true)
input := prompt.TextInput(ruleName, "Name:", "Name of the rule.", true)

if err := prompt.AskOne(input, &flags); err != nil {
return err
Expand Down Expand Up @@ -306,8 +300,16 @@ func deleteRulesCmd(cli *cli) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if shouldPrompt(cmd, ruleID) {
input := prompt.TextInput(ruleID, "Id:", "Id of the rule.", true)
if shouldPrompt(cmd, ruleID) && flags.Name == "" {
input := prompt.TextInput(ruleID, "Id:", "Id of the rule to delete.", false)

if err := prompt.AskOne(input, &flags); err != nil {
return err
}
}

if shouldPrompt(cmd, ruleName) && flags.ID == "" {
input := prompt.TextInput(ruleName, "Name:", "Name of the rule to delete.", false)

if err := prompt.AskOne(input, &flags); err != nil {
return err
Expand Down Expand Up @@ -358,8 +360,8 @@ func deleteRulesCmd(cli *cli) *cobra.Command {
},
}

cmd.Flags().StringVarP(&flags.ID, ruleID, "i", "", "ID of the rule to delete (required)")
cmd.Flags().StringVarP(&flags.Name, ruleName, "n", "", "Name of the rule to delete")
cmd.Flags().StringVarP(&flags.ID, ruleID, "i", "", "ID of the rule to delete.")
cmd.Flags().StringVarP(&flags.Name, ruleName, "n", "", "Name of the rule to delete.")

return cmd
}
Expand Down
59 changes: 37 additions & 22 deletions internal/cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,76 @@ import (
"errors"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/prompt"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"
)

const (
userID = "id"
userEmail = "email"
)

func usersCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "users",
Short: "manage users.",
}

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(getusersCmd(cli))
cmd.AddCommand(showUserCmd(cli))

return cmd
}

func getusersCmd(cli *cli) *cobra.Command {
func showUserCmd(cli *cli) *cobra.Command {
var flags struct {
id string
email string
fields string
ID string
Email string
Fields string
}

cmd := &cobra.Command{
Use: "get",
Short: "Get a user's details",
Long: `$ auth0 users get
Use: "show",
Short: "Show a user's details",
Long: `$ auth0 users show --id id --email email
Get a user
`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
userID, err := cmd.LocalFlags().GetString("id")
if err != nil {
return err
if shouldPrompt(cmd, userID) && flags.Email == "" {
input := prompt.TextInput(userID, "Id:", "ID of the user to show.", false)

if err := prompt.AskOne(input, &flags); err != nil {
return err
}
}

userEmail, err := cmd.LocalFlags().GetString("email")
if err != nil {
return err
if shouldPrompt(cmd, userEmail) && flags.ID == "" {
input := prompt.TextInput(userEmail, "Email:", "Email of the user to show.", false)

if err := prompt.AskOne(input, &flags); err != nil {
return err
}
}

if userID == "" && userEmail == "" {
if flags.ID == "" && flags.Email == "" {
return errors.New("User id or email flag must be specified")
}

if userID != "" && userEmail != "" {
if flags.ID != "" && flags.Email != "" {
return errors.New("User id and email flags cannot be combined")
}

var users []*management.User
var user *management.User

if userID != "" {
if flags.ID != "" {
err := ansi.Spinner("Getting user", func() error {
var err error
user, err = cli.api.User.Read(flags.id)
user, err = cli.api.User.Read(flags.ID)
return err
})

Expand All @@ -72,10 +87,10 @@ Get a user
return nil
}

if userEmail != "" {
if flags.Email != "" {
err := ansi.Spinner("Getting user(s)", func() error {
var err error
users, err = cli.api.User.ListByEmail(userEmail)
users, err = cli.api.User.ListByEmail(flags.Email)
return err
})

Expand All @@ -91,8 +106,8 @@ Get a user
},
}

cmd.Flags().StringVarP(&flags.id, "id", "i", "", "User ID of user to get.")
cmd.Flags().StringVarP(&flags.email, "email", "e", "", "Email of user to get.")
cmd.Flags().StringVarP(&flags.ID, userID, "i", "", "ID of the user to show.")
cmd.Flags().StringVarP(&flags.Email, userEmail, "e", "", "Email of the user to show.")

return cmd
}

0 comments on commit befc44c

Please sign in to comment.