~emersion/public-inbox

hut: pages: Add list v1 SUPERSEDED

Thorben Günther: 1
 pages: Add list

 4 files changed, 51 insertions(+), 0 deletions(-)
#654470 .build.yml failed
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/public-inbox/patches/27583/mbox | git am -3
Learn more about email & git

[PATCH hut] pages: Add list Export this patch

---
All those patches more or less depend on each other for a clean apply.
I don't really like the output format right now, but have no idea how to
improve it. The updated timestamp seems broken at the moment and always
returns Updated: 0001-01-01 00:00:00 +0000 UTC for me.
In the future I would like to add a second output format (maybe json?)
so that the output can be better processed in scripts - I really miss
that in srhtctl as well.
What is still missing in this patch is cursor handling. What do you have
planned? In srhtctl I currently iterate over them, but that could end in
API abuse. Otherwise we could introduce a cursor flag.
 doc/hut.1.scd                     |  3 +++
 pages.go                          | 28 ++++++++++++++++++++++++++++
 srht/pagessrht/gql.go             | 10 ++++++++++
 srht/pagessrht/operations.graphql | 10 ++++++++++
 4 files changed, 51 insertions(+)

diff --git a/doc/hut.1.scd b/doc/hut.1.scd
index fb4016a..3ebd5b9 100644
--- a/doc/hut.1.scd
+++ b/doc/hut.1.scd
@@ -80,6 +80,9 @@ hut is a CLI companion utility to interact with sr.ht.
	*-p, --protocol* <string>
		Protocol to use (either HTTPS or GEMINI; defaults to HTTPS)

*list*
	List registered sites.

# CONFIGURATION

Generate a new OAuth2 access token on _meta.sr.ht_.
diff --git a/pages.go b/pages.go
index 2cda8c6..654c224 100644
--- a/pages.go
+++ b/pages.go
@@ -19,6 +19,7 @@ func newPagesCommand() *cobra.Command {
	}
	cmd.AddCommand(newPagesPublishCommand())
	cmd.AddCommand(newPagesUnpublishCommand())
	cmd.AddCommand(newPagesListCommand())
	return cmd
}

@@ -105,6 +106,33 @@ func newPagesUnpublishCommand() *cobra.Command {
	return cmd
}

func newPagesListCommand() *cobra.Command {
	run := func(cmd *cobra.Command, args []string) {
		ctx := cmd.Context()

		c := createClient("pages")

		sites, err := pagessrht.Sites(c.Client, ctx, nil)
		if err != nil {
			log.Fatalf("failed to list sites: %v", err)
		}

		if sites.Cursor != nil {
			fmt.Println(*sites.Cursor)
		}
		for _, site := range sites.Results {
			fmt.Printf("Protocol: %s\tDomain: %s\n", site.Protocol, site.Domain)
		}
	}

	cmd := &cobra.Command{
		Use:   "list",
		Short: "List registered sites",
		Run:   run,
	}
	return cmd
}

func getProtocol(protocol string) (pagessrht.Protocol, error) {
	switch strings.ToLower(protocol) {
	case "https":
diff --git a/srht/pagessrht/gql.go b/srht/pagessrht/gql.go
index c136502..397fe24 100644
--- a/srht/pagessrht/gql.go
+++ b/srht/pagessrht/gql.go
@@ -94,3 +94,13 @@ func Unpublish(client *gqlclient.Client, ctx context.Context, domain string, pro
	err = client.Execute(ctx, op, &respData)
	return respData.Unpublish, err
}

func Sites(client *gqlclient.Client, ctx context.Context, cursor *Cursor) (sites SiteCursor, err error) {
	op := gqlclient.NewOperation("query sites ($cursor: Cursor) {\n\tsites(cursor: $cursor) {\n\t\tcursor\n\t\tresults {\n\t\t\tdomain\n\t\t\tprotocol\n\t\t}\n\t}\n}\n")
	op.Var("cursor", cursor)
	var respData struct {
		Sites SiteCursor
	}
	err = client.Execute(ctx, op, &respData)
	return respData.Sites, err
}
diff --git a/srht/pagessrht/operations.graphql b/srht/pagessrht/operations.graphql
index 2ab7ffd..7ee2b1d 100644
--- a/srht/pagessrht/operations.graphql
+++ b/srht/pagessrht/operations.graphql
@@ -9,3 +9,13 @@ mutation unpublish($domain: String!, $protocol: Protocol) {
        domain
    }
}

query sites($cursor: Cursor) {
    sites(cursor: $cursor) {
        cursor
        results {
            domain
            protocol
        }
    }
}
--
2.34.1
hut/patches/.build.yml: FAILED in 22s

[pages: Add list][0] from [Thorben Günther][1]

[0]: https://lists.sr.ht/~emersion/public-inbox/patches/27583
[1]: mailto:admin@xenrox.net

✗ #654470 FAILED hut/patches/.build.yml https://builds.sr.ht/~emersion/job/654470
> I don't really like the output format right now, but have no idea how to
> improve it.
I think I'd start with something simple at first, like "<domain> (<protocol>)".
It would be nice to add formatting in the future (e.g. bold and/or colors).