mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 16:19:07 -05:00
support reading from stdin.
This commit is contained in:
parent
459b127988
commit
208d909727
10
encode.go
10
encode.go
|
@ -31,7 +31,7 @@ var encode = &cli.Command{
|
||||||
Name: "npub",
|
Name: "npub",
|
||||||
Usage: "encode a hex private key into bech32 'npub' format",
|
Usage: "encode a hex private key into bech32 'npub' format",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
target := c.Args().First()
|
target := getStdinOrFirstArgument(c)
|
||||||
if err := validate32BytesHex(target); err != nil {
|
if err := validate32BytesHex(target); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ var encode = &cli.Command{
|
||||||
Name: "nsec",
|
Name: "nsec",
|
||||||
Usage: "encode a hex private key into bech32 'nsec' format",
|
Usage: "encode a hex private key into bech32 'nsec' format",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
target := c.Args().First()
|
target := getStdinOrFirstArgument(c)
|
||||||
if err := validate32BytesHex(target); err != nil {
|
if err := validate32BytesHex(target); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ var encode = &cli.Command{
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
target := c.Args().First()
|
target := getStdinOrFirstArgument(c)
|
||||||
if err := validate32BytesHex(target); err != nil {
|
if err := validate32BytesHex(target); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ var encode = &cli.Command{
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
target := c.Args().First()
|
target := getStdinOrFirstArgument(c)
|
||||||
if err := validate32BytesHex(target); err != nil {
|
if err := validate32BytesHex(target); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -191,7 +191,7 @@ var encode = &cli.Command{
|
||||||
Name: "note",
|
Name: "note",
|
||||||
Usage: "generate note1 event codes (not recommended)",
|
Usage: "generate note1 event codes (not recommended)",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
target := c.Args().First()
|
target := getStdinOrFirstArgument(c)
|
||||||
if err := validate32BytesHex(target); err != nil {
|
if err := validate32BytesHex(target); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
2
fetch.go
2
fetch.go
|
@ -17,7 +17,7 @@ var fetch = &cli.Command{
|
||||||
ArgsUsage: "[nip19code]",
|
ArgsUsage: "[nip19code]",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
filter := nostr.Filter{}
|
filter := nostr.Filter{}
|
||||||
code := c.Args().First()
|
code := getStdinOrFirstArgument(c)
|
||||||
|
|
||||||
prefix, value, err := nip19.Decode(code)
|
prefix, value, err := nip19.Decode(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
29
helpers.go
Normal file
29
helpers.go
Normal 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()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user