Skip to content

Commit

Permalink
Change apps update command to prompt when flagless
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket committed Mar 5, 2021
1 parent 72c1a66 commit aebb126
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 40 deletions.
24 changes: 15 additions & 9 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ 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 {
Expand Down Expand Up @@ -201,7 +201,7 @@ 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 {
Expand Down Expand Up @@ -298,7 +298,7 @@ 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 {
Expand Down Expand Up @@ -391,7 +391,7 @@ 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 {
Expand Down Expand Up @@ -567,9 +567,12 @@ 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 {
Expand Down Expand Up @@ -651,9 +654,12 @@ 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 {
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 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 Down Expand Up @@ -845,7 +851,7 @@ 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 {
Expand Down
129 changes: 98 additions & 31 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
appName = "name"
appType = "type"
appDescription = "description"
appCallback = "callbacks"
)

func appsCmd(cli *cli) *cobra.Command {
Expand Down Expand Up @@ -83,7 +84,7 @@ auth0 apps show <id>
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, appID) {
if canPrompt(cmd) {
input := prompt.TextInput(appID, "Id:", "Id of the application.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
Expand Down Expand Up @@ -135,7 +136,7 @@ auth0 apps delete <id>
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, appID) {
if canPrompt(cmd) {
input := prompt.TextInput(appID, "Id:", "Id of the application.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
Expand Down Expand Up @@ -235,7 +236,7 @@ auth0 apps create --name myapp --type [native|spa|regular|m2m]
OIDCConformant: &oidcConformant,
}

if (len(flags.Grants) == 0) {
if len(flags.Grants) == 0 {
a.GrantTypes = apiDefaultGrantsFor(flags.Type)
} else {
a.GrantTypes = apiGrantsFor(flags.Grants)
Expand Down Expand Up @@ -282,6 +283,7 @@ func updateAppCmd(cli *cli) *cobra.Command {
Type string
Description string
Callbacks []string
CallbacksString string
AllowedOrigins []string
AllowedWebOrigins []string
AllowedLogoutURLs []string
Expand All @@ -302,7 +304,7 @@ auth0 apps update <id> --name myapp --type [native|spa|regular|m2m]
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if shouldPrompt(cmd, appID) {
if canPrompt(cmd) {
input := prompt.TextInput(appID, "Id:", "Id of the application.", true)

if err := prompt.AskOne(input, &inputs); err != nil {
Expand All @@ -315,15 +317,15 @@ auth0 apps update <id> --name myapp --type [native|spa|regular|m2m]
inputs.ID = args[0]
}

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

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

if shouldPrompt(cmd, appType) {
if shouldPromptWhenFlagless(cmd, appType) {
input := prompt.SelectInput(
appType,
"Type:",
Expand All @@ -339,35 +341,96 @@ auth0 apps update <id> --name myapp --type [native|spa|regular|m2m]
}
}

if shouldPrompt(cmd, appDescription) {
if shouldPromptWhenFlagless(cmd, appDescription) {
input := prompt.TextInput(appDescription, "Description:", "Description of the application.", false)

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

a := &management.Client{
Name: &inputs.Name,
Description: &inputs.Description,
AppType: auth0.String(apiTypeFor(inputs.Type)),
Callbacks: stringToInterfaceSlice(inputs.Callbacks),
AllowedOrigins: stringToInterfaceSlice(inputs.AllowedOrigins),
WebOrigins: stringToInterfaceSlice(inputs.AllowedWebOrigins),
AllowedLogoutURLs: stringToInterfaceSlice(inputs.AllowedLogoutURLs),
TokenEndpointAuthMethod: apiAuthMethodFor(inputs.AuthMethod),
GrantTypes: apiGrantsFor(inputs.Grants),
if shouldPromptWhenFlagless(cmd, "CallbacksString") {
input := prompt.TextInput("CallbacksString", "Callback URLs:", "Callback URLs of the application, comma-separated.", false)

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

err := ansi.Spinner("Updating application", func() error {
a := &management.Client{}

updateErr := ansi.Spinner("Updating application", func() error {
current, readErr := cli.api.Client.Read(inputs.ID)

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

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

if len(inputs.Description) == 0 {
a.Description = current.Description
} else {
a.Description = &inputs.Description
}

if len(inputs.Type) == 0 {
a.AppType = current.AppType
} else {
a.AppType = auth0.String(apiTypeFor(inputs.Type))
}

if len(inputs.Callbacks) == 0 {
if len(inputs.CallbacksString) == 0 {
a.Callbacks = current.Callbacks
} else {
a.Callbacks = stringToInterfaceSlice(commaSeparatedStringToSlice(inputs.CallbacksString))
}
} else {
a.Callbacks = stringToInterfaceSlice(inputs.Callbacks)
}

if len(inputs.AllowedOrigins) == 0 {
a.AllowedOrigins = current.AllowedOrigins
} else {
a.AllowedOrigins = stringToInterfaceSlice(inputs.AllowedOrigins)
}

if len(inputs.AllowedWebOrigins) == 0 {
a.WebOrigins = current.WebOrigins
} else {
a.WebOrigins = stringToInterfaceSlice(inputs.AllowedWebOrigins)
}

if len(inputs.AllowedLogoutURLs) == 0 {
a.AllowedLogoutURLs = current.AllowedLogoutURLs
} else {
a.AllowedLogoutURLs = stringToInterfaceSlice(inputs.AllowedLogoutURLs)
}

if len(inputs.AuthMethod) == 0 {
a.TokenEndpointAuthMethod = current.TokenEndpointAuthMethod
} else {
a.TokenEndpointAuthMethod = apiAuthMethodFor(inputs.AuthMethod)
}

if len(inputs.Grants) == 0 {
a.GrantTypes = current.GrantTypes
} else {
a.GrantTypes = apiGrantsFor(inputs.Grants)
}

return cli.api.Client.Update(inputs.ID, a)
})

if err != nil {
return fmt.Errorf("Unable to update application %v: %v", inputs.ID, err)
if updateErr != nil {
return fmt.Errorf("Unable to update application %v: %v", inputs.ID, updateErr)
}

// note: a is populated with the rest of the client fields by the API during creation.
revealClientSecret := auth0.StringValue(a.AppType) != "native" && auth0.StringValue(a.AppType) != "spa"
cli.renderer.ApplicationUpdate(a, revealClientSecret)

Expand Down Expand Up @@ -455,16 +518,16 @@ func apiGrantsFor(s []string) []interface{} {

func apiDefaultGrantsFor(t string) []interface{} {
switch apiTypeFor(strings.ToLower(t)) {
case "native":
return stringToInterfaceSlice([]string{"implicit", "authorization_code", "refresh_token"})
case "spa":
return stringToInterfaceSlice([]string{"implicit", "authorization_code", "refresh_token"})
case "regular_web":
return stringToInterfaceSlice([]string{"implicit", "authorization_code", "refresh_token", "client_credentials"})
case "non_interactive":
return stringToInterfaceSlice([]string{"client_credentials"})
default:
return nil
case "native":
return stringToInterfaceSlice([]string{"implicit", "authorization_code", "refresh_token"})
case "spa":
return stringToInterfaceSlice([]string{"implicit", "authorization_code", "refresh_token"})
case "regular_web":
return stringToInterfaceSlice([]string{"implicit", "authorization_code", "refresh_token", "client_credentials"})
case "non_interactive":
return stringToInterfaceSlice([]string{"client_credentials"})
default:
return nil
}
}

Expand All @@ -476,6 +539,10 @@ func urlsFor(s []interface{}) []string {
return res
}

func commaSeparatedStringToSlice(s string) []string {
return strings.Split(strings.Join(strings.Fields(s), ""), ",")
}

func stringToInterfaceSlice(s []string) []interface{} {
var result []interface{} = make([]interface{}, len(s))
for i, d := range s {
Expand Down
12 changes: 12 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ func shouldPrompt(cmd *cobra.Command, flag string) bool {
return canPrompt(cmd) && !cmd.Flags().Changed(flag)
}

func shouldPromptWhenFlagless(cmd *cobra.Command, flag string) bool {
isSet := false

cmd.Flags().VisitAll(func(f *pflag.Flag) {
if (f.Changed) {
isSet = true
}
})

return canPrompt(cmd) && !isSet
}

func prepareInteractivity(cmd *cobra.Command) {
if canPrompt(cmd) {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
Expand Down

0 comments on commit aebb126

Please sign in to comment.