Skip to content

Commit

Permalink
refactor: one prompt; check if url already added
Browse files Browse the repository at this point in the history
  • Loading branch information
as-herzog committed Mar 15, 2021
1 parent 9f313f1 commit 391d57e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
32 changes: 22 additions & 10 deletions internal/cli/quickstarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,31 +311,30 @@ func quickstartsTypeFor(v string) string {
}
}

// promptDefaultURLs checks whether the application is SPA or WebApp and
// whether the app has already default quickstart url to allowed url lists.
// If not, it prompts the user to add the default url and updates the application
// if they accept.
func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, qsType string) error {
if !strings.EqualFold(qsType, QSSpa) && !strings.EqualFold(qsType, QSWebApp) {
return nil
}
if containsStr(client.Callbacks, _defaultURL) || containsStr(client.AllowedLogoutURLs, _defaultURL) {
return nil
}

a := &management.Client{
Callbacks: client.Callbacks,
WebOrigins: client.WebOrigins,
AllowedLogoutURLs: client.AllowedLogoutURLs,
}
shouldUpdate := false
if confirmed := prompt.Confirm(fmt.Sprintf("Do you want to add this URL to the list of allowed callback URLs: %s", _defaultURL)); confirmed {
if confirmed := prompt.Confirm(formatURLPrompt(qsType)); confirmed {
a.Callbacks = append(a.Callbacks, _defaultURL)
shouldUpdate = true
}
if confirmed := prompt.Confirm(fmt.Sprintf("Do you want to add this URL to the list of allowed logout URLs: %s", _defaultURL)); confirmed {
a.AllowedLogoutURLs = append(a.AllowedLogoutURLs, _defaultURL)
a.WebOrigins = append(a.WebOrigins, _defaultURL)
shouldUpdate = true
}
if strings.EqualFold(qsType, QSSpa) {
if confirmed := prompt.Confirm(fmt.Sprintf("Do you want to add this URL to the list of allowed web origins: %s", _defaultURL)); confirmed {
a.WebOrigins = append(a.WebOrigins, _defaultURL)
shouldUpdate = true
}
}
if shouldUpdate {
err := ansi.Spinner("Updating application", func() error {
return cli.api.Client.Update(client.GetClientID(), a)
Expand All @@ -347,3 +346,16 @@ func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client,
}
return nil
}

// formatURLPrompt creates the correct prompt based on app type for
// asking the user if they would like to add default urls.
func formatURLPrompt(qsType string) string {
var p strings.Builder
p.WriteString("\nQuickstarts use localhost, do you want to add %s to the list of allowed callback URLs")
if strings.EqualFold(qsType, QSSpa) {
p.WriteString(", logout URLs, and web origins?")
} else {
p.WriteString(" and logout URLs?")
}
return fmt.Sprintf(p.String(), _defaultURL)
}
10 changes: 10 additions & 0 deletions internal/cli/utils_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,13 @@ func generateState(size int) (string, error) {

return base64.RawURLEncoding.EncodeToString(b), nil
}

// check if slice contains a string
func containsStr(s []interface{}, u string) bool {
for _, a := range s {
if a == u {
return true
}
}
return false
}

0 comments on commit 391d57e

Please sign in to comment.