Skip to content

Commit

Permalink
feat: add a tmp dir for adding dependency to reduce the impact of fai…
Browse files Browse the repository at this point in the history
…led downloads

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Jun 5, 2024
1 parent c5c5085 commit 755928d
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,19 +976,42 @@ 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.
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 {
Expand Down

0 comments on commit 755928d

Please sign in to comment.