github.com/chelnak/go-gh@v0.0.2/example_gh_test.go (about) 1 package gh 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "time" 8 9 "github.com/chelnak/go-gh/pkg/api" 10 graphql "github.com/cli/shurcooL-graphql" 11 ) 12 13 // Execute 'gh issue list -R cli/cli', and print the output. 14 func ExampleExec() { 15 args := []string{"issue", "list", "-R", "cli/cli"} 16 stdOut, stdErr, err := Exec(args...) 17 if err != nil { 18 log.Fatal(err) 19 } 20 fmt.Println(stdOut.String()) 21 fmt.Println(stdErr.String()) 22 } 23 24 // Get tags from cli/cli repository using REST API. 25 func ExampleRESTClient_simple() { 26 client, err := RESTClient(nil) 27 if err != nil { 28 log.Fatal(err) 29 } 30 response := []struct{ Name string }{} 31 err = client.Get("repos/cli/cli/tags", &response) 32 if err != nil { 33 log.Fatal(err) 34 } 35 fmt.Println(response) 36 } 37 38 // Get tags from cli/cli repository using REST API. 39 // Specifying host, auth token, headers and logging to stdout. 40 func ExampleRESTClient_advanced() { 41 opts := api.ClientOptions{ 42 Host: "github.com", 43 AuthToken: "xxxxxxxxxx", // Replace with valid auth token. 44 Headers: map[string]string{"Time-Zone": "America/Los_Angeles"}, 45 Log: os.Stdout, 46 } 47 client, err := RESTClient(&opts) 48 if err != nil { 49 log.Fatal(err) 50 } 51 response := []struct{ Name string }{} 52 err = client.Get("repos/cli/cli/tags", &response) 53 if err != nil { 54 log.Fatal(err) 55 } 56 fmt.Println(response) 57 } 58 59 // Query tags from cli/cli repository using GQL API. 60 func ExampleGQLClient_simple() { 61 client, err := GQLClient(nil) 62 if err != nil { 63 log.Fatal(err) 64 } 65 var query struct { 66 Repository struct { 67 Refs struct { 68 Nodes []struct { 69 Name string 70 } 71 } `graphql:"refs(refPrefix: $refPrefix, last: $last)"` 72 } `graphql:"repository(owner: $owner, name: $name)"` 73 } 74 variables := map[string]interface{}{ 75 "refPrefix": graphql.String("refs/tags/"), 76 "last": graphql.Int(30), 77 "owner": graphql.String("cli"), 78 "name": graphql.String("cli"), 79 } 80 err = client.Query("RepositoryTags", &query, variables) 81 if err != nil { 82 log.Fatal(err) 83 } 84 fmt.Println(query) 85 } 86 87 // Query tags from cli/cli repository using GQL API. 88 // Enable caching and request timeout. 89 func ExampleGQLClient_advanced() { 90 opts := api.ClientOptions{ 91 EnableCache: true, 92 Timeout: 5 * time.Second, 93 } 94 client, err := GQLClient(&opts) 95 if err != nil { 96 log.Fatal(err) 97 } 98 var query struct { 99 Repository struct { 100 Refs struct { 101 Nodes []struct { 102 Name string 103 } 104 } `graphql:"refs(refPrefix: $refPrefix, last: $last)"` 105 } `graphql:"repository(owner: $owner, name: $name)"` 106 } 107 variables := map[string]interface{}{ 108 "refPrefix": graphql.String("refs/tags/"), 109 "last": graphql.Int(30), 110 "owner": graphql.String("cli"), 111 "name": graphql.String("cli"), 112 } 113 err = client.Query("RepositoryTags", &query, variables) 114 if err != nil { 115 log.Fatal(err) 116 } 117 fmt.Println(query) 118 } 119 120 // Get repository for the current directory. 121 func ExampleCurrentRepository() { 122 repo, err := CurrentRepository() 123 if err != nil { 124 log.Fatal(err) 125 } 126 fmt.Printf("%s/%s/%s\n", repo.Host(), repo.Owner(), repo.Name()) 127 }