feat(cli): data transfer watch option

make watching data transfer CLI updates only an option
This commit is contained in:
hannahhoward 2020-08-18 18:23:29 -07:00
parent 66ac7c195c
commit 4fc5f9fe9f

View File

@ -3,6 +3,7 @@ package cli
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -1209,7 +1210,7 @@ var clientInfoCmd = &cli.Command{
var clientListTransfers = &cli.Command{ var clientListTransfers = &cli.Command{
Name: "list-transfers", Name: "list-transfers",
Usage: "Monitor ongoing data transfers for deals", Usage: "List ongoing data transfers for deals",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "color", Name: "color",
@ -1220,6 +1221,10 @@ var clientListTransfers = &cli.Command{
Name: "completed", Name: "completed",
Usage: "show completed data transfers", Usage: "show completed data transfers",
}, },
&cli.BoolFlag{
Name: "watch",
Usage: "watch deal updates in real-time, rather than a one time list",
},
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx) api, closer, err := GetFullNodeAPI(cctx)
@ -1234,89 +1239,97 @@ var clientListTransfers = &cli.Command{
return err return err
} }
channelUpdates, err := api.ClientDataTransferUpdates(ctx) completed := cctx.Bool("completed")
if err != nil { color := cctx.Bool("color")
return err watch := cctx.Bool("watch")
}
for { if watch {
tm.Clear() // Clear current screen channelUpdates, err := api.ClientDataTransferUpdates(ctx)
if err != nil {
tm.MoveCursor(1, 1) return err
sort.Slice(channels, func(i, j int) bool {
return channels[i].TransferID < channels[j].TransferID
})
completed := cctx.Bool("completed")
var receivingChannels, sendingChannels []lapi.DataTransferChannel
for _, channel := range channels {
if !completed && channel.Status == datatransfer.Completed {
continue
}
if channel.IsSender {
sendingChannels = append(sendingChannels, channel)
} else {
receivingChannels = append(receivingChannels, channel)
}
} }
color := cctx.Bool("color") for {
tm.Clear() // Clear current screen
tm.Printf("Sending Channels\n\n") tm.MoveCursor(1, 1)
w := tablewriter.New(tablewriter.Col("ID"),
tablewriter.Col("Status"),
tablewriter.Col("Sending To"),
tablewriter.Col("Root Cid"),
tablewriter.Col("Initiated?"),
tablewriter.Col("Transferred"),
tablewriter.Col("Voucher"),
tablewriter.NewLineCol("Message"))
for _, channel := range sendingChannels {
w.Write(toChannelOutput(color, "Sending To", channel))
}
w.Flush(tm.Screen)
fmt.Fprintf(os.Stdout, "\nReceiving Channels\n\n") outputChannels(tm.Screen, channels, completed, color)
w = tablewriter.New(tablewriter.Col("ID"),
tablewriter.Col("Status"),
tablewriter.Col("Receiving From"),
tablewriter.Col("Root Cid"),
tablewriter.Col("Initiated?"),
tablewriter.Col("Transferred"),
tablewriter.Col("Voucher"),
tablewriter.NewLineCol("Message"))
for _, channel := range receivingChannels {
w.Write(toChannelOutput(color, "Receiving From", channel)) tm.Flush()
}
w.Flush(tm.Screen)
tm.Flush() select {
case <-ctx.Done():
select { return nil
case <-ctx.Done(): case channelUpdate := <-channelUpdates:
return nil var found bool
case channelUpdate := <-channelUpdates: for i, existing := range channels {
var found bool if existing.TransferID == channelUpdate.TransferID &&
for i, existing := range channels { existing.OtherPeer == channelUpdate.OtherPeer &&
if existing.TransferID == channelUpdate.TransferID && existing.IsSender == channelUpdate.IsSender &&
existing.OtherPeer == channelUpdate.OtherPeer && existing.IsInitiator == channelUpdate.IsInitiator {
existing.IsSender == channelUpdate.IsSender && channels[i] = channelUpdate
existing.IsInitiator == channelUpdate.IsInitiator { found = true
channels[i] = channelUpdate break
found = true }
break }
if !found {
channels = append(channels, channelUpdate)
} }
} }
if !found {
channels = append(channels, channelUpdate)
}
} }
} }
outputChannels(os.Stdout, channels, completed, color)
return nil
}, },
} }
func outputChannels(out io.Writer, channels []api.DataTransferChannel, completed bool, color bool) {
sort.Slice(channels, func(i, j int) bool {
return channels[i].TransferID < channels[j].TransferID
})
var receivingChannels, sendingChannels []lapi.DataTransferChannel
for _, channel := range channels {
if !completed && channel.Status == datatransfer.Completed {
continue
}
if channel.IsSender {
sendingChannels = append(sendingChannels, channel)
} else {
receivingChannels = append(receivingChannels, channel)
}
}
fmt.Fprintf(out, "Sending Channels\n\n")
w := tablewriter.New(tablewriter.Col("ID"),
tablewriter.Col("Status"),
tablewriter.Col("Sending To"),
tablewriter.Col("Root Cid"),
tablewriter.Col("Initiated?"),
tablewriter.Col("Transferred"),
tablewriter.Col("Voucher"),
tablewriter.NewLineCol("Message"))
for _, channel := range sendingChannels {
w.Write(toChannelOutput(color, "Sending To", channel))
}
w.Flush(out)
fmt.Fprintf(out, "\nReceiving Channels\n\n")
w = tablewriter.New(tablewriter.Col("ID"),
tablewriter.Col("Status"),
tablewriter.Col("Receiving From"),
tablewriter.Col("Root Cid"),
tablewriter.Col("Initiated?"),
tablewriter.Col("Transferred"),
tablewriter.Col("Voucher"),
tablewriter.NewLineCol("Message"))
for _, channel := range receivingChannels {
w.Write(toChannelOutput(color, "Receiving From", channel))
}
w.Flush(out)
}
func channelStatusString(useColor bool, status datatransfer.Status) string { func channelStatusString(useColor bool, status datatransfer.Status) string {
s := datatransfer.Statuses[status] s := datatransfer.Statuses[status]
if !useColor { if !useColor {