(opts *SelectOptions)
| 86 | } |
| 87 | |
| 88 | func runSelectCmd(opts *SelectOptions) (*dashboard.Application, error) { |
| 89 | cs := opts.IO.ColorScheme() |
| 90 | client := opts.NewDashboardClient(auth.OAuthClientID()) |
| 91 | |
| 92 | accessToken, err := auth.EnsureAuthenticated(opts.IO, client) |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | |
| 97 | opts.IO.StartProgressIndicatorWithLabel("Fetching applications") |
| 98 | apps, err := client.ListApplications(accessToken) |
| 99 | opts.IO.StopProgressIndicator() |
| 100 | if err != nil { |
| 101 | newToken, reAuthErr := auth.ReauthenticateIfExpired(opts.IO, client, err) |
| 102 | if reAuthErr != nil { |
| 103 | return nil, reAuthErr |
| 104 | } |
| 105 | accessToken = newToken |
| 106 | opts.IO.StartProgressIndicatorWithLabel("Fetching applications") |
| 107 | apps, err = client.ListApplications(accessToken) |
| 108 | opts.IO.StopProgressIndicator() |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if len(apps) == 0 { |
| 115 | fmt.Fprintf(opts.IO.Out, "%s No applications found.\n", cs.WarningIcon()) |
| 116 | fmt.Fprintf(opts.IO.Out, " Use %s to create one.\n", cs.Bold("algolia application create")) |
| 117 | return nil, nil |
| 118 | } |
| 119 | |
| 120 | chosen, err := pickApplication(opts, apps) |
| 121 | if err != nil { |
| 122 | return nil, err |
| 123 | } |
| 124 | |
| 125 | // If a profile already exists for this app, switch the default |
| 126 | // and ensure it has an API key. |
| 127 | if exists, profileName := opts.Config.ApplicationIDExists(chosen.ID); exists { |
| 128 | // Read the profile BEFORE SetDefaultProfile, because viper.Set() calls |
| 129 | // inside SetDefaultProfile pollute the override map and cause |
| 130 | // UnmarshalKey to return empty fields (known viper issue). |
| 131 | var existingProfile *config.Profile |
| 132 | for _, p := range opts.Config.ConfiguredProfiles() { |
| 133 | if p.Name == profileName { |
| 134 | existingProfile = p |
| 135 | break |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if err := opts.Config.SetDefaultProfile(profileName); err != nil { |
| 140 | return nil, fmt.Errorf("failed to set default profile: %w", err) |
| 141 | } |
| 142 | fmt.Fprintf(opts.IO.Out, "%s Switched to profile %q (application %s).\n", |
| 143 | cs.SuccessIcon(), profileName, cs.Bold(chosen.ID)) |
| 144 | |
| 145 | if existingProfile != nil && existingProfile.APIKey == "" { |
no test coverage detected