From a6aeb03a6d6d1f11c970e77b087edec57e8cdcd9 Mon Sep 17 00:00:00 2001 From: bndw Date: Mon, 27 Mar 2023 20:42:09 -0700 Subject: [PATCH] feat: publish event json from file Adds an optional `--file` flag to publish a json event from file. This can be useful as a dev tool when combined with `noscl sign`. --- main.go | 6 ++--- publish.go | 77 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/main.go b/main.go index a0996e9..9096e8b 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ Usage: noscl sign noscl verify noscl public - noscl publish [--reference=...] [--profile=...] + noscl publish [--reference=...] [--profile=...] [--file=] [] noscl message [--reference=...] noscl metadata --name= [--about=] [--picture=] [--nip05=] [--banner=] [--displayname=] [--lud16=] [--username=] [--website=] noscl profile [--verbose] [--json] @@ -76,8 +76,8 @@ func main() { switch { case opts["home"].(bool): home(opts, false) - case opts["inbox"].(bool): - home(opts, true) + case opts["inbox"].(bool): + home(opts, true) case opts["setprivate"].(bool): // TODO make this read STDIN and encrypt the key locally setPrivateKey(opts) diff --git a/publish.go b/publish.go index 84ea782..61ca53d 100644 --- a/publish.go +++ b/publish.go @@ -1,8 +1,10 @@ package main import ( + "encoding/json" "errors" "log" + "os" "time" "github.com/docopt/docopt-go" @@ -17,45 +19,66 @@ func publish(opts docopt.Opts) { initNostr() - var tags nostr.Tags + var event nostr.Event - references, err := optSlice(opts, "--reference") - if err != nil { - return - } + if file, _ := opts.String("--file"); file != "" { + jsonb, err := os.ReadFile(file) + if err != nil { + log.Printf("Failed reading content from file: %s", err) + return + } + if err := json.Unmarshal(jsonb, &event); err != nil { + log.Printf("Failed unmarshaling json from file: %s", err) + return + } + } else { + references, err := optSlice(opts, "--reference") + if err != nil { + return + } - for _, ref := range references { - tags = append(tags, nostr.Tag{"e", ref}) - } + var tags nostr.Tags + for _, ref := range references { + tags = append(tags, nostr.Tag{"e", ref}) + } - profiles, err := optSlice(opts, "--profile") - if err != nil { - return - } + profiles, err := optSlice(opts, "--profile") + if err != nil { + return + } - for _, profile := range profiles { - tags = append(tags, nostr.Tag{"p", profile}) - } + for _, profile := range profiles { + tags = append(tags, nostr.Tag{"p", profile}) + } - content := opts[""].(string) - if content == "-" { - content, err = readContentStdin(4096) - if err != nil { - log.Printf("Failed reading content from stdin: %s", err) + content, _ := opts.String("") + if content == "" { + log.Printf("Content must not be empty") + return + } + if content == "-" { + content, err = readContentStdin(4096) + if err != nil { + log.Printf("Failed reading content from stdin: %s", err) + return + } + } + + event = nostr.Event{ + CreatedAt: time.Now(), + Kind: nostr.KindTextNote, + Tags: tags, + Content: content, } } - event, statuses, err := pool.PublishEvent(&nostr.Event{ - CreatedAt: time.Now(), - Kind: nostr.KindTextNote, - Tags: tags, - Content: content, - }) + + publishEvent, statuses, err := pool.PublishEvent(&event) if err != nil { log.Printf("Error publishing: %s.\n", err.Error()) return } - printPublishStatus(event, statuses) + printPublishStatus(publishEvent, statuses) } func optSlice(opts docopt.Opts, key string) ([]string, error) {