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