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

Function GetCurrentLogin

pkg/cmd/auth/shared/login_flow.go:253–285  ·  view source on GitHub ↗
(httpClient httpClient, hostname, authToken string)

Source from the content-addressed store, hash-verified

251}
252
253func GetCurrentLogin(httpClient httpClient, hostname, authToken string) (string, error) {
254 query := `query UserCurrent{viewer{login}}`
255 reqBody, err := json.Marshal(map[string]interface{}{"query": query})
256 if err != nil {
257 return "", err
258 }
259 result := struct {
260 Data struct{ Viewer struct{ Login string } }
261 }{}
262 apiEndpoint, err := safeurl.JoinPathWithHostPrefix(ghinstance.GraphQLEndpoint(hostname))
263 if err != nil {
264 return "", err
265 }
266 req, err := http.NewRequest("POST", apiEndpoint.String(), bytes.NewBuffer(reqBody))
267 if err != nil {
268 return "", err
269 }
270 req.Header.Set("Authorization", "token "+authToken)
271 res, err := httpClient.Do(req)
272 if err != nil {
273 return "", err
274 }
275 defer res.Body.Close()
276 if res.StatusCode > 299 {
277 return "", api.HandleHTTPError(res)
278 }
279 decoder := json.NewDecoder(res.Body)
280 err = decoder.Decode(&result)
281 if err != nil {
282 return "", err
283 }
284 return result.Data.Viewer.Login, nil
285}

Callers 2

loginRunFunction · 0.92
LoginFunction · 0.85

Calls 7

JoinPathWithHostPrefixFunction · 0.92
GraphQLEndpointFunction · 0.92
HandleHTTPErrorFunction · 0.92
StringMethod · 0.65
SetMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected