From 683c51f0e83d317bc21d77775b639b95dda106b4 Mon Sep 17 00:00:00 2001 From: Conflux Date: Tue, 27 Aug 2024 17:35:19 +0800 Subject: [PATCH] refactor code for diff reuse --- cmd/download.go | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/cmd/download.go b/cmd/download.go index 65d7cff..19294ae 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -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", @@ -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") @@ -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") } @@ -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") } @@ -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) @@ -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,