From 755928df5c74a47cba54e14c6cabd29f5440e82d Mon Sep 17 00:00:00 2001 From: zongz Date: Wed, 5 Jun 2024 17:48:12 +0800 Subject: [PATCH 1/3] feat: add a tmp dir for adding dependency to reduce the impact of failed downloads Signed-off-by: zongz --- pkg/client/client.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index c818a992..2c818ae6 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -976,8 +976,16 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (* } } - err := c.DepDownloader.Download(*downloader.NewDownloadOptions( - downloader.WithLocalPath(localPath), + // create a tmp dir to download the oci package. + tmpDir, err := os.MkdirTemp("", "") + if err != nil { + return nil, reporter.NewErrorEvent(reporter.Bug, err, fmt.Sprintf("failed to create temp dir '%s'.", tmpDir)) + } + // clean the temp dir. + defer os.RemoveAll(tmpDir) + + err = c.DepDownloader.Download(*downloader.NewDownloadOptions( + downloader.WithLocalPath(tmpDir), downloader.WithSource(dep.Source), downloader.WithLogWriter(c.logWriter), downloader.WithSettings(c.settings), @@ -985,10 +993,25 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (* if err != nil { return nil, err } - dpkg, err := pkg.FindFirstKclPkgFrom(localPath) + + // check the package in tmp dir is a valid kcl package. + _, err = pkg.FindFirstKclPkgFrom(tmpDir) if err != nil { return nil, err } + + // rename the tmp dir to the local path. + err = os.Rename(tmpDir, localPath) + if err != nil { + return nil, err + } + + // load the package from the local path. + dpkg, err := c.LoadPkgFromPath(localPath) + if err != nil { + return nil, err + } + dep.FromKclPkg(dpkg) dep.Sum, err = c.AcquireDepSum(*dep) if err != nil { From 12338e0ac193eec8ab17746ba2e9b91828dcf0f1 Mon Sep 17 00:00:00 2001 From: zongz Date: Wed, 5 Jun 2024 18:37:00 +0800 Subject: [PATCH 2/3] fix: fix test case Signed-off-by: zongz --- pkg/client/client.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/client/client.go b/pkg/client/client.go index 2c818ae6..8728117d 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -1001,6 +1001,12 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (* } // rename the tmp dir to the local path. + if utils.DirExists(localPath) { + err := os.RemoveAll(localPath) + if err != nil { + return nil, err + } + } err = os.Rename(tmpDir, localPath) if err != nil { return nil, err From 1e80f0e6536eaaadf4cfc7cc0392212f2fdef751 Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 6 Jun 2024 10:38:27 +0800 Subject: [PATCH 3/3] fix: fix windows test case Signed-off-by: zongz --- pkg/client/client.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 8728117d..ef192745 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -6,6 +6,7 @@ import ( "io" "os" "path/filepath" + "runtime" "strings" "github.com/BurntSushi/toml" @@ -1007,9 +1008,17 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (* return nil, err } } - err = os.Rename(tmpDir, localPath) - if err != nil { - return nil, err + + if runtime.GOOS != "windows" { + err = os.Rename(tmpDir, localPath) + if err != nil { + return nil, err + } + } else { + err = copy.Copy(tmpDir, localPath) + if err != nil { + return nil, err + } } // load the package from the local path.