From 28a0f54a1cd46f7b3587afed9c654ab43f843836 Mon Sep 17 00:00:00 2001 From: Laxmi Prasad Oli Date: Sat, 8 Apr 2023 12:04:40 +0545 Subject: [PATCH 1/2] Add read command --- cmd/reader.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 +-- 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 cmd/reader.go diff --git a/cmd/reader.go b/cmd/reader.go new file mode 100644 index 00000000..d4dca469 --- /dev/null +++ b/cmd/reader.go @@ -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") + 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") + readerCmd.Flags().BoolP("verifydownload", "v", false, "pass this option to verify downloaded blocks") + + readerCmd.MarkFlagRequired("allocation") + readerCmd.MarkFlagRequired("localpath") +} diff --git a/go.mod b/go.mod index 1c99b99b..eeea3578 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 873c9a40..466718db 100644 --- a/go.sum +++ b/go.sum @@ -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= From ac38ba1f87e3c29db79db697fb7839913e6de644 Mon Sep 17 00:00:00 2001 From: lpoli Date: Tue, 4 Jul 2023 20:02:52 +0545 Subject: [PATCH 2/2] Update gosdk --- go.mod | 4 +++- go.sum | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 1eff6ead..8d7e18e7 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 ) diff --git a/go.sum b/go.sum index 245dbd8b..d5128f89 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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=