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

Add read command #405

Open
wants to merge 3 commits into
base: sprint-1.11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions cmd/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cmd

import (
"os"
"sync"

"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)

// readerCmd represents reader command that downloads file from io.Reader interface provided in
// gosdk
var readerCmd = &cobra.Command{
Use: "read",
Short: "read and store file from blobbers",
Long: `This command will use io.ReadSeekCloser interface provided by Allocation object in gosdk to read file
from blobbers`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if !(fflags.Changed("remotepath") || fflags.Changed("authticket")) {
PrintError("Error: remotepath / authticket flag is missing")
os.Exit(1)
}

remotePath := cmd.Flag("remotepath").Value.String()
authTicket := cmd.Flag("authticket").Value.String()
lookupHash := cmd.Flag("lookuphash").Value.String()
verifyDownload, err := cmd.Flags().GetBool("verifydownload")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use verify to keep it short

if err != nil {
PrintError("Error: ", err)
os.Exit(1)
}

thumbnail, err := cmd.Flags().GetBool("thumbnail")
if err != nil {
PrintError("Error: ", err)
os.Exit(1)
}

contentMode := sdk.DOWNLOAD_CONTENT_FULL
if thumbnail {
contentMode = sdk.DOWNLOAD_CONTENT_THUMB
}

localPath := cmd.Flag("localpath").Value.String()
allocationID := cmd.Flag("allocation").Value.String()

numBlocks, _ := cmd.Flags().GetInt("blockspermarker")
wg := &sync.WaitGroup{}
wg.Add(1)
var allocationObj *sdk.Allocation
if authTicket != "" {
allocationObj, err = sdk.GetAllocationFromAuthTicket(authTicket)
} else {
if allocationID == "" {
PrintError("Both authtoken and allocationID are empty")
os.Exit(1)
}
allocationObj, err = sdk.GetAllocation(allocationID)
}

if err != nil {
PrintError("Error fetching the allocation", err)
os.Exit(1)
}

err = allocationObj.DownloadFromReader(
remotePath, localPath, lookupHash, authTicket, contentMode, verifyDownload, uint(numBlocks))
if err != nil {
PrintError("Error: ", err)
os.Exit(1)
}
PrintError("Download successful")
},
}

func init() {
rootCmd.AddCommand(readerCmd)
readerCmd.PersistentFlags().String("allocation", "", "Allocation ID")
readerCmd.PersistentFlags().String("remotepath", "", "Remote path to download")
readerCmd.PersistentFlags().String("localpath", "", "Local path of file to download")
readerCmd.PersistentFlags().String("authticket", "", "Auth ticket fot the file to download if you dont own it")
readerCmd.PersistentFlags().String("lookuphash", "", "The remote lookuphash of the object retrieved from the list")
readerCmd.Flags().BoolP("thumbnail", "t", false, "pass this option to download only the thumbnail")

readerCmd.Flags().IntP("blockspermarker", "b", 10, "pass this option to download multiple blocks per marker")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use blocknum to keep it short, and remove the pass this option from the usage field. So could be Number of blocks to download per marker. Btw, is there any limit to the number.

readerCmd.Flags().BoolP("verifydownload", "v", false, "pass this option to verify downloaded blocks")

readerCmd.MarkFlagRequired("allocation")
readerCmd.MarkFlagRequired("localpath")
}
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.16-0.20230403121102-c569149d9e31
github.com/0chain/gosdk v1.8.16-0.20230408060011-fe70ee799f07
github.com/icza/bitio v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ github.com/0chain/common v0.0.6-0.20221123040931-4a3feacdb97c h1:TDqF7VJa7uLLlEs
github.com/0chain/common v0.0.6-0.20221123040931-4a3feacdb97c/go.mod h1:OxV9kVgVzAPgGHHPcS/aUSL2ZxNvKDU6jPoggKMbqns=
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.16-0.20230403121102-c569149d9e31 h1:BbKA9pJBN6Lk6LrZvZkdUxWyIVzPeslWOvE8Us6HkwE=
github.com/0chain/gosdk v1.8.16-0.20230403121102-c569149d9e31/go.mod h1:cBm7vTDxG+QvWb3SAnsYaOnLkBhUXc5UmtJn/QfVWKI=
github.com/0chain/gosdk v1.8.16-0.20230408060011-fe70ee799f07 h1:KbjsQoLb93mPpkCMdpE4rCUd4tCKdX+OVg4MGIW9QFE=
github.com/0chain/gosdk v1.8.16-0.20230408060011-fe70ee799f07/go.mod h1:cBm7vTDxG+QvWb3SAnsYaOnLkBhUXc5UmtJn/QfVWKI=
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/Luzifer/go-openssl/v3 v3.1.0 h1:QqKqo6kYXGGUsvtUoCpRZm8lHw+jDfhbzr36gVj+/gw=
Expand Down