/
example_gh_test.go
184 lines (168 loc) · 4.31 KB
/
example_gh_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package gh
import (
"fmt"
"io"
"log"
"os"
"time"
"github.com/cli/go-gh/pkg/api"
"github.com/cli/go-gh/pkg/tableprinter"
"github.com/cli/go-gh/pkg/term"
graphql "github.com/cli/shurcooL-graphql"
)
// Execute 'gh issue list -R cli/cli', and print the output.
func ExampleExec() {
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())
}
// Get tags from cli/cli repository using REST API.
func ExampleRESTClient_simple() {
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)
}
// Get tags from cli/cli repository using REST API.
// Specifying host, auth token, headers and logging to stdout.
func ExampleRESTClient_advanced() {
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)
}
// Get release asset from cli/cli repository using REST API.
func ExampleRESTClient_request() {
opts := api.ClientOptions{
Headers: map[string]string{"Accept": "application/octet-stream"},
}
client, err := RESTClient(&opts)
if err != nil {
log.Fatal(err)
}
// URL to cli/cli release v2.14.2 checksums.txt
assetURL := "repos/cli/cli/releases/assets/71589494"
resp, err := client.Request("GET", assetURL, nil)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
f, err := os.CreateTemp("", "*_checksums.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Asset downloaded to %s\n", f.Name())
}
// Query tags from cli/cli repository using GQL API.
func ExampleGQLClient_simple() {
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)
}
// Query tags from cli/cli repository using GQL API.
// Enable caching and request timeout.
func ExampleGQLClient_advanced() {
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)
}
// Get repository for the current directory.
func ExampleCurrentRepository() {
repo, err := CurrentRepository()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s/%s/%s\n", repo.Host(), repo.Owner(), repo.Name())
}
// Print tabular data to a terminal or in machine-readable format for scripts.
func ExampleTablePrinter() {
terminal := term.FromEnv()
termWidth, _, _ := terminal.Size()
t := tableprinter.New(terminal.Out(), terminal.IsTerminalOutput(), termWidth)
red := func(s string) string {
return "\x1b[31m" + s + "\x1b[m"
}
// add a field that will render with color and will not be auto-truncated
t.AddField("1", tableprinter.WithColor(red), tableprinter.WithTruncate(nil))
t.AddField("hello")
t.EndRow()
t.AddField("2")
t.AddField("world")
t.EndRow()
if err := t.Render(); err != nil {
log.Fatal(err)
}
}