MCPcopy
hub / github.com/cli/cli / GetCurrentLogin

Function GetCurrentLogin

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

Source from the content-addressed store, hash-verified

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

Callers 2

loginRunFunction · 0.92
LoginFunction · 0.85

Calls 5

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

Tested by

no test coverage detected