Skip to content

Commit

Permalink
Make interactivity tty-based on rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket committed Jan 27, 2021
1 parent e1e926b commit c205e83
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 118 deletions.
82 changes: 40 additions & 42 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

const (
id = "id"
name = "name"
identifier = "identifier"
apiID = "id"
apiName = "name"
apiIdentifier = "identifier"
)

func apisCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "apis",
Short: "manage resources for APIs.",
Use: "apis",
Short: "manage resources for APIs.",
Aliases: []string{"resource-servers"},
}

Expand Down Expand Up @@ -62,7 +62,7 @@ Lists your existing APIs. To create one try:

func showApiCmd(cli *cli) *cobra.Command {
var flags struct {
ID string
ID string
}

cmd := &cobra.Command{
Expand All @@ -72,12 +72,12 @@ func showApiCmd(cli *cli) *cobra.Command {
auth0 apis show --id id
`,
PreRun: func(cmd *cobra.Command, args []string) {
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if shouldPrompt(cmd, id) {
input := prompt.TextInput(id, "Id:", "Id of the API.", "", true)
if shouldPrompt(cmd, apiID) {
input := prompt.TextInput(apiID, "Id:", "Id of the API.", "", true)

if err := prompt.AskOne(input, &flags); err != nil {
return err
Expand All @@ -101,8 +101,8 @@ auth0 apis show --id id
},
}

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

return cmd
}
Expand All @@ -120,27 +120,27 @@ func createApiCmd(cli *cli) *cobra.Command {
auth0 apis create --name myapi --identifier http://my-api
`,
PreRun: func(cmd *cobra.Command, args []string) {
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if shouldPrompt(cmd, name) {
if shouldPrompt(cmd, apiName) {
input := prompt.TextInput(
name, "Name:",
"Name of the API. You can change the API name later in the API settings.",
"",
apiName, "Name:",
"Name of the API. You can change the API name later in the API settings.",
"",
true)

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

if shouldPrompt(cmd, identifier) {
if shouldPrompt(cmd, apiIdentifier) {
input := prompt.TextInput(
identifier, "Identifier:",
"Identifier of the API. Cannot be changed once set.",
"",
apiIdentifier, "Identifier:",
"Identifier of the API. Cannot be changed once set.",
"",
true)

if err := prompt.AskOne(input, &flags); err != nil {
Expand All @@ -166,9 +166,9 @@ 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.")
mustRequireFlags(cmd, name, identifier)
cmd.Flags().StringVarP(&flags.Name, apiName, "n", "", "Name of the API.")
cmd.Flags().StringVarP(&flags.Identifier, apiIdentifier, "i", "", "Identifier of the API.")
mustRequireFlags(cmd, apiName, apiIdentifier)

return cmd
}
Expand All @@ -186,20 +186,20 @@ func updateApiCmd(cli *cli) *cobra.Command {
auth0 apis update --id id --name myapi
`,
PreRun: func(cmd *cobra.Command, args []string) {
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if shouldPrompt(cmd, id) {
input := prompt.TextInput(id, "Id:", "Id of the API.", "", true)
if shouldPrompt(cmd, apiID) {
input := prompt.TextInput(apiID, "Id:", "Id of the API.", "", true)

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

if shouldPrompt(cmd, name) {
input := prompt.TextInput(name, "Name:", "Name of the API.", "", true)
if shouldPrompt(cmd, apiName) {
input := prompt.TextInput(apiName, "Name:", "Name of the API.", "", true)

if err := prompt.AskOne(input, &flags); err != nil {
return err
Expand All @@ -221,44 +221,43 @@ 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.")
mustRequireFlags(cmd, id, name)
cmd.Flags().StringVarP(&flags.ID, apiID, "i", "", "ID of the API.")
cmd.Flags().StringVarP(&flags.Name, apiName, "n", "", "Name of the API.")
mustRequireFlags(cmd, apiID, apiName)

return cmd
}

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

cmd := &cobra.Command{
Use: "delete",
Short: "Delete an API",
Long: `Deletes an API:
auth0 apis delete --id id --force
auth0 apis delete --id id
`,
PreRun: func(cmd *cobra.Command, args []string) {
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if shouldPrompt(cmd, id) {
input := prompt.TextInput(id, "Id:", "Id of the API.", "", true)
if shouldPrompt(cmd, apiID) {
input := prompt.TextInput(apiID, "Id:", "Id of the API.", "", true)

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

if !flags.force && canPrompt() {
if !cli.force && canPrompt() {
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 @@ -271,9 +270,8 @@ auth0 apis delete --id id --force
},
}

cmd.Flags().StringVarP(&flags.ID, id, "i", "", "ID of the API.")
cmd.Flags().BoolVarP(&flags.force, "force", "f", false, "Do not ask for confirmation.")
mustRequireFlags(cmd, id)
cmd.Flags().StringVarP(&flags.ID, apiID, "i", "", "ID of the API.")
mustRequireFlags(cmd, apiID)

return cmd
}
12 changes: 1 addition & 11 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,6 @@ 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 All @@ -246,7 +236,7 @@ func canPrompt() bool {
}

func shouldPrompt(cmd *cobra.Command, flag string) bool {
return ansi.IsTerminal() && !cmd.Flags().Changed(flag)
return canPrompt() && !cmd.Flags().Changed(flag)
}

func prepareInteractivity(cmd *cobra.Command) {
Expand Down
Loading

0 comments on commit c205e83

Please sign in to comment.