-
Notifications
You must be signed in to change notification settings - Fork 20
/
fileRefs.go
109 lines (93 loc) · 2.63 KB
/
fileRefs.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
package cmd
import (
"fmt"
"os"
"strconv"
"time"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/zboxcli/util"
"github.com/spf13/cobra"
)
const (
PageLimit = 100
Layout = "2006-01-02 15:04:05"
)
func checkError(err error) {
if err != nil {
PrintError(err)
os.Exit(1)
}
}
var fileRefsCmd = &cobra.Command{
Use: "recent-refs",
Short: "get list of recently added refs",
Long: `get list of recently added refs`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
page, err := cmd.Flags().GetUint("page")
checkError(err)
pageLimit, err := cmd.Flags().GetUint("page_limit")
checkError(err)
if pageLimit > PageLimit {
PrintError("Invalid page limit value. Should be in range (0,100]")
os.Exit(1)
}
allocID, err := cmd.Flags().GetString("allocation")
checkError(err)
in, err := cmd.Flags().GetDuration("in")
checkError(err)
doJSON, err := cmd.Flags().GetBool("json")
checkError(err)
if len(allocID) != 64 {
PrintError("Invalid allocation id")
os.Exit(1)
}
alloc, err := sdk.GetAllocation(allocID)
checkError(err)
d := time.Now().Unix() - int64(in.Seconds())
result, err := alloc.GetRecentlyAddedRefs(int(page), d, int(pageLimit))
checkError(err)
if doJSON {
util.PrintJSON(result)
return
}
fmt.Printf("\nRequested page:%d with page limit: %d, in last: %v ago\n",
page, pageLimit, in)
fmt.Printf(""+
"\nCurrent Page: %d"+
"\nRetrieved Refs: %d"+
"\nNew Offset: %d\n",
page, len(result.Refs), result.Offset,
)
header := []string{"Type", "Name", "Path", "Size", "Lookup Hash", "Created At"}
data := make([][]string, len(result.Refs))
for i, ref := range result.Refs {
var size string
if ref.Type != fileref.DIRECTORY {
size = strconv.FormatInt(ref.ActualFileSize, 10)
}
var createdAt string
t := time.Unix(int64(ref.CreatedAt), 0)
createdAt = t.Local().Format(Layout)
data[i] = []string{
ref.Type,
ref.Name,
ref.Path,
size,
ref.LookupHash,
createdAt,
}
}
util.WriteTable(os.Stdout, header, []string{}, data)
},
}
func init() {
rootCmd.AddCommand(fileRefsCmd)
fileRefsCmd.PersistentFlags().String("allocation", "", "Allocation ID")
fileRefsCmd.PersistentFlags().Uint("page", 0, "Page to get refs from")
fileRefsCmd.PersistentFlags().Duration("in", 0, "Recent refs in this duration")
fileRefsCmd.PersistentFlags().Uint("page_limit", 0, "Number of refs to return in the page")
fileRefsCmd.Flags().Bool("json", false, "(default false) pass this option to print response as json data")
fileRefsCmd.MarkFlagRequired("allocation")
}