forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasks_query.go
More file actions
80 lines (67 loc) · 1.86 KB
/
asks_query.go
File metadata and controls
80 lines (67 loc) · 1.86 KB
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
package cmd
import (
"context"
"errors"
"os"
"strconv"
"github.com/caarlos0/spin"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/textileio/powergate/index/ask"
)
func init() {
queryCmd.Flags().Uint64P("maxPrice", "m", 0, "max price of the asks to query")
queryCmd.Flags().IntP("pieceSize", "p", 0, "piece size of the asks to query")
queryCmd.Flags().IntP("limit", "l", -1, "limit the number of results")
queryCmd.Flags().IntP("offset", "o", -1, "offset of results")
asksCmd.AddCommand(queryCmd)
}
var queryCmd = &cobra.Command{
Use: "query",
Short: "Query the available asks",
Long: `Query the available asks`,
PreRun: func(cmd *cobra.Command, args []string) {
err := viper.BindPFlags(cmd.Flags())
checkErr(err)
},
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
defer cancel()
mp := viper.GetUint64("maxPrice")
ps := viper.GetUint64("pieceSize")
l := viper.GetInt("limit")
o := viper.GetInt("offset")
if mp == 0 {
Fatal(errors.New("maxPrice must be > 0"))
}
if ps == 0 {
Fatal(errors.New("pieceSize must be > 0"))
}
q := ask.Query{
MaxPrice: mp,
PieceSize: ps,
Limit: l,
Offset: o,
}
s := spin.New("%s Querying network for available storage asks...")
s.Start()
asks, err := fcClient.Asks.Query(ctx, q)
s.Stop()
checkErr(err)
if len(asks) > 0 {
data := make([][]string, len(asks))
for i, a := range asks {
data[i] = []string{
a.Miner,
strconv.Itoa(int(a.Price)),
strconv.Itoa(int(a.MinPieceSize)),
strconv.FormatInt(a.Timestamp, 10),
strconv.FormatInt(a.Expiry, 10),
}
}
RenderTable(os.Stdout, []string{"miner", "price", "min piece size", "timestamp", "expiry"}, data)
}
Message("Found %d asks", aurora.White(len(asks)).Bold())
},
}