~w1ke/hut

797cffae85bf201f7d01404801f9cff5ee7f440b — w1ke 11 months ago 3da5f7b
changed colors, added meta config subcommand
4 files changed, 88 insertions(+), 8 deletions(-)

M api/meta/meta.go
M cmd/hut/meta.go
M go.mod
M go.sum
M api/meta/meta.go => api/meta/meta.go +43 -0
@@ 96,3 96,46 @@ func (c *Client) User(user string) (*UserInfo, error) {
	}
	return &userInfo, nil
}

type ConfigOptions struct {
	Url      *string
	Location *string
	Bio      *string
	Email    *string
}

func (c *Client) Config(cfg ConfigOptions) error {
	src := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: c.token},
	)
	httpClient := oauth2.NewClient(context.Background(), src)

	client := graphql.NewClient("https://meta.sr.ht/query", httpClient)

	if cfg.Url == nil && cfg.Location == nil && cfg.Bio == nil && cfg.Email == nil {
		return errors.New("no mutation occurred")
	}

	var mutation struct {
		UpdateUser UserInfo `graphql:"updateUser(input: $input)"`
	}

	type UserInput struct {
		Url      *graphql.String `json:"url,omitempty"`
		Location *graphql.String `json:"location,omitempty"`
		Bio      *graphql.String `json:"bio,omitempty"`
		Email    *graphql.String `json:"email,omitempty"`
	}

	variables := map[string]interface{}{
		"input": &UserInput{
			Url:      (*graphql.String)(cfg.Url),
			Location: (*graphql.String)(cfg.Location),
			Bio:      (*graphql.String)(cfg.Bio),
			Email:    (*graphql.String)(cfg.Email),
		},
	}

	err := client.Mutate(context.Background(), &mutation, variables)
	return err
}

M cmd/hut/meta.go => cmd/hut/meta.go +40 -5
@@ 1,6 1,7 @@
package main

import (
	"flag"
	"fmt"
	"log"
	"os"


@@ 22,22 23,56 @@ func metaRoot(args []string) {
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(color.GreenString(userInfo.CanonicalName), "  ", color.BlueString("<"+userInfo.Email+">"))
		if userInfo.Url != nil {
		fmt.Println(color.GreenString(userInfo.CanonicalName + "  " + "<" + userInfo.Email + ">"))
		if userInfo.Url != nil && *userInfo.Url != "" {
			fmt.Println(color.BlueString("=> " + *userInfo.Url))
		}
		if userInfo.Location != nil {
		if userInfo.Location != nil && *userInfo.Location != "" {
			fmt.Println(color.YellowString("@ " + *userInfo.Location))
		}
		if userInfo.Bio != nil {
			fmt.Println(color.HiBlackString(*userInfo.Bio))
		if userInfo.Bio != nil && *userInfo.Bio != "" {
			fmt.Println("\n" + color.HiBlackString(*userInfo.Bio))
		}
	case "config":
		flagSet := flag.NewFlagSet("hut meta", flag.ExitOnError)
		var (
			url      = flagSet.String("url", "", "url of user")
			location = flagSet.String("location", "", "location of user")
			bio      = flagSet.String("bio", "", "bio of user")
			email    = flagSet.String("email", "", "email of user")
		)
		flagSet.Parse(args[1:])
		var cfg meta.ConfigOptions
		if isFlagPassed(flagSet, "url") {
			cfg.Url = url
		}
		if isFlagPassed(flagSet, "location") {
			cfg.Location = location
		}
		if isFlagPassed(flagSet, "bio") {
			cfg.Bio = bio
		}
		if isFlagPassed(flagSet, "email") {
			cfg.Email = email
		}
		err := client.Config(cfg)
		if err != nil {
			log.Fatal(err)
		}
	default:
		metaUsage()
	}
}

func isFlagPassed(flagSet *flag.FlagSet, name string) (found bool) {
	flagSet.Visit(func(f *flag.Flag) {
		if f.Name == name {
			found = true
		}
	})
	return found
}

func metaUsage() {
	fmt.Fprintln(os.Stderr, `hut meta - account & security


M go.mod => go.mod +3 -3
@@ 3,8 3,8 @@ module git.sr.ht/~w1ke/hut
go 1.16

require (
	github.com/fatih/color v1.12.0 // indirect
	github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
	github.com/fatih/color v1.12.0
	github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a
	golang.org/x/net v0.0.0-20210521195947-fe42d452be8f // indirect
	golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
	golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
)

M go.sum => go.sum +2 -0
@@ 82,6 82,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=


@@ 283,6 284,7 @@ golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=