Skip to content

Commit

Permalink
refactor code for diff reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
wanliqun committed Aug 27, 2024
1 parent d8e0f06 commit 683c51f
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ import (
"github.com/spf13/cobra"
)

var (
downloadArgs struct {
file string
folder string
type downloadArgument struct {
file string
folder string

indexer string
nodes []string
indexer string
nodes []string

root string
proof bool
root string
proof bool

timeout time.Duration
}
timeout time.Duration
}

var (
downloadArgs downloadArgument

downloadCmd = &cobra.Command{
Use: "download",
Expand All @@ -39,8 +41,9 @@ var (

func init() {
downloadCmd.Flags().StringVar(&downloadArgs.file, "file", "", "File name to download")
downloadCmd.Flags().StringVar(&downloadArgs.file, "folder", "", "Folder name to download")
downloadCmd.Flags().StringVar(&downloadArgs.folder, "folder", "", "Folder name to download")
downloadCmd.MarkFlagsOneRequired("file", "folder")
downloadCmd.MarkFlagsMutuallyExclusive("file", "folder")

downloadCmd.Flags().StringSliceVar(&downloadArgs.nodes, "node", []string{}, "ZeroGStorage storage node URL. Multiple nodes could be specified and separated by comma, e.g. url1,url2,url3")
downloadCmd.Flags().StringVar(&downloadArgs.indexer, "indexer", "", "ZeroGStorage indexer URL")
Expand All @@ -63,7 +66,7 @@ func download(*cobra.Command, []string) {
defer cancel()
}

downloader, closer, err := newDownloader()
downloader, closer, err := newDownloader(downloadArgs)
if err != nil {
logrus.WithError(err).Fatal("Failed to initialize downloader")
}
Expand All @@ -89,7 +92,7 @@ func downloadFolder(ctx context.Context, downloader iDownloader) error {
return errors.WithMessage(err, "failed to create folder")
}

root, err := buildZgFileTree(ctx, downloader)
root, err := buildZgFileTree(ctx, downloader, downloadArgs.root, downloadArgs.proof)
if err != nil {
return errors.WithMessage(err, "failed to build file free from ZeroGStorage network")
}
Expand Down Expand Up @@ -121,13 +124,14 @@ func downloadFolder(ctx context.Context, downloader iDownloader) error {
}

// buildZgFileTree downloads directory metadata from the ZeroGStorage network, and then decodes it to a file tree.
func buildZgFileTree(ctx context.Context, downloader iDownloader) (*dir.FsNode, error) {
func buildZgFileTree(ctx context.Context, downloader iDownloader, root string, proof bool) (*dir.FsNode, error) {
// Download the directory metadata file to a temporary location.
dirMetaFilePath := filepath.Join(os.TempDir(), "0g."+downloadArgs.root+".metadata")
err := downloader.Download(ctx, downloadArgs.root, dirMetaFilePath, downloadArgs.proof)
dirMetaFilePath := filepath.Join(os.TempDir(), "0gdir-"+root+".metadata")
err := downloader.Download(ctx, root, dirMetaFilePath, proof)
if err != nil && !errors.Is(err, transfer.ErrFileAlreadyExists) {
return nil, errors.WithMessage(err, "failed to download directory metadata")
}
defer os.Remove(dirMetaFilePath)

// Read the downloaded metadata file.
metaData, err := os.ReadFile(dirMetaFilePath)
Expand All @@ -149,7 +153,7 @@ type iDownloader interface {
Download(ctx context.Context, root string, filename string, withProof bool) error
}

func newDownloader() (iDownloader, func(), error) {
func newDownloader(downloadArgs downloadArgument) (iDownloader, func(), error) {
if downloadArgs.indexer != "" {
indexerClient, err := indexer.NewClient(downloadArgs.indexer, indexer.IndexerClientOption{
ProviderOption: providerOption,
Expand Down

0 comments on commit 683c51f

Please sign in to comment.