diff --git a/blossom.go b/blossom.go new file mode 100644 index 0000000..f4b039f --- /dev/null +++ b/blossom.go @@ -0,0 +1,222 @@ +package main + +import ( + "context" + "fmt" + "os" + + "github.com/nbd-wtf/go-nostr/keyer" + "github.com/nbd-wtf/go-nostr/nipb0/blossom" + "github.com/urfave/cli/v3" +) + +var blossomCmd = &cli.Command{ + Name: "blossom", + Suggest: true, + UseShortOptionHandling: true, + Usage: "an army knife for blossom things", + DisableSliceFlagSeparator: true, + Flags: append(defaultKeyFlags, + &cli.StringFlag{ + Name: "server", + Aliases: []string{"s"}, + Usage: "the hostname of the target mediaserver", + Required: true, + }, + ), + Commands: []*cli.Command{ + { + Name: "list", + Usage: "lists blobs from a pubkey", + Description: `takes one pubkey passed as an argument or derives one from the --sec supplied. if that is given then it will also pre-authorize the list, which some servers may require.`, + DisableSliceFlagSeparator: true, + ArgsUsage: "[pubkey]", + Action: func(ctx context.Context, c *cli.Command) error { + var client *blossom.Client + pubkey := c.Args().First() + if pubkey != "" { + client = blossom.NewClient(client.GetMediaServer(), keyer.NewReadOnlySigner(pubkey)) + } else { + var err error + client, err = getBlossomClient(ctx, c) + if err != nil { + return err + } + } + + bds, err := client.List(ctx) + if err != nil { + return err + } + + for _, bd := range bds { + stdout(bd) + } + + return nil + }, + }, + { + Name: "upload", + Usage: "uploads a file to a specific mediaserver.", + Description: `takes any number of local file paths and uploads them to a mediaserver, printing the resulting blob descriptions when successful.`, + DisableSliceFlagSeparator: true, + ArgsUsage: "[files...]", + Action: func(ctx context.Context, c *cli.Command) error { + client, err := getBlossomClient(ctx, c) + if err != nil { + return err + } + + hasError := false + for _, fpath := range c.Args().Slice() { + bd, err := client.UploadFile(ctx, fpath) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + hasError = true + continue + } + + j, _ := json.Marshal(bd) + stdout(string(j)) + } + + if hasError { + os.Exit(3) + } + return nil + }, + }, + { + Name: "download", + Usage: "downloads files from mediaservers", + Description: `takes any number of sha256 hashes as hex, downloads them and prints them to stdout (unless --output is specified).`, + DisableSliceFlagSeparator: true, + ArgsUsage: "[sha256...]", + Flags: []cli.Flag{ + &cli.StringSliceFlag{ + Name: "output", + Aliases: []string{"o"}, + Usage: "file name to save downloaded file to, can be passed multiple times when downloading multiple hashes", + }, + }, + Action: func(ctx context.Context, c *cli.Command) error { + client, err := getBlossomClient(ctx, c) + if err != nil { + return err + } + + outputs := c.StringSlice("output") + + hasError := false + for i, hash := range c.Args().Slice() { + if len(outputs)-1 >= i && outputs[i] != "--" { + // save to this file + err := client.DownloadToFile(ctx, hash, outputs[i]) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + hasError = true + } + } else { + // if output wasn't specified, print to stdout + data, err := client.Download(ctx, hash) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + hasError = true + continue + } + os.Stdout.Write(data) + } + } + + if hasError { + os.Exit(2) + } + return nil + }, + }, + { + Name: "del", + Aliases: []string{"delete"}, + Usage: "deletes a file from a mediaserver", + Description: `takes any number of sha256 hashes, signs authorizations and deletes them from the current mediaserver. + +if any of the files are not deleted command will fail, otherwise it will succeed. it will also print error messages to stderr and the hashes it successfully deletes to stdout.`, + DisableSliceFlagSeparator: true, + ArgsUsage: "[sha256...]", + Action: func(ctx context.Context, c *cli.Command) error { + client, err := getBlossomClient(ctx, c) + if err != nil { + return err + } + + hasError := false + for _, hash := range c.Args().Slice() { + err := client.Delete(ctx, hash) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + hasError = true + continue + } + + stdout(hash) + } + + if hasError { + os.Exit(3) + } + return nil + }, + }, + { + Name: "check", + Usage: "asks the mediaserver if it has the specified hashes.", + Description: `uses the HEAD request to succintly check if the server has the specified sha256 hash. + +if any of the files are not found the command will fail, otherwise it will succeed. it will also print error messages to stderr and the hashes it finds to stdout.`, + DisableSliceFlagSeparator: true, + ArgsUsage: "[sha256...]", + Action: func(ctx context.Context, c *cli.Command) error { + client, err := getBlossomClient(ctx, c) + if err != nil { + return err + } + + hasError := false + for _, hash := range c.Args().Slice() { + err := client.Check(ctx, hash) + if err != nil { + hasError = true + fmt.Fprintf(os.Stderr, "%s\n", err) + continue + } + + stdout(hash) + } + + if hasError { + os.Exit(2) + } + return nil + }, + }, + { + Name: "mirror", + Usage: "", + Description: ``, + DisableSliceFlagSeparator: true, + ArgsUsage: "", + Action: func(ctx context.Context, c *cli.Command) error { + return nil + }, + }, + }, +} + +func getBlossomClient(ctx context.Context, c *cli.Command) (*blossom.Client, error) { + keyer, _, err := gatherKeyerFromArguments(ctx, c) + if err != nil { + return nil, err + } + return blossom.NewClient(c.String("server"), keyer), nil +} diff --git a/go.mod b/go.mod index 2948186..0866e84 100644 --- a/go.mod +++ b/go.mod @@ -16,13 +16,14 @@ require ( github.com/mailru/easyjson v0.9.0 github.com/mark3labs/mcp-go v0.8.3 github.com/markusmobius/go-dateparser v1.2.3 - github.com/nbd-wtf/go-nostr v0.50.0 + github.com/nbd-wtf/go-nostr v0.50.1 github.com/urfave/cli/v3 v3.0.0-beta1 golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 ) require ( fiatjaf.com/lib v0.2.0 // indirect + github.com/ImVexed/fasturl v0.0.0-20230304231329-4e41488060f3 // indirect github.com/andybalholm/brotli v1.0.5 // indirect github.com/btcsuite/btcd v0.24.2 // indirect github.com/btcsuite/btcd/btcutil v1.1.5 // indirect @@ -44,10 +45,12 @@ require ( github.com/hablullah/go-juliandays v1.0.0 // indirect github.com/jalaali/go-jalaali v0.0.0-20210801064154-80525e88d958 // indirect github.com/josharian/intern v1.0.0 // indirect - github.com/klauspost/compress v1.17.11 // indirect + github.com/klauspost/compress v1.18.0 // indirect + github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/magefile/mage v1.14.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/minio/simdjson-go v0.4.5 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect @@ -64,6 +67,6 @@ require ( github.com/x448/float16 v0.8.4 // indirect golang.org/x/crypto v0.32.0 // indirect golang.org/x/net v0.34.0 // indirect - golang.org/x/sys v0.29.0 // indirect + golang.org/x/sys v0.30.0 // indirect golang.org/x/text v0.21.0 // indirect ) diff --git a/go.sum b/go.sum index 01fa5eb..9f4c18c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ fiatjaf.com/lib v0.2.0 h1:TgIJESbbND6GjOgGHxF5jsO6EMjuAxIzZHPo5DXYexs= fiatjaf.com/lib v0.2.0/go.mod h1:Ycqq3+mJ9jAWu7XjbQI1cVr+OFgnHn79dQR5oTII47g= +github.com/ImVexed/fasturl v0.0.0-20230304231329-4e41488060f3 h1:ClzzXMDDuUbWfNNZqGeYq4PnYOlwlOVIvSyNaIy0ykg= +github.com/ImVexed/fasturl v0.0.0-20230304231329-4e41488060f3/go.mod h1:we0YA5CsBbH5+/NUzC/AlMmxaDtWlXeNsqrwXjTzmzA= github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= @@ -58,6 +60,7 @@ github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WA github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/dvyukov/go-fuzz v0.0.0-20200318091601-be3528f3a813/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw= github.com/elliotchance/pie/v2 v2.7.0 h1:FqoIKg4uj0G/CrLGuMS9ejnFKa92lxE1dEgBD3pShXg= github.com/elliotchance/pie/v2 v2.7.0/go.mod h1:18t0dgGFH006g4eVdDtWfgFZPQEgl10IoEO8YWEq3Og= github.com/elnosh/gonuts v0.3.1-0.20250123162555-7c0381a585e3 h1:k7evIqJ2BtFn191DgY/b03N2bMYA/iQwzr4f/uHYn20= @@ -106,8 +109,10 @@ github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlT github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= -github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= -github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE= +github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4= @@ -121,13 +126,15 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/minio/simdjson-go v0.4.5 h1:r4IQwjRGmWCQ2VeMc7fGiilu1z5du0gJ/I/FsKwgo5A= +github.com/minio/simdjson-go v0.4.5/go.mod h1:eoNz0DcLQRyEDeaPr4Ru6JpjlZPzbA0IodxVJk8lO8E= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/nbd-wtf/go-nostr v0.50.0 h1:MgL/HPnWSTb5BFCL9RuzYQQpMrTi67MvHem4nWFn47E= -github.com/nbd-wtf/go-nostr v0.50.0/go.mod h1:M50QnhkraC5Ol93v3jqxSMm1aGxUQm5mlmkYw5DJzh8= +github.com/nbd-wtf/go-nostr v0.50.1 h1:l02wKcnYVyvjnj53CNB3I/C161uoH1W51sPWbrLb9C0= +github.com/nbd-wtf/go-nostr v0.50.1/go.mod h1:gOzf8mcTlMs7e+LjYDssRU4Vb/YGeeYO61aDNrxDStY= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -149,6 +156,7 @@ github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1Avp github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= @@ -200,8 +208,8 @@ golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= -golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/main.go b/main.go index 9fb116f..495f129 100644 --- a/main.go +++ b/main.go @@ -22,10 +22,10 @@ var app = &cli.Command{ Usage: "the nostr army knife command-line tool", DisableSliceFlagSeparator: true, Commands: []*cli.Command{ - req, - count, - fetch, event, + req, + fetch, + count, decode, encode, key, @@ -33,6 +33,7 @@ var app = &cli.Command{ relay, bunker, serve, + blossomCmd, encrypt, decrypt, outbox,