Skip to content

Commit

Permalink
Add interactive prompts to APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket committed Jan 26, 2021
1 parent 738873e commit fdbbb0f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
65 changes: 46 additions & 19 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"github.com/AlecAivazis/survey/v2"
"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/prompt"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -55,8 +56,8 @@ Lists your existing APIs. To create one try:

func createApiCmd(cli *cli) *cobra.Command {
var flags struct {
name string
identifier string
Name string
Identifier string
}

cmd := &cobra.Command{
Expand All @@ -66,10 +67,22 @@ func createApiCmd(cli *cli) *cobra.Command {
auth0 apis create --name myapi --identifier http://my-api
`,
PreRun: func(cmd *cobra.Command, args []string) {
checkFlags(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if !hasFlags(cmd) {
name := prompt.TextInput("name", "Name:", "Name of the API. You can change the API name later in the API settings.", true)
identifier := prompt.TextInput("identifier", "Identifier:", "Identifier of the API. Cannot be changed once set.", true)

if err := prompt.Ask([]*survey.Question {name, identifier}, &flags); err != nil {
return err
}
}

api := &management.ResourceServer{
Name: &flags.name,
Identifier: &flags.identifier,
Name: &flags.Name,
Identifier: &flags.Identifier,
}

err := ansi.Spinner("Creating API", func() error {
Expand All @@ -85,8 +98,8 @@ auth0 apis create --name myapi --identifier http://my-api
},
}

cmd.Flags().StringVarP(&flags.name, "name", "n", "", "Name of the API.")
cmd.Flags().StringVarP(&flags.identifier, "identifier", "i", "", "Identifier of the API.")
cmd.Flags().StringVarP(&flags.Name, "name", "n", "", "Name of the API.")
cmd.Flags().StringVarP(&flags.Identifier, "identifier", "i", "", "Identifier of the API.")

mustRequireFlags(cmd, "name", "identifier")

Expand All @@ -95,8 +108,8 @@ auth0 apis create --name myapi --identifier http://my-api

func updateApiCmd(cli *cli) *cobra.Command {
var flags struct {
id string
name string
ID string
Name string
}

cmd := &cobra.Command{
Expand All @@ -106,11 +119,23 @@ func updateApiCmd(cli *cli) *cobra.Command {
auth0 apis update --id id --name myapi
`,
PreRun: func(cmd *cobra.Command, args []string) {
checkFlags(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
api := &management.ResourceServer{Name: &flags.name}
if !hasFlags(cmd) {
id := prompt.TextInput("id", "Id:", "Id of the API.", true)
name := prompt.TextInput("name", "Name:", "Name of the API.", true)

if err := prompt.Ask([]*survey.Question {id, name}, &flags); err != nil {
return err
}
}

api := &management.ResourceServer{Name: &flags.Name}

err := ansi.Spinner("Updating API", func() error {
return cli.api.ResourceServer.Update(flags.id, api)
return cli.api.ResourceServer.Update(flags.ID, api)
})

if err != nil {
Expand All @@ -122,8 +147,8 @@ auth0 apis update --id id --name myapi
},
}

cmd.Flags().StringVarP(&flags.id, "id", "i", "", "ID of the API.")
cmd.Flags().StringVarP(&flags.name, "name", "n", "", "Name of the API.")
cmd.Flags().StringVarP(&flags.ID, "id", "i", "", "ID of the API.")
cmd.Flags().StringVarP(&flags.Name, "name", "n", "", "Name of the API.")

mustRequireFlags(cmd, "id", "name")

Expand All @@ -132,21 +157,24 @@ auth0 apis update --id id --name myapi

func deleteApiCmd(cli *cli) *cobra.Command {
var flags struct {
id string
id string
force bool
}

cmd := &cobra.Command{
Use: "delete",
Short: "Delete an API",
Long: `Deletes an API:
auth0 apis delete --id id
auth0 apis delete --id id --force
`,
RunE: func(cmd *cobra.Command, args []string) error {
if confirmed := prompt.Confirm("Are you sure you want to proceed?"); !confirmed {
return nil
if !flags.force {
if confirmed := prompt.Confirm("Are you sure you want to proceed?"); !confirmed {
return nil
}
}

err := ansi.Spinner("Deleting API", func() error {
return cli.api.ResourceServer.Delete(flags.id)
})
Expand All @@ -160,8 +188,7 @@ auth0 apis delete --id id
}

cmd.Flags().StringVarP(&flags.id, "id", "i", "", "ID of the API.")

mustRequireFlags(cmd, "id")
cmd.Flags().BoolVarP(&flags.force, "force", "f", false, "Do not ask for confirmation.")

return cmd
}
10 changes: 10 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ func (c *cli) initContext() (err error) {
return nil
}

func hasFlags(cmd *cobra.Command) bool {
return cmd.Flags().NFlag() > 0
}

func checkFlags(cmd *cobra.Command) {
if (!hasFlags(cmd)) {
cmd.ResetFlags()
}
}

func mustRequireFlags(cmd *cobra.Command, flags ...string) {
for _, f := range flags {
if err := cmd.MarkFlagRequired(f); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ func Ask(inputs []*survey.Question, response interface{}) error {
return survey.Ask(inputs, response, stdErrWriter)
}

func TextInput(name string, message string, required bool) *survey.Question {
func TextInput(name string, message string, help string, required bool) *survey.Question {
input := &survey.Question{
Name: name,
Prompt: &survey.Input{Message: message},
Prompt: &survey.Input{Message: message, Help: help},
Transform: survey.Title,
}

Expand Down

0 comments on commit fdbbb0f

Please sign in to comment.