reflector.go/cmd/sendblob.go

71 lines
1.2 KiB
Go
Raw Permalink Normal View History

2018-08-15 20:25:15 +02:00
package cmd
import (
"crypto/rand"
2020-10-06 00:07:06 +02:00
"os"
2018-08-15 20:25:15 +02:00
"github.com/lbryio/reflector.go/reflector"
2019-01-10 14:02:43 +01:00
2019-11-14 01:11:35 +01:00
"github.com/lbryio/lbry.go/v2/stream"
2018-08-15 20:25:15 +02:00
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
var cmd = &cobra.Command{
2020-10-06 00:07:06 +02:00
Use: "sendblob ADDRESS:PORT [PATH]",
2018-08-15 20:25:15 +02:00
Short: "Send a random blob to a reflector server",
2020-10-06 00:07:06 +02:00
Args: cobra.RangeArgs(1, 2),
2018-08-15 20:25:15 +02:00
Run: sendBlobCmd,
}
rootCmd.AddCommand(cmd)
}
func sendBlobCmd(cmd *cobra.Command, args []string) {
addr := args[0]
2020-10-06 00:07:06 +02:00
var path string
if len(args) >= 2 {
path = args[1]
}
2018-08-15 20:25:15 +02:00
c := reflector.Client{}
err := c.Connect(addr)
if err != nil {
2019-06-05 17:03:55 +02:00
log.Fatal("error connecting client to server: ", err)
2018-08-15 20:25:15 +02:00
}
2020-10-06 00:07:06 +02:00
if path == "" {
blob := make(stream.Blob, 1024)
_, err = rand.Read(blob)
if err != nil {
log.Fatal("failed to make random blob: ", err)
}
err = c.SendBlob(blob)
if err != nil {
log.Error(err)
}
return
2018-08-15 20:25:15 +02:00
}
2020-10-06 00:07:06 +02:00
file, err := os.Open(path)
checkErr(err)
2021-04-13 00:52:56 +02:00
defer file.Close()
s, err := stream.New(file)
2020-10-06 00:07:06 +02:00
checkErr(err)
sdBlob := &stream.SDBlob{}
err = sdBlob.FromBlob(s[0])
checkErr(err)
for i, b := range s {
if i == 0 {
err = c.SendSDBlob(b)
} else {
err = c.SendBlob(b)
}
checkErr(err)
2018-08-15 20:25:15 +02:00
}
}