Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/list recent files #284

Merged
merged 15 commits into from
Aug 15, 2022
109 changes: 109 additions & 0 deletions cmd/fileRefs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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, "pass this option to print response as json data")
fileRefsCmd.MarkFlagRequired("allocation")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/0chain/errors v1.0.3
github.com/0chain/gosdk v1.8.7-0.20220812142644-3b9c3f698c07
github.com/0chain/gosdk v1.8.7-0.20220815002611-96240cfb56cc
github.com/icza/bitio v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.5.0
Expand Down
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,18 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM=
github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc=
github.com/0chain/gosdk v1.8.7-0.20220812142644-3b9c3f698c07 h1:a/2DP7VLAN3xSEUMQCTwnalYxkCODDp8kweFJIUw8mA=
github.com/0chain/gosdk v1.8.7-0.20220812142644-3b9c3f698c07/go.mod h1:ZQU/M42RUPryspHYEHZkBzKyFOUF7B1GSCwvFItjFyA=
github.com/0chain/gosdk v1.8.7-0.20220805044835-6091b1c4aed5 h1:E5QvqW7JT4it1bZZtrcPKO8YDggiu9JMUzcct2y0MAU=
github.com/0chain/gosdk v1.8.7-0.20220805044835-6091b1c4aed5/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220807080924-1aff002bd1b0 h1:jsJQK8YlR+7Uexxp+VKNogq2Wpj0TtpDKEWDs+nFkuE=
github.com/0chain/gosdk v1.8.7-0.20220807080924-1aff002bd1b0/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220809161948-fd05936d0b70 h1:CGTiH+/lSTrsfcF6N5oUFXGEMxO6CM9qhqLLG55jwyo=
github.com/0chain/gosdk v1.8.7-0.20220809161948-fd05936d0b70/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220810014712-78a2669c99cf h1:IdVxllVdH9N15FwQfQO6K8M1M3dhnyvSnS8Dh213LE0=
github.com/0chain/gosdk v1.8.7-0.20220810014712-78a2669c99cf/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220811084901-819b86c6922a h1:1+6ASxrfE4PJLwH6IK8y2X4gh5LnZrWV/Hq8YwTsPsU=
github.com/0chain/gosdk v1.8.7-0.20220811084901-819b86c6922a/go.mod h1:ZdbSM2r3G9wekID5HHqJFiP2bYrzyheLRWz9DDzmFYw=
github.com/0chain/gosdk v1.8.7-0.20220815002611-96240cfb56cc h1:AyYZ8npC53+SWJrW3s0QMNGQ0ZcvkM5DjUGQfq9nWbY=
github.com/0chain/gosdk v1.8.7-0.20220815002611-96240cfb56cc/go.mod h1:ZQU/M42RUPryspHYEHZkBzKyFOUF7B1GSCwvFItjFyA=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/VictoriaMetrics/fastcache v1.6.0 h1:C/3Oi3EiBCqufydp1neRZkqcwmEiuRT9c3fqvvgKm5o=
Expand Down