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

Function refreshRun

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

Source from the content-addressed store, hash-verified

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

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