-
Notifications
You must be signed in to change notification settings - Fork 23
/
listallocations.go
68 lines (59 loc) · 1.8 KB
/
listallocations.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
package cmd
import (
"os"
"strconv"
"time"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/zboxcli/util"
"github.com/spf13/cobra"
)
var listallocationsCmd = &cobra.Command{
Use: "listallocations",
Short: "List allocations for the client",
Long: `List allocations for the client`,
Run: func(cmd *cobra.Command, args []string) {
doJSON, _ := cmd.Flags().GetBool("json")
allocations, err := sdk.GetAllocations()
if err != nil {
PrintError("Error getting allocations list." + err.Error())
os.Exit(1)
}
if doJSON {
util.PrintJSON(allocations)
return
}
header := []string{"ID", "Size", "Expiration", "Datashards",
"Parityshards", "Finalized", "Canceled", "R. Price", "W. Price"}
data := make([][]string, len(allocations))
for idx, allocation := range allocations {
size := strconv.FormatInt(allocation.Size, 10)
expStr := strconv.FormatInt(allocation.Expiration, 10)
exp, err := strconv.ParseInt(expStr, 10, 64)
if err == nil {
tm := time.Unix(exp, 0)
expStr = tm.String()
}
d := strconv.FormatInt(int64(allocation.DataShards), 10)
p := strconv.FormatInt(int64(allocation.ParityShards), 10)
// TODO (sfxdx): data shards, parity shards
var rp, wp common.Balance
for _, d := range allocation.BlobberDetails {
rp += d.Terms.ReadPrice
wp += d.Terms.WritePrice
}
data[idx] = []string{
allocation.ID, size, expStr, d, p,
strconv.FormatBool(allocation.Finalized),
strconv.FormatBool(allocation.Canceled),
rp.String(), wp.String(),
}
}
util.WriteTable(os.Stdout, header, []string{}, data)
return
},
}
func init() {
rootCmd.AddCommand(listallocationsCmd)
listallocationsCmd.Flags().Bool("json", false, "(default false) pass this option to print response as json data")
}