/
deals_watch.go
118 lines (101 loc) · 2.53 KB
/
deals_watch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cmd
import (
"context"
"errors"
"fmt"
"io"
"sort"
"github.com/gosuri/uilive"
"github.com/ipfs/go-cid"
"github.com/spf13/cobra"
"github.com/textileio/filecoin/api/client"
)
func init() {
watchCmd.Flags().StringSliceP("cids", "c", []string{}, "List of deal cids to watch")
dealsCmd.AddCommand(watchCmd)
}
var watchCmd = &cobra.Command{
Use: "watch",
Short: "Watch storage process",
Long: `Watch storage process`,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
defer cancel()
cidStrings, err := cmd.Flags().GetStringSlice("cids")
checkErr(err)
if len(cidStrings) == 0 {
Fatal(errors.New("you must provide at least one cid using the cids flag"))
}
cids := make([]cid.Cid, len(cidStrings))
for i, s := range cidStrings {
cid, err := cid.Parse(s)
checkErr(err)
cids[i] = cid
}
processWatchEventsUntilDone(ctx, cids)
},
}
func processWatchEventsUntilDone(ctx context.Context, cids []cid.Cid) {
state := make(map[string]*client.WatchEvent, len(cids))
for _, cid := range cids {
state[cid.String()] = nil
}
writer := uilive.New()
writer.Start()
updateOutput(writer, state)
ch, err := fcClient.Deals.Watch(ctx, cids)
checkErr(err)
for {
event, ok := <-ch
if !ok {
break
}
state[event.Deal.ProposalCid.String()] = &event
updateOutput(writer, state)
if isComplete(state) {
break
}
}
writer.Stop()
}
func updateOutput(writer io.Writer, state map[string]*client.WatchEvent) {
keys := make([]string, 0, len(state))
for k := range state {
keys = append(keys, k)
}
sort.Strings(keys)
data := make([][]string, len(keys))
for i, k := range keys {
val := "awaiting state"
if state[k] != nil {
if state[k].Deal.StateName == "DealError" && state[k].Err != nil {
val = fmt.Sprintf("%v: %v", state[k].Deal.StateName, state[k].Err.Error())
} else if state[k].Err != nil {
val = fmt.Sprintf("Error: %v", state[k].Err.Error())
} else {
val = state[k].Deal.StateName
}
}
data[i] = []string{
k,
val,
}
}
RenderTable(writer, []string{"deal id", "state"}, data)
}
func isComplete(state map[string]*client.WatchEvent) bool {
for _, value := range state {
processing := false
if value == nil ||
value.Deal.StateName == "DealUnknown" ||
value.Deal.StateName == "DealAccepted" ||
value.Deal.StateName == "DealStaged" ||
value.Deal.StateName == "DealSealing" {
processing = true
}
if processing && value != nil && value.Err == nil {
return false
}
}
return true
}