~emersion/public-inbox

hut: paste: Add option to set visibility v1 APPLIED

Thorben Günther: 1
 paste: Add option to set visibility

 4 files changed, 40 insertions(+), 9 deletions(-)
#654314 .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/27571/mbox | git am -3
Learn more about email & git

[PATCH hut] paste: Add option to set visibility Export this patch

---
 doc/hut.1.scd                     |  5 +++++
 paste.go                          | 27 +++++++++++++++++++++++++--
 srht/pastesrht/gql.go             |  5 +++--
 srht/pastesrht/operations.graphql | 12 +++++++-----
 4 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/doc/hut.1.scd b/doc/hut.1.scd
index 22b6361..fb4016a 100644
--- a/doc/hut.1.scd
+++ b/doc/hut.1.scd
@@ -51,6 +51,11 @@ hut is a CLI companion utility to interact with sr.ht.
*create* <filenames...>
	Create a new paste.

	Options are:

	*-v, --visibility* <string>
		Visibility to use (public, unlisted, private)

## pages

*publish* <tarball> [options...]
diff --git a/paste.go b/paste.go
index 5b1a9c0..40637d2 100644
--- a/paste.go
+++ b/paste.go
@@ -6,6 +6,7 @@ import (
	"mime"
	"os"
	"path/filepath"
	"strings"

	"git.sr.ht/~emersion/gqlclient"
	"github.com/spf13/cobra"
@@ -23,8 +24,15 @@ func newPasteCommand() *cobra.Command {
}

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

		pasteVisibility, err := getVisibility(visibility)
		if err != nil {
			log.Fatal(err)
		}

		c := createClient("paste")

		var files []gqlclient.Upload
@@ -55,7 +63,7 @@ func newPasteCreateCommand() *cobra.Command {
			})
		}

		paste, err := pastesrht.CreatePaste(c.Client, ctx, files)
		paste, err := pastesrht.CreatePaste(c.Client, ctx, files, pasteVisibility)
		if err != nil {
			log.Fatal(err)
		}
@@ -63,9 +71,24 @@ func newPasteCreateCommand() *cobra.Command {
		fmt.Printf("%v/%v/%v\n", c.BaseURL, paste.User.CanonicalName, paste.Id)
	}

	return &cobra.Command{
	cmd := &cobra.Command{
		Use:   "create [filenames...]",
		Short: "Create a new paste",
		Run:   run,
	}
	cmd.Flags().StringVarP(&visibility, "visibility", "v", "unlisted", "paste visibility")
	return cmd
}

func getVisibility(visibility string) (pastesrht.Visibility, error) {
	switch strings.ToLower(visibility) {
	case "unlisted":
		return pastesrht.VisibilityUnlisted, nil
	case "private":
		return pastesrht.VisibilityPrivate, nil
	case "public":
		return pastesrht.VisibilityPublic, nil
	default:
		return "", fmt.Errorf("invalid visibility: %s", visibility)
	}
}
diff --git a/srht/pastesrht/gql.go b/srht/pastesrht/gql.go
index 4fb857f..81b5471 100644
--- a/srht/pastesrht/gql.go
+++ b/srht/pastesrht/gql.go
@@ -75,9 +75,10 @@ const (
	VisibilityPrivate  Visibility = "PRIVATE"
)

func CreatePaste(client *gqlclient.Client, ctx context.Context, files []gqlclient.Upload) (create Paste, err error) {
	op := gqlclient.NewOperation("mutation createPaste ($files: [Upload!]!) {\n\tcreate(files: $files, visibility: UNLISTED) {\n\t\tid\n\t\tuser {\n\t\t\tcanonicalName\n\t\t}\n\t}\n}\n")
func CreatePaste(client *gqlclient.Client, ctx context.Context, files []gqlclient.Upload, visibility Visibility) (create Paste, err error) {
	op := gqlclient.NewOperation("mutation createPaste ($files: [Upload!]!, $visibility: Visibility!) {\n\tcreate(files: $files, visibility: $visibility) {\n\t\tid\n\t\tuser {\n\t\t\tcanonicalName\n\t\t}\n\t}\n}\n")
	op.Var("files", files)
	op.Var("visibility", visibility)
	var respData struct {
		Create Paste
	}
diff --git a/srht/pastesrht/operations.graphql b/srht/pastesrht/operations.graphql
index 42fb77f..a813865 100644
--- a/srht/pastesrht/operations.graphql
+++ b/srht/pastesrht/operations.graphql
@@ -1,6 +1,8 @@
mutation createPaste($files: [Upload!]!) {
	create(files: $files, visibility: UNLISTED) {
		id
		user { canonicalName }
	}
mutation createPaste($files: [Upload!]!, $visibility: Visibility!) {
    create(files: $files, visibility: $visibility) {
        id
        user {
            canonicalName
        }
    }
}
--
2.34.1
hut/patches/.build.yml: FAILED in 25s

[paste: Add option to set visibility][0] from [Thorben Günther][1]

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

✗ #654314 FAILED hut/patches/.build.yml https://builds.sr.ht/~emersion/job/654314
Pushed, thanks!