mirror of https://github.com/fiatjaf/nak.git
nak encode that takes json from stdin.
This commit is contained in:
parent
148f6e8bcb
commit
e91a454fc0
76
encode.go
76
encode.go
|
@ -18,14 +18,68 @@ var encode = &cli.Command{
|
||||||
nak encode nprofile --relay <relay-url> <pubkey-hex>
|
nak encode nprofile --relay <relay-url> <pubkey-hex>
|
||||||
nak encode nevent <event-id>
|
nak encode nevent <event-id>
|
||||||
nak encode nevent --author <pubkey-hex> --relay <relay-url> --relay <other-relay> <event-id>
|
nak encode nevent --author <pubkey-hex> --relay <relay-url> --relay <other-relay> <event-id>
|
||||||
nak encode nsec <privkey-hex>`,
|
nak encode nsec <privkey-hex>
|
||||||
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
|
echo '{"pubkey":"7b225d32d3edb978dba1adfd9440105646babbabbda181ea383f74ba53c3be19","relays":["wss://nada.zero"]}' | nak encode
|
||||||
if c.Args().Len() < 1 {
|
echo '{
|
||||||
return ctx, fmt.Errorf("expected more than 1 argument.")
|
"id":"7b225d32d3edb978dba1adfd9440105646babbabbda181ea383f74ba53c3be19"
|
||||||
}
|
"relays":["wss://nada.zero"],
|
||||||
return ctx, nil
|
"author":"ebb6ff85430705651b311ed51328767078fd790b14f02d22efba68d5513376bc"
|
||||||
|
} | nak encode`,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringSliceFlag{
|
||||||
|
Name: "relay",
|
||||||
|
Aliases: []string{"r"},
|
||||||
|
Usage: "attach relay hints to naddr code",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
DisableSliceFlagSeparator: true,
|
DisableSliceFlagSeparator: true,
|
||||||
|
Action: func(ctx context.Context, c *cli.Command) error {
|
||||||
|
if c.Args().Len() != 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
relays := c.StringSlice("relay")
|
||||||
|
if err := normalizeAndValidateRelayURLs(relays); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hasStdin := false
|
||||||
|
for jsonStr := range getJsonsOrBlank() {
|
||||||
|
if jsonStr == "{}" {
|
||||||
|
hasStdin = false
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
hasStdin = true
|
||||||
|
}
|
||||||
|
|
||||||
|
var eventPtr nostr.EventPointer
|
||||||
|
if err := json.Unmarshal([]byte(jsonStr), &eventPtr); err == nil && eventPtr.ID != nostr.ZeroID {
|
||||||
|
stdout(nip19.EncodeNevent(eventPtr.ID, appendUnique(relays, eventPtr.Relays...), eventPtr.Author))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var profilePtr nostr.ProfilePointer
|
||||||
|
if err := json.Unmarshal([]byte(jsonStr), &profilePtr); err == nil && profilePtr.PublicKey != nostr.ZeroPK {
|
||||||
|
stdout(nip19.EncodeNprofile(profilePtr.PublicKey, appendUnique(relays, profilePtr.Relays...)))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var entityPtr nostr.EntityPointer
|
||||||
|
if err := json.Unmarshal([]byte(jsonStr), &entityPtr); err == nil && entityPtr.PublicKey != nostr.ZeroPK {
|
||||||
|
stdout(nip19.EncodeNaddr(entityPtr.PublicKey, entityPtr.Kind, entityPtr.Identifier, appendUnique(relays, entityPtr.Relays...)))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = lineProcessingError(ctx, "couldn't decode JSON '%s'", jsonStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasStdin {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
exitIfLineProcessingError(ctx)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
{
|
{
|
||||||
Name: "npub",
|
Name: "npub",
|
||||||
|
@ -100,11 +154,6 @@ var encode = &cli.Command{
|
||||||
Name: "nevent",
|
Name: "nevent",
|
||||||
Usage: "generate event codes with optionally attached relay information",
|
Usage: "generate event codes with optionally attached relay information",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringSliceFlag{
|
|
||||||
Name: "relay",
|
|
||||||
Aliases: []string{"r"},
|
|
||||||
Usage: "attach relay hints to nevent code",
|
|
||||||
},
|
|
||||||
&PubKeyFlag{
|
&PubKeyFlag{
|
||||||
Name: "author",
|
Name: "author",
|
||||||
Aliases: []string{"a"},
|
Aliases: []string{"a"},
|
||||||
|
@ -155,11 +204,6 @@ var encode = &cli.Command{
|
||||||
Usage: "kind of referred replaceable event",
|
Usage: "kind of referred replaceable event",
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
&cli.StringSliceFlag{
|
|
||||||
Name: "relay",
|
|
||||||
Aliases: []string{"r"},
|
|
||||||
Usage: "attach relay hints to naddr code",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
DisableSliceFlagSeparator: true,
|
DisableSliceFlagSeparator: true,
|
||||||
Action: func(ctx context.Context, c *cli.Command) error {
|
Action: func(ctx context.Context, c *cli.Command) error {
|
||||||
|
|
20
helpers.go
20
helpers.go
|
@ -49,6 +49,7 @@ func isPiped() bool {
|
||||||
func getJsonsOrBlank() iter.Seq[string] {
|
func getJsonsOrBlank() iter.Seq[string] {
|
||||||
var curr strings.Builder
|
var curr strings.Builder
|
||||||
|
|
||||||
|
var finalJsonErr error
|
||||||
return func(yield func(string) bool) {
|
return func(yield func(string) bool) {
|
||||||
hasStdin := writeStdinLinesOrNothing(func(stdinLine string) bool {
|
hasStdin := writeStdinLinesOrNothing(func(stdinLine string) bool {
|
||||||
// we're look for an event, but it may be in multiple lines, so if json parsing fails
|
// we're look for an event, but it may be in multiple lines, so if json parsing fails
|
||||||
|
@ -58,8 +59,10 @@ func getJsonsOrBlank() iter.Seq[string] {
|
||||||
|
|
||||||
var dummy any
|
var dummy any
|
||||||
if err := json.Unmarshal([]byte(stdinEvent), &dummy); err != nil {
|
if err := json.Unmarshal([]byte(stdinEvent), &dummy); err != nil {
|
||||||
|
finalJsonErr = err
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
finalJsonErr = nil
|
||||||
|
|
||||||
if !yield(stdinEvent) {
|
if !yield(stdinEvent) {
|
||||||
return false
|
return false
|
||||||
|
@ -72,6 +75,10 @@ func getJsonsOrBlank() iter.Seq[string] {
|
||||||
if !hasStdin {
|
if !hasStdin {
|
||||||
yield("{}")
|
yield("{}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if finalJsonErr != nil {
|
||||||
|
log(color.YellowString("stdin json parse error: %s", finalJsonErr))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,6 +395,19 @@ func clampError(err error, prefixAlreadyPrinted int) string {
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appendUnique[A comparable](list []A, newEls ...A) []A {
|
||||||
|
ex:
|
||||||
|
for _, newEl := range newEls {
|
||||||
|
for _, el := range list {
|
||||||
|
if el == newEl {
|
||||||
|
continue ex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list = append(list, newEl)
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
var colors = struct {
|
var colors = struct {
|
||||||
reset func(...any) (int, error)
|
reset func(...any) (int, error)
|
||||||
italic func(...any) string
|
italic func(...any) string
|
||||||
|
|
Loading…
Reference in New Issue