From a448af9b3e650c18a938599166f2ff35671ede5c Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Wed, 27 Jan 2021 18:27:09 -0300 Subject: [PATCH] feat: add prompts to users --- internal/cli/rules.go | 26 ++++++++++--------- internal/cli/users.go | 59 +++++++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/internal/cli/rules.go b/internal/cli/rules.go index a579d0b6c..81546062e 100644 --- a/internal/cli/rules.go +++ b/internal/cli/rules.go @@ -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 @@ -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 @@ -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 @@ -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 } diff --git a/internal/cli/users.go b/internal/cli/users.go index e625876c3..92682cd11 100644 --- a/internal/cli/users.go +++ b/internal/cli/users.go @@ -4,10 +4,16 @@ 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", @@ -15,50 +21,59 @@ func usersCmd(cli *cli) *cobra.Command { } 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 }) @@ -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 }) @@ -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 }