convert to using nostrlib.

This commit is contained in:
fiatjaf
2025-04-20 18:11:21 -03:00
parent 1b43dbda02
commit d733a31898
32 changed files with 568 additions and 418 deletions

157
flags.go
View File

@@ -6,14 +6,18 @@ import (
"strconv"
"time"
"github.com/urfave/cli/v3"
"fiatjaf.com/nostr"
"github.com/markusmobius/go-dateparser"
"github.com/nbd-wtf/go-nostr"
"github.com/urfave/cli/v3"
)
//
//
//
type NaturalTimeFlag = cli.FlagBase[nostr.Timestamp, struct{}, naturalTimeValue]
// wrap to satisfy golang's flag interface.
// wrap to satisfy flag interface.
type naturalTimeValue struct {
timestamp *nostr.Timestamp
hasBeenSet bool
@@ -39,11 +43,6 @@ func (t naturalTimeValue) ToString(b nostr.Timestamp) string {
return fmt.Sprintf("%v", ts)
}
// Timestamp constructor(for internal testing only)
func newTimestamp(timestamp nostr.Timestamp) *naturalTimeValue {
return &naturalTimeValue{timestamp: &timestamp}
}
// Below functions are to satisfy the flag.Value interface
// Parses the string value to timestamp
@@ -93,3 +92,145 @@ func (t *naturalTimeValue) Get() any {
func getNaturalDate(cmd *cli.Command, name string) nostr.Timestamp {
return cmd.Value(name).(nostr.Timestamp)
}
//
//
//
type (
PubKeyFlag = cli.FlagBase[nostr.PubKey, struct{}, pubkeyValue]
)
// wrap to satisfy flag interface.
type pubkeyValue struct {
pubkey nostr.PubKey
hasBeenSet bool
}
var _ cli.ValueCreator[nostr.PubKey, struct{}] = pubkeyValue{}
// Below functions are to satisfy the ValueCreator interface
func (t pubkeyValue) Create(val nostr.PubKey, p *nostr.PubKey, c struct{}) cli.Value {
*p = val
return &pubkeyValue{
pubkey: val,
}
}
func (t pubkeyValue) ToString(b nostr.PubKey) string {
return t.pubkey.String()
}
// Below functions are to satisfy the flag.Value interface
// Parses the string value to timestamp
func (t *pubkeyValue) Set(value string) error {
pk, err := nostr.PubKeyFromHex(value)
t.pubkey = pk
t.hasBeenSet = true
return err
}
// String returns a readable representation of this value (for usage defaults)
func (t *pubkeyValue) String() string {
return fmt.Sprintf("%#v", t.pubkey)
}
// Value returns the pubkey value stored in the flag
func (t *pubkeyValue) Value() nostr.PubKey {
return t.pubkey
}
// Get returns the flag structure
func (t *pubkeyValue) Get() any {
return t.pubkey
}
func getPubKey(cmd *cli.Command, name string) nostr.PubKey {
return cmd.Value(name).(nostr.PubKey)
}
//
//
//
type (
pubkeySlice = cli.SliceBase[nostr.PubKey, struct{}, pubkeyValue]
PubKeySliceFlag = cli.FlagBase[[]nostr.PubKey, struct{}, pubkeySlice]
)
func getPubKeySlice(cmd *cli.Command, name string) []nostr.PubKey {
return cmd.Value(name).([]nostr.PubKey)
}
//
//
//
type (
IDFlag = cli.FlagBase[nostr.ID, struct{}, idValue]
)
// wrap to satisfy flag interface.
type idValue struct {
id nostr.ID
hasBeenSet bool
}
var _ cli.ValueCreator[nostr.ID, struct{}] = idValue{}
// Below functions are to satisfy the ValueCreator interface
func (t idValue) Create(val nostr.ID, p *nostr.ID, c struct{}) cli.Value {
*p = val
return &idValue{
id: val,
}
}
func (t idValue) ToString(b nostr.ID) string {
return t.id.String()
}
// Below functions are to satisfy the flag.Value interface
// Parses the string value to timestamp
func (t *idValue) Set(value string) error {
pk, err := nostr.IDFromHex(value)
t.id = pk
t.hasBeenSet = true
return err
}
// String returns a readable representation of this value (for usage defaults)
func (t *idValue) String() string {
return fmt.Sprintf("%#v", t.id)
}
// Value returns the id value stored in the flag
func (t *idValue) Value() nostr.ID {
return t.id
}
// Get returns the flag structure
func (t *idValue) Get() any {
return t.id
}
func getID(cmd *cli.Command, name string) nostr.ID {
return cmd.Value(name).(nostr.ID)
}
//
//
//
type (
idSlice = cli.SliceBase[nostr.ID, struct{}, idValue]
IDSliceFlag = cli.FlagBase[[]nostr.ID, struct{}, idSlice]
)
func getIDSlice(cmd *cli.Command, name string) []nostr.ID {
return cmd.Value(name).([]nostr.ID)
}