cli / go-gh

A Go module for interacting with gh and the GitHub API from the command line.
https://pkg.go.dev/github.com/cli/go-gh/v2
MIT License
322 stars 45 forks source link

Expose GetHTTPClient method #14

Closed chelnak closed 2 years ago

chelnak commented 2 years ago

Closes #13.

The idea behind this is that a developer can opt to configure another library (e.g go-github) with a http client instance that has been configured to use gh-cli auth.

For example:

func NewClient() (Client, error) {
    restClient, err := gh.RESTClient(nil)
    if err != nil {
        return nil, err
    }

        // This is https://github.com/google/go-github
    g := github.NewClient(restClient.GetHttpClient())

    currentRepository, err := gh.CurrentRepository()
    if err != nil {
        return nil, err
    }

    client := client{GitHub: g, owner: currentRepository.Owner(), repo: currentRepository.Name()}
    return client, nil
}

Not sure if this implementation is of any use to you but thought i'd PR it as i've added it to my fork.

Thanks!