support reading from stdin.

This commit is contained in:
fiatjaf 2023-10-20 20:57:29 -03:00
parent 459b127988
commit 208d909727
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
3 changed files with 35 additions and 6 deletions

View File

@ -31,7 +31,7 @@ var encode = &cli.Command{
Name: "npub",
Usage: "encode a hex private key into bech32 'npub' format",
Action: func(c *cli.Context) error {
target := c.Args().First()
target := getStdinOrFirstArgument(c)
if err := validate32BytesHex(target); err != nil {
return err
}
@ -48,7 +48,7 @@ var encode = &cli.Command{
Name: "nsec",
Usage: "encode a hex private key into bech32 'nsec' format",
Action: func(c *cli.Context) error {
target := c.Args().First()
target := getStdinOrFirstArgument(c)
if err := validate32BytesHex(target); err != nil {
return err
}
@ -72,7 +72,7 @@ var encode = &cli.Command{
},
},
Action: func(c *cli.Context) error {
target := c.Args().First()
target := getStdinOrFirstArgument(c)
if err := validate32BytesHex(target); err != nil {
return err
}
@ -105,7 +105,7 @@ var encode = &cli.Command{
},
},
Action: func(c *cli.Context) error {
target := c.Args().First()
target := getStdinOrFirstArgument(c)
if err := validate32BytesHex(target); err != nil {
return err
}
@ -191,7 +191,7 @@ var encode = &cli.Command{
Name: "note",
Usage: "generate note1 event codes (not recommended)",
Action: func(c *cli.Context) error {
target := c.Args().First()
target := getStdinOrFirstArgument(c)
if err := validate32BytesHex(target); err != nil {
return err
}

View File

@ -17,7 +17,7 @@ var fetch = &cli.Command{
ArgsUsage: "[nip19code]",
Action: func(c *cli.Context) error {
filter := nostr.Filter{}
code := c.Args().First()
code := getStdinOrFirstArgument(c)
prefix, value, err := nip19.Decode(code)
if err != nil {

29
helpers.go Normal file
View File

@ -0,0 +1,29 @@
package main
import (
"bytes"
"io"
"os"
"github.com/urfave/cli/v2"
)
func getStdin() string {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
read := bytes.NewBuffer(make([]byte, 0, 1000))
_, err := io.Copy(read, os.Stdin)
if err == nil {
return read.String()
}
}
return ""
}
func getStdinOrFirstArgument(c *cli.Context) string {
target := c.Args().First()
if target != "" {
return target
}
return getStdin()
}