| 83 | } |
| 84 | |
| 85 | func installWithDeps(ctx context.Context, wsl wsllib.WslLib, reg wsllib.WslReg, name string, rootPath string, sha256Sum string, showProgress bool, deps installDeps) error { |
| 86 | ctx = normalizeContext(ctx) |
| 87 | if err := ctx.Err(); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | if deps.tempDir == nil { |
| 91 | deps.tempDir = os.TempDir |
| 92 | } |
| 93 | if deps.statFile == nil { |
| 94 | deps.statFile = os.Stat |
| 95 | } |
| 96 | if deps.createFile == nil { |
| 97 | deps.createFile = func(path string) (io.Closer, error) { |
| 98 | return os.Create(path) |
| 99 | } |
| 100 | } |
| 101 | if deps.renameFile == nil { |
| 102 | deps.renameFile = os.Rename |
| 103 | } |
| 104 | if deps.removeFile == nil { |
| 105 | deps.removeFile = os.Remove |
| 106 | } |
| 107 | if deps.copyFile == nil { |
| 108 | deps.copyFile = fileutil.CopyFile |
| 109 | } |
| 110 | |
| 111 | rootPathLower := strings.ToLower(rootPath) |
| 112 | sha256Actual := "" |
| 113 | usedCachedDownload := false |
| 114 | downloadCachePath := "" |
| 115 | downloadPartialPath := "" |
| 116 | if showProgress { |
| 117 | fmt.Printf("Using: %s\n", rootPath) |
| 118 | } |
| 119 | |
| 120 | if strings.HasPrefix(rootPathLower, "http://") || strings.HasPrefix(rootPathLower, "https://") { |
| 121 | progressBarWidth := 0 |
| 122 | if showProgress { |
| 123 | progressBarWidth = 35 |
| 124 | } |
| 125 | tmpRootDir := deps.tempDir() |
| 126 | if tmpRootDir == "" { |
| 127 | return errors.New("failed to create temp directory") |
| 128 | } |
| 129 | downloadURL := rootPath |
| 130 | cacheRootPath := getDownloadCachePath(tmpRootDir, downloadURL) |
| 131 | cachePartialPath := cacheRootPath + ".part" |
| 132 | downloadCachePath = cacheRootPath |
| 133 | downloadPartialPath = cachePartialPath |
| 134 | downloadToCache := func() (string, error) { |
| 135 | if showProgress { |
| 136 | fmt.Println("Downloading...") |
| 137 | } |
| 138 | sum, err := download.DownloadFile(ctx, downloadURL, cachePartialPath, progressBarWidth) |
| 139 | if err != nil { |
| 140 | return "", err |
| 141 | } |
| 142 | if err := deps.renameFile(cachePartialPath, cacheRootPath); err != nil { |