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 all commits
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")
}
4 changes: 3 additions & 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.17-0.20230623160341-6d98b815ed3e
github.com/0chain/gosdk v1.8.17-0.20230704092638-a89680ad957a
github.com/icza/bitio v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.6.0
Expand All @@ -28,6 +28,7 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/herumi/bls-go-binary v1.31.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
Expand Down Expand Up @@ -65,6 +66,7 @@ require (
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
7 changes: 5 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.20230127095721-8df4d1d72565 h1:z+DtCR8mBsjPnEs
github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565/go.mod h1:UyDC8Qyl5z9lGkCnf9RHJPMektnFX8XtCJZHXCCVj8E=
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.17-0.20230623160341-6d98b815ed3e h1:DFoj1CqpwT/7t+KwzHca+TEk0cwY7FOl/b1T0xkQZBQ=
github.com/0chain/gosdk v1.8.17-0.20230623160341-6d98b815ed3e/go.mod h1:GgqNjoHBO0JN9TcpDAC28/VE2VsZi4MDr4qGdzfWV+I=
github.com/0chain/gosdk v1.8.17-0.20230704092638-a89680ad957a h1:RZL1gCy8GDa1UEKLiS+HJr4b0Z7oZ7bk5qK7psxDnSQ=
github.com/0chain/gosdk v1.8.17-0.20230704092638-a89680ad957a/go.mod h1:GgqNjoHBO0JN9TcpDAC28/VE2VsZi4MDr4qGdzfWV+I=
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 Expand Up @@ -194,6 +194,7 @@ github.com/hashicorp/go-bexpr v0.1.10 h1:9kuI5PFotCboP3dkDYFr/wi0gg0QVbSNz5oFRpx
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/herumi/bls-go-binary v1.31.0 h1:L1goQ2tMtGgpXCg5AwHAdJQpLs/pfnWWEc3Wog6OhmI=
Expand Down Expand Up @@ -665,6 +666,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
Expand Down