MCPcopy Create free account
hub / github.com/algolia/cli / runSelectCmd

Function runSelectCmd

pkg/cmd/application/selectapp/select.go:88–170  ·  view source on GitHub ↗
(opts *SelectOptions)

Source from the content-addressed store, hash-verified

86}
87
88func 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 == "" {

Callers 2

NewSelectCmdFunction · 0.85
RunFunction · 0.85

Calls 15

AddMethod · 0.95
OAuthClientIDFunction · 0.92
EnsureAuthenticatedFunction · 0.92
ReauthenticateIfExpiredFunction · 0.92
EnsureAPIKeyFunction · 0.92
ConfigureProfileFunction · 0.92
pickApplicationFunction · 0.85
ColorSchemeMethod · 0.80
ListApplicationsMethod · 0.80
StopProgressIndicatorMethod · 0.80
WarningIconMethod · 0.80

Tested by

no test coverage detected