From df147271d945ce70e8c74d5e9a8d6543d68a6f2d Mon Sep 17 00:00:00 2001 From: Sebastien Vermeille Date: Thu, 21 Sep 2023 16:47:55 +0200 Subject: [PATCH] Add the client --- cirrusci_client.go => generated.go | 2 +- genqlient.yaml | 4 +- main.go | 67 ++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 6 deletions(-) rename cirrusci_client.go => generated.go (98%) diff --git a/cirrusci_client.go b/generated.go similarity index 98% rename from cirrusci_client.go rename to generated.go index b39d838..3c9834b 100644 --- a/cirrusci_client.go +++ b/generated.go @@ -1,6 +1,6 @@ // Code generated by github.com/Khan/genqlient, DO NOT EDIT. -package client +package cirrus import ( "context" diff --git a/genqlient.yaml b/genqlient.yaml index 625e6e5..57647dd 100644 --- a/genqlient.yaml +++ b/genqlient.yaml @@ -1,9 +1,9 @@ schema: resources/graphql/schema/cirrus-ci.schema.graphql operations: - resources/graphql/client/queries.graphql -generated: cirrusci_client.go +generated: generated.go ## needed since it doesn't match the directory name: -package: client +package: cirrus # We bind github's DateTime scalar type to Go's time.Time (which conveniently # already defines MarshalJSON and UnmarshalJSON). This means genqlient will diff --git a/main.go b/main.go index eb5fdc3..eb1a4e2 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,68 @@ -package client +package cirrus -import "fmt" +import ( + "context" + "fmt" + "net/http" + "os" + + "github.com/Khan/genqlient/graphql" +) + +type authedTransport struct { + key string + wrapped http.RoundTripper +} + +func (t *authedTransport) RoundTrip(req *http.Request) (*http.Response, error) { + req.Header.Set("Authorization", "bearer "+t.key) + return t.wrapped.RoundTrip(req) +} func main() { - fmt.Println("Hi, DEV World! 😉") + var err error + defer func() { + if err != nil { + fmt.Println(err) + os.Exit(1) + } + }() + + key := os.Getenv("GITHUB_TOKEN") + if key == "" { + err = fmt.Errorf("must set GITHUB_TOKEN=") + return + } + + httpClient := http.Client{ + Transport: &authedTransport{ + key: key, + wrapped: http.DefaultTransport, + }, + } + graphqlClient := graphql.NewClient("https://api.cirrus-ci.com/graphql", &httpClient) + + switch len(os.Args) { + case 1: + var viewerResp *getViewerResponse + viewerResp, err = getViewer(context.Background(), graphqlClient) + if err != nil { + return + } + fmt.Println("you are", viewerResp.Viewer.Id) + + //case 2: + // username := os.Args[1] + // var userResp *getUserResponse + // userResp, err = getUser(context.Background(), graphqlClient, username) + // if err != nil { + // return + // } + // fmt.Println(username, "is", userResp.User.TheirName, "created on", userResp.User.CreatedAt.Format("2006-01-02")) + + default: + err = fmt.Errorf("usage: %v [username]", os.Args[0]) + } } + +//go:generate go run github.com/Khan/genqlient genqlient.yaml