Documentation ¶
Overview ¶
Package gh is a library for CLI Go applications to help interface with the gh CLI tool, and the GitHub API.
Note that the examples in this package assume gh and git are installed. They do not run in the Go Playground used by pkg.go.dev.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Exec ¶
Exec gh command with provided arguments.
Example ¶
Execute 'gh issue list -R cli/cli', and print the output.
args := []string{"issue", "list", "-R", "cli/cli"} stdOut, stdErr, err := Exec(args...) if err != nil { log.Fatal(err) } fmt.Println(stdOut.String()) fmt.Println(stdErr.String())
Output:
func GQLClient ¶
func GQLClient(opts *api.ClientOptions) (api.GQLClient, error)
GQLClient builds a client to send requests to GitHub GraphQL API endpoints. As part of the configuration a hostname, auth token, and default set of headers are resolved from the gh environment configuration. These behaviors can be overridden using the opts argument.
Example (Advanced) ¶
Query tags from cli/cli repository using GQL API. Enable caching and request timeout.
opts := api.ClientOptions{ EnableCache: true, Timeout: 5 * time.Second, } client, err := GQLClient(&opts) if err != nil { log.Fatal(err) } var query struct { Repository struct { Refs struct { Nodes []struct { Name string } } `graphql:"refs(refPrefix: $refPrefix, last: $last)"` } `graphql:"repository(owner: $owner, name: $name)"` } variables := map[string]interface{}{ "refPrefix": graphql.String("refs/tags/"), "last": graphql.Int(30), "owner": graphql.String("cli"), "name": graphql.String("cli"), } err = client.Query("RepositoryTags", &query, variables) if err != nil { log.Fatal(err) } fmt.Println(query)
Output:
Example (Simple) ¶
Query tags from cli/cli repository using GQL API.
client, err := GQLClient(nil) if err != nil { log.Fatal(err) } var query struct { Repository struct { Refs struct { Nodes []struct { Name string } } `graphql:"refs(refPrefix: $refPrefix, last: $last)"` } `graphql:"repository(owner: $owner, name: $name)"` } variables := map[string]interface{}{ "refPrefix": graphql.String("refs/tags/"), "last": graphql.Int(30), "owner": graphql.String("cli"), "name": graphql.String("cli"), } err = client.Query("RepositoryTags", &query, variables) if err != nil { log.Fatal(err) } fmt.Println(query)
Output:
func RESTClient ¶
func RESTClient(opts *api.ClientOptions) (api.RESTClient, error)
RESTClient builds a client to send requests to GitHub REST API endpoints. As part of the configuration a hostname, auth token, and default set of headers are resolved from the gh environment configuration. These behaviors can be overridden using the opts argument.
Example (Advanced) ¶
Get tags from cli/cli repository using REST API. Specifying host, auth token, headers and logging to stdout.
opts := api.ClientOptions{ Host: "github.com", AuthToken: "xxxxxxxxxx", // Replace with valid auth token. Headers: map[string]string{"Time-Zone": "America/Los_Angeles"}, Log: os.Stdout, } client, err := RESTClient(&opts) if err != nil { log.Fatal(err) } response := []struct{ Name string }{} err = client.Get("repos/cli/cli/tags", &response) if err != nil { log.Fatal(err) } fmt.Println(response)
Output:
Example (Simple) ¶
Get tags from cli/cli repository using REST API.
client, err := RESTClient(nil) if err != nil { log.Fatal(err) } response := []struct{ Name string }{} err = client.Get("repos/cli/cli/tags", &response) if err != nil { log.Fatal(err) } fmt.Println(response)
Output:
Types ¶
type Repository ¶
Repository is the interface that wraps repository information methods.
func CurrentRepository ¶
func CurrentRepository() (Repository, error)
CurrentRepository uses git remotes to determine the GitHub repository the current directory is tracking.
Example ¶
Get repository for the current directory.
repo, err := CurrentRepository() if err != nil { log.Fatal(err) } fmt.Printf("%s/%s/%s\n", repo.Host(), repo.Owner(), repo.Name())
Output: