~emersion/hut

d1306f1798f6d279ec88ca60e74ff53dd1862f05 — Simon Ser 7 months ago 940c6d7
Add basic graphql command

References: https://todo.sr.ht/~emersion/hut/1
3 files changed, 64 insertions(+), 0 deletions(-)

M doc/hut.1.scd
A graphql.go
M main.go
M doc/hut.1.scd => doc/hut.1.scd +16 -0
@@ 23,6 23,22 @@ hut is a CLI companion utility to interact with sr.ht.
*help* <command>
	Help about any command.

*graphql* <service>
	Read a GraphQL query from stdin and execute it. The JSON response is
	written to stdout. _service_ is the sr.ht service to execute to query on
	(for instance "meta" or "builds").

	A tool like *jq*(1) can be used to prettify the output and process the
	data. Example:

	```
	hut graphql meta <<EOF | jq '.me'
	query {
		me { canonicalName }
	}
	EOF
	```

## git

*artifact* <filename> [options...]

A graphql.go => graphql.go +47 -0
@@ 0,0 1,47 @@
package main

import (
	"encoding/json"
	"io"
	"log"
	"os"

	"git.sr.ht/~emersion/gqlclient"
	"github.com/spf13/cobra"
)

func newGraphqlCommand() *cobra.Command {
	run := func(cmd *cobra.Command, args []string) {
		service := args[0]

		ctx := cmd.Context()
		c := createClient(service)

		b, err := io.ReadAll(os.Stdin)
		if err != nil {
			log.Fatalf("failed to read GraphQL query: %v", err)
		}
		query := string(b)

		op := gqlclient.NewOperation(query)
		var data json.RawMessage
		if err := c.Execute(ctx, op, &data); err != nil {
			log.Fatal(err)
		}

		enc := json.NewEncoder(os.Stdout)
		enc.SetIndent("", "  ")
		if err := enc.Encode(data); err != nil {
			log.Fatalf("failed to write JSON response: %v", err)
		}
	}

	cmd := &cobra.Command{
		Use:   "graphql <service>",
		Short: "Execute a GraphQL query",
		Args:  cobra.ExactArgs(1),
		Run:   run,
	}
	// TODO: variables
	return cmd
}

M main.go => main.go +1 -0
@@ 17,6 17,7 @@ func main() {
	}
	cmd.AddCommand(newBuildsCommand())
	cmd.AddCommand(newGitCommand())
	cmd.AddCommand(newGraphqlCommand())
	cmd.AddCommand(newPasteCommand())
	cmd.AddCommand(newPagesCommand())