Skip to content

Commit

Permalink
Merge pull request #136 from auth0/prompt-when-flagless
Browse files Browse the repository at this point in the history
Change update commands to only prompt when flagless [CLI-56]
  • Loading branch information
cyx authored Mar 6, 2021
2 parents 53a4320 + ce9fc3b commit 5c5a741
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 74 deletions.
48 changes: 27 additions & 21 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ auth0 actions test <id> --file payload.json`,
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, actionID) {
if canPrompt(cmd) {
input := prompt.TextInput(actionID, "Id:", "Action Id to test.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return err
}
} else {
return errors.New("missing action id")
return errors.New("Please provide an action Id")
}
} else {
inputs.ID = args[0]
Expand Down Expand Up @@ -201,14 +201,14 @@ auth0 actions deploy <id> --version version-id`,
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, actionID) {
if canPrompt(cmd) {
input := prompt.TextInput(actionID, "Id:", "Action Id to deploy.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return err
}
} else {
return errors.New("missing action id")
return errors.New("Please provide an action Id")
}
} else {
inputs.ID = args[0]
Expand Down Expand Up @@ -298,14 +298,14 @@ auth0 actions download <id> --version <version-id | draft>`,
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, actionID) {
if canPrompt(cmd) {
input := prompt.TextInput(actionID, "Id:", "Action Id to download.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return err
}
} else {
return errors.New("missing action id")
return errors.New("Please provide an action Id")
}
} else {
inputs.ID = args[0]
Expand Down Expand Up @@ -391,14 +391,14 @@ auth0 actions versions <id>`,
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, actionID) {
if canPrompt(cmd) {
input := prompt.TextInput(actionID, "Id:", "Action Id to show versions.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return err
}
} else {
return errors.New("missing action id")
return errors.New("Please provide an action Id")
}
} else {
inputs.ID = args[0]
Expand Down Expand Up @@ -567,22 +567,25 @@ func updateActionCmd(cli *cli) *cobra.Command {
$ auth0 actions update <id> --file action.js --dependency [email protected]
`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, actionID) {
if canPrompt(cmd) {
input := prompt.TextInput(actionID, "Id:", "Id of the action.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return err
}
} else {
return errors.New("missing action id")
return errors.New("Please provide an action Id")
}
} else {
inputs.ID = args[0]
}

if shouldPrompt(cmd, actionFile) && shouldPrompt(cmd, actionScript) {
if shouldPromptWhenFlagless(cmd, actionFile) && shouldPrompt(cmd, actionScript) {
input := prompt.TextInput(actionFile, "Action File:", "File containing the action source code.", false)

if err := prompt.AskOne(input, &inputs); err != nil {
Expand Down Expand Up @@ -633,7 +636,7 @@ $ auth0 actions update <id> --file action.js --dependency [email protected]
cmd.Flags().BoolVarP(&inputs.CreateVersion, actionVersion, "v", false, "Create an explicit action version from the source code instead of a draft.")

if err := cmd.MarkFlagFilename(actionFile); err != nil {
panic(err)
fmt.Println(fmt.Errorf("An unexpected error occurred: %w", err))
}

return cmd
Expand All @@ -651,16 +654,19 @@ func deleteActionCmd(cli *cli) *cobra.Command {
Long: `Delete an action:
$ auth0 actions delete <id>`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, actionID) {
if canPrompt(cmd) {
input := prompt.TextInput(actionID, "Id:", "Id of the action.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return err
}
} else {
return errors.New("missing action id")
return errors.New("Please provide an action Id")
}
} else {
inputs.ID = args[0]
Expand Down Expand Up @@ -704,7 +710,7 @@ auth0 actions flows show <trigger>`,
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, actionTrigger) {
if canPrompt(cmd) {
input := prompt.SelectInput(
actionTrigger,
"Trigger:",
Expand All @@ -716,7 +722,7 @@ auth0 actions flows show <trigger>`,
return err
}
} else {
return errors.New("missing action trigger")
return errors.New("Please provide an action trigger")
}
} else {
inputs.Trigger = args[0]
Expand Down Expand Up @@ -764,7 +770,7 @@ auth0 actions flows update <trigger> --file bindings.json`,
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, actionTrigger) {
if canPrompt(cmd) {
input := prompt.SelectInput(
actionTrigger,
"Trigger:",
Expand All @@ -776,13 +782,13 @@ auth0 actions flows update <trigger> --file bindings.json`,
return err
}
} else {
return errors.New("missing action trigger")
return errors.New("Please provide an action trigger")
}
} else {
inputs.Trigger = args[0]
}

if shouldPrompt(cmd, actionFile) {
if shouldPromptWhenFlagless(cmd, actionFile) {
input := prompt.TextInput(actionFile, "File:", "File containing the bindings.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
Expand Down Expand Up @@ -845,14 +851,14 @@ auth0 actions bind <id> --trigger post-login`,
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, actionID) {
if canPrompt(cmd) {
input := prompt.TextInput(actionID, "Action Id:", "Action Id to bind.", false)

if err := prompt.AskOne(input, &inputs); err != nil {
return err
}
} else {
return errors.New("missing action id")
return errors.New("Please provide an action Id")
}
} else {
inputs.ID = args[0]
Expand Down
56 changes: 35 additions & 21 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ auth0 apis show <id>
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, apiID) {
if canPrompt(cmd) {
input := prompt.TextInput(apiID, "Id:", "Id of the API.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}
} else {
return errors.New("Please include an API id")
return errors.New("Please include an API Id")
}
} else {
inputs.ID = args[0]
Expand All @@ -118,7 +118,7 @@ auth0 apis show <id>
})

if err != nil {
return fmt.Errorf("Unable to get an API with id %s: %w", inputs.ID, err)
return fmt.Errorf("Unable to get an API with Id %s: %w", inputs.ID, err)
}

cli.renderer.ApiShow(api)
Expand Down Expand Up @@ -183,7 +183,7 @@ auth0 apis create --name myapi --identifier http://my-api
}

if flags.Scopes != "" {
api.Scopes = getScopes(flags.Scopes)
api.Scopes = apiScopesFor(flags.Scopes)
}

err := ansi.Spinner("Creating API", func() error {
Expand Down Expand Up @@ -227,47 +227,61 @@ auth0 apis update <id> --name myapi
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, apiID) {
if canPrompt(cmd) {
input := prompt.TextInput(apiID, "Id:", "Id of the API.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}
} else {
return errors.New("Please include an API id")
return errors.New("Please include an API Id")
}
} else {
inputs.ID = args[0]
}

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

if err := prompt.AskOne(input, &inputs); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}
}

if shouldPrompt(cmd, apiScopes) {
if shouldPromptWhenFlagless(cmd, apiScopes) {
input := prompt.TextInput(apiScopes, "Scopes:", "Space-separated list of scopes.", false)

if err := prompt.AskOne(input, &inputs); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}
}

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

if inputs.Scopes != "" {
api.Scopes = getScopes(inputs.Scopes)
}
api := &management.ResourceServer{}

err := ansi.Spinner("Updating API", func() error {
current, err := cli.api.ResourceServer.Read(inputs.ID)

if err != nil {
return fmt.Errorf("Unable to load API. The Id %v specified doesn't exist", inputs.ID)
}

if len(inputs.Name) == 0 {
api.Name = current.Name
} else {
api.Name = &inputs.Name
}

if len(inputs.Scopes) == 0 {
api.Scopes = current.Scopes
} else {
api.Scopes = apiScopesFor(inputs.Scopes)
}

return cli.api.ResourceServer.Update(inputs.ID, api)
})

if err != nil {
return fmt.Errorf("An unexpected error occurred while trying to update an API with id %s: %w", inputs.ID, err)
return fmt.Errorf("An unexpected error occurred while trying to update an API with Id %s: %w", inputs.ID, err)
}

cli.renderer.ApiUpdate(api)
Expand Down Expand Up @@ -299,14 +313,14 @@ auth0 apis delete <id>
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, apiID) {
if canPrompt(cmd) {
input := prompt.TextInput(apiID, "Id:", "Id of the API.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}
} else {
return errors.New("Please include an API id")
return errors.New("Please include an API Id")
}
} else {
inputs.ID = args[0]
Expand All @@ -321,7 +335,7 @@ auth0 apis delete <id>
return ansi.Spinner("Deleting API", func() error {
err := cli.api.ResourceServer.Delete(inputs.ID)
if err != nil {
return fmt.Errorf("An unexpected error occurred while attempting to delete an API with id %s: %w", inputs.ID, err)
return fmt.Errorf("An unexpected error occurred while attempting to delete an API with Id %s: %w", inputs.ID, err)
}
return nil
})
Expand Down Expand Up @@ -349,14 +363,14 @@ auth0 apis scopes list <id>
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, apiID) {
if canPrompt(cmd) {
input := prompt.TextInput(apiID, "Id:", "Id of the API.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}
} else {
return errors.New("Please include an API id")
return errors.New("Please include an API Id")
}
} else {
inputs.ID = args[0]
Expand All @@ -371,7 +385,7 @@ auth0 apis scopes list <id>
})

if err != nil {
return fmt.Errorf("An unexpected error occurred while getting scopes for an API with id %s: %w", inputs.ID, err)
return fmt.Errorf("An unexpected error occurred while getting scopes for an API with Id %s: %w", inputs.ID, err)
}

cli.renderer.ScopesList(api.GetName(), api.Scopes)
Expand All @@ -382,7 +396,7 @@ auth0 apis scopes list <id>
return cmd
}

func getScopes(scopes string) []*management.ResourceServerScope {
func apiScopesFor(scopes string) []*management.ResourceServerScope {
list := strings.Fields(scopes)
models := []*management.ResourceServerScope{}

Expand Down
Loading

0 comments on commit 5c5a741

Please sign in to comment.