Skip to content

Commit

Permalink
Merge pull request #360 from zong-zhe/add-tmp-path
Browse files Browse the repository at this point in the history
feat: add a tmp dir for adding dependency to reduce the impact of failed downloads
  • Loading branch information
Peefy committed Jun 6, 2024
2 parents c5c5085 + 1e80f0e commit f5cf19d
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -976,19 +977,56 @@ 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),
))
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.
if utils.DirExists(localPath) {
err := os.RemoveAll(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.
dpkg, err := c.LoadPkgFromPath(localPath)
if err != nil {
return nil, err
}

dep.FromKclPkg(dpkg)
dep.Sum, err = c.AcquireDepSum(*dep)
if err != nil {
Expand Down

0 comments on commit f5cf19d

Please sign in to comment.