|
6 | 6 | "io"
|
7 | 7 | "os"
|
8 | 8 | "path/filepath"
|
| 9 | + "runtime" |
9 | 10 | "strings"
|
10 | 11 |
|
11 | 12 | "github.com/BurntSushi/toml"
|
@@ -976,19 +977,56 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
|
976 | 977 | }
|
977 | 978 | }
|
978 | 979 |
|
979 |
| - err := c.DepDownloader.Download(*downloader.NewDownloadOptions( |
980 |
| - downloader.WithLocalPath(localPath), |
| 980 | + // create a tmp dir to download the oci package. |
| 981 | + tmpDir, err := os.MkdirTemp("", "") |
| 982 | + if err != nil { |
| 983 | + return nil, reporter.NewErrorEvent(reporter.Bug, err, fmt.Sprintf("failed to create temp dir '%s'.", tmpDir)) |
| 984 | + } |
| 985 | + // clean the temp dir. |
| 986 | + defer os.RemoveAll(tmpDir) |
| 987 | + |
| 988 | + err = c.DepDownloader.Download(*downloader.NewDownloadOptions( |
| 989 | + downloader.WithLocalPath(tmpDir), |
981 | 990 | downloader.WithSource(dep.Source),
|
982 | 991 | downloader.WithLogWriter(c.logWriter),
|
983 | 992 | downloader.WithSettings(c.settings),
|
984 | 993 | ))
|
985 | 994 | if err != nil {
|
986 | 995 | return nil, err
|
987 | 996 | }
|
988 |
| - dpkg, err := pkg.FindFirstKclPkgFrom(localPath) |
| 997 | + |
| 998 | + // check the package in tmp dir is a valid kcl package. |
| 999 | + _, err = pkg.FindFirstKclPkgFrom(tmpDir) |
989 | 1000 | if err != nil {
|
990 | 1001 | return nil, err
|
991 | 1002 | }
|
| 1003 | + |
| 1004 | + // rename the tmp dir to the local path. |
| 1005 | + if utils.DirExists(localPath) { |
| 1006 | + err := os.RemoveAll(localPath) |
| 1007 | + if err != nil { |
| 1008 | + return nil, err |
| 1009 | + } |
| 1010 | + } |
| 1011 | + |
| 1012 | + if runtime.GOOS != "windows" { |
| 1013 | + err = os.Rename(tmpDir, localPath) |
| 1014 | + if err != nil { |
| 1015 | + return nil, err |
| 1016 | + } |
| 1017 | + } else { |
| 1018 | + err = copy.Copy(tmpDir, localPath) |
| 1019 | + if err != nil { |
| 1020 | + return nil, err |
| 1021 | + } |
| 1022 | + } |
| 1023 | + |
| 1024 | + // load the package from the local path. |
| 1025 | + dpkg, err := c.LoadPkgFromPath(localPath) |
| 1026 | + if err != nil { |
| 1027 | + return nil, err |
| 1028 | + } |
| 1029 | + |
992 | 1030 | dep.FromKclPkg(dpkg)
|
993 | 1031 | dep.Sum, err = c.AcquireDepSum(*dep)
|
994 | 1032 | if err != nil {
|
|
0 commit comments