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

Function refreshRun

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

Source from the content-addressed store, hash-verified

126}
127
128func refreshRun(opts *RefreshOptions) error {
129 plainHTTPClient, err := opts.PlainHttpClient()
130 if err != nil {
131 return err
132 }
133
134 cfg, err := opts.Config()
135 if err != nil {
136 return err
137 }
138 authCfg := cfg.Authentication()
139 copyToClipboard := shared.ShouldCopyToClipboard(cfg, opts.Clipboard)
140
141 candidates := authCfg.Hosts()
142 if len(candidates) == 0 {
143 return fmt.Errorf("not logged in to any hosts. Use 'gh auth login' to authenticate with a host")
144 }
145
146 hostname := opts.Hostname
147 if hostname == "" {
148 if len(candidates) == 1 {
149 hostname = candidates[0]
150 } else {
151 selected, err := opts.Prompter.Select("What account do you want to refresh auth for?", "", candidates)
152 if err != nil {
153 return fmt.Errorf("could not prompt: %w", err)
154 }
155 hostname = candidates[selected]
156 }
157 } else if !slices.Contains(candidates, hostname) {
158 return fmt.Errorf("not logged in to %s. use 'gh auth login' to authenticate with this host", hostname)
159 }
160
161 if src, writeable := shared.AuthTokenWriteable(authCfg, hostname); !writeable {
162 fmt.Fprintf(opts.IO.ErrOut, "The value of the %s environment variable is being used for authentication.\n", src)
163 fmt.Fprint(opts.IO.ErrOut, "To refresh credentials stored in GitHub CLI, first clear the value from the environment.\n")
164 return cmdutil.SilentError
165 }
166
167 additionalScopes := set.NewStringSet()
168
169 if !opts.ResetScopes {
170 if oldToken, _ := authCfg.ActiveToken(hostname); oldToken != "" {
171 if oldScopes, err := shared.GetScopes(plainHTTPClient, hostname, oldToken); err == nil {
172 for s := range strings.SplitSeq(oldScopes, ",") {
173 s = strings.TrimSpace(s)
174 if s != "" {
175 additionalScopes.Add(s)
176 }
177 }
178 }
179 }
180 }
181
182 credentialFlow := &shared.GitCredentialFlow{
183 Prompter: opts.Prompter,
184 HelperConfig: &gitcredentials.HelperConfig{
185 SelfExecutablePath: opts.MainExecutable,

Callers 2

NewCmdRefreshFunction · 0.85
Test_refreshRunFunction · 0.85

Calls 15

PromptMethod · 0.95
ScopesMethod · 0.95
ShouldSetupMethod · 0.95
SetupMethod · 0.95
ShouldCopyToClipboardFunction · 0.92
AuthTokenWriteableFunction · 0.92
NewStringSetFunction · 0.92
GetScopesFunction · 0.92
usernameTypeAlias · 0.85
ContainsMethod · 0.80
AddValuesMethod · 0.80
RemoveValuesMethod · 0.80

Tested by 1

Test_refreshRunFunction · 0.68