MCPcopy Create free account
hub / github.com/cli/cli / refreshRun

Function refreshRun

pkg/cmd/auth/refresh/refresh.go:148–338  ·  view source on GitHub ↗
(opts *RefreshOptions)

Source from the content-addressed store, hash-verified

146}
147
148func refreshRun(opts *RefreshOptions) error {
149 plainHTTPClient, err := opts.PlainHttpClient()
150 if err != nil {
151 return err
152 }
153
154 cfg, err := opts.Config()
155 if err != nil {
156 return err
157 }
158 authCfg := cfg.Authentication()
159 copyToClipboard := shared.ShouldCopyToClipboard(cfg, opts.Clipboard)
160
161 candidates := authCfg.Hosts()
162 if len(candidates) == 0 {
163 return fmt.Errorf("not logged in to any hosts. Use 'gh auth login' to authenticate with a host")
164 }
165
166 hostname := opts.Hostname
167 if hostname == "" {
168 if len(candidates) == 1 {
169 hostname = candidates[0]
170 } else {
171 selected, err := opts.Prompter.Select("What account do you want to refresh auth for?", "", candidates)
172 if err != nil {
173 return fmt.Errorf("could not prompt: %w", err)
174 }
175 hostname = candidates[selected]
176 }
177 } else if !slices.Contains(candidates, hostname) {
178 return fmt.Errorf("not logged in to %s. use 'gh auth login' to authenticate with this host", hostname)
179 }
180
181 // Resolve whether to request an expiring token. An explicit --expiring or --expiring=false wins; otherwise refresh
182 // keeps the current token's kind, so an expiring token stays expiring and a non-expiring one stays non-expiring
183 // without the user restating the preference on every refresh. The expiring_credentials configuration is a
184 // login-time preference and is deliberately not consulted here.
185 var expiring bool
186 if opts.Expiring != nil {
187 expiring = *opts.Expiring
188 } else {
189 expiring = authCfg.ActiveToken(hostname).IsRefreshable()
190 }
191
192 if src, writeable := shared.AuthTokenWriteable(authCfg, hostname); !writeable {
193 fmt.Fprintf(opts.IO.ErrOut, "The value of the %s environment variable is being used for authentication.\n", src)
194 fmt.Fprint(opts.IO.ErrOut, "To refresh credentials stored in GitHub CLI, first clear the value from the environment.\n")
195 return cmdutil.SilentError
196 }
197
198 cs := opts.IO.ColorScheme()
199
200 // Begin the auth-state telemetry event. It is nil for disabled telemetry or enterprise hosts, and all event methods
201 // are nil-tolerant, so the calls below stay unconditional.
202 authEvent := shared.BeginAuthTelemetry(opts.Telemetry, "refresh", hostname, expiring)
203
204 additionalScopes := set.NewStringSet()
205

Calls 15

PromptMethod · 0.95
ScopesMethod · 0.95
ShouldSetupMethod · 0.95
SetupMethod · 0.95
ShouldCopyToClipboardFunction · 0.92
AuthTokenWriteableFunction · 0.92
BeginAuthTelemetryFunction · 0.92
NewStringSetFunction · 0.92
GetScopesFunction · 0.92
ContainsMethod · 0.80