Skip to content

Commit 154c4e9

Browse files
authored
disable progressbar for mass pdsc downloads, issue #250 (#521)
## Fixes <!-- List the issue(s) this PR resolves --> - #250 ## Changes <!-- List the changes this PR introduces --> - ## Checklist <!-- Put an `x` in the boxes. All tasks must be completed and boxes checked before merging. --> - [ ] 🤖 This change is covered by unit tests (if applicable). - [x] 🤹 Manual testing has been performed (if necessary). - [ ] 🛡️ Security impacts have been considered (if relevant). - [ ] 📖 Documentation updates are complete (if required). - [ ] 🧠 Third-party dependencies and TPIP updated (if required).
1 parent 5d2e42c commit 154c4e9

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

cmd/installer/pack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (p *PackType) fetch(timeout int) error {
191191
log.Debugf("Fetching pack file \"%s\" (or just making sure it exists locally)", p.path)
192192
var err error
193193
if strings.HasPrefix(p.path, "http") {
194-
p.path, err = utils.DownloadFile(p.path, timeout)
194+
p.path, err = utils.DownloadFile(p.path, false, timeout)
195195
if err == errs.ErrTerminatedByUser {
196196
log.Infof("Aborting pack download. Removing \"%s\"", p.path)
197197
}

cmd/installer/root.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func RemovePdsc(pdscPath string) error {
391391
//
392392
// massDownloadPdscFiles(pdscTag, true, 30)
393393
func massDownloadPdscFiles(pdscTag xml.PdscTag, skipInstalledPdscFiles bool, timeout int, errTags *lockedSlice) {
394-
if err := Installation.downloadPdscFile(pdscTag, skipInstalledPdscFiles, timeout); err != nil {
394+
if err := Installation.downloadPdscFile(pdscTag, skipInstalledPdscFiles, true, timeout); err != nil {
395395
errTags.lock.Lock()
396396
errTags.slice = append(errTags.slice, pdscTag)
397397
errTags.lock.Unlock()
@@ -832,7 +832,7 @@ func UpdatePublicIndexIfOnline() error {
832832
// - overwrite: A boolean flag to indicate whether to overwrite the existing public index. This will be removed in future versions.
833833
// - sparse: A boolean flag to indicate whether to perform a sparse update.
834834
// - downloadPdsc: A boolean flag to indicate whether to download PDSC files.
835-
// - downloadRemainingPdscFiles: A boolean flag to indicate whether to download remaining PDSC files.
835+
// - downloadRemainingPdscFiles: A boolean flag to indicate whether to download all remaining PDSC files.
836836
// - concurrency: The number of concurrent operations allowed.
837837
// - timeout: The timeout duration for network operations.
838838
//
@@ -871,7 +871,7 @@ func UpdatePublicIndex(indexPath string, overwrite bool, sparse bool, downloadPd
871871
log.Warnf("Non-HTTPS url: \"%s\"", indexPath)
872872
}
873873

874-
indexPath, err = utils.DownloadFile(indexPath, timeout)
874+
indexPath, err = utils.DownloadFile(indexPath, false, timeout)
875875
if err != nil {
876876
return err
877877
}
@@ -1306,14 +1306,14 @@ func FindPackURL(pack *PackType) (string, error) {
13061306
})
13071307
if len(tags) != 0 {
13081308
tags[0].Version = ""
1309-
if err := Installation.downloadPdscFile(tags[0], true, 0); err != nil {
1309+
if err := Installation.downloadPdscFile(tags[0], true, false, 0); err != nil {
13101310
return "", err
13111311
}
13121312
} else {
13131313
return "", errs.ErrPdscEntryNotFound
13141314
}
13151315
} else {
1316-
if err := Installation.downloadPdscFile(xml.PdscTag{URL: pack.URL, Vendor: pack.Vendor, Name: pack.Name}, true, 0); err != nil {
1316+
if err := Installation.downloadPdscFile(xml.PdscTag{URL: pack.URL, Vendor: pack.Vendor, Name: pack.Name}, true, false, 0); err != nil {
13171317
return "", err
13181318
}
13191319
}
@@ -1861,18 +1861,25 @@ func (p *PacksInstallationType) packIsPublic(pack *PackType, pdscTag *xml.PdscTa
18611861
return true, nil
18621862
}
18631863

1864-
// downloadPdscFile downloads a PDSC file based on the provided pdscTag and saves it to the specified location.
1865-
// If skipInstalledPdscFiles is true and the file already exists, the function will skip the download.
1866-
// The function also handles switching to a cache URL if necessary and ensures the file is moved to the correct location.
1864+
// downloadPdscFile downloads a PDSC (Pack Description) file from a specified URL and saves it to the local file system.
1865+
// It handles scenarios such as skipping already installed files, switching to a cache URL, and managing file permissions.
18671866
//
18681867
// Parameters:
1869-
// - pdscTag: An xml.PdscTag containing the vendor, name, and URL of the PDSC file.
1870-
// - skipInstalledPdscFiles: A boolean indicating whether to skip downloading if the file already exists.
1871-
// - timeout: An integer specifying the timeout duration for the download.
1868+
// - pdscTag: An xml.PdscTag object containing metadata about the PDSC file to be downloaded.
1869+
// - skipInstalledPdscFiles: A boolean flag indicating whether to skip downloading if the PDSC file already exists locally.
1870+
// - noProgressBar: A boolean flag indicating whether to suppress the progress bar during the download.
1871+
// - timeout: An integer specifying the timeout duration (in seconds) for the download operation.
18721872
//
18731873
// Returns:
1874-
// - error: An error if any issues occur during the download or file operations.
1875-
func (p *PacksInstallationType) downloadPdscFile(pdscTag xml.PdscTag, skipInstalledPdscFiles bool, timeout int) error {
1874+
// - error: An error object if any issues occur during the download or file operations, or nil if the operation succeeds.
1875+
//
1876+
// Behavior:
1877+
// - If skipInstalledPdscFiles is true and the file already exists locally, the function returns without downloading.
1878+
// - If the PDSC file's URL is not the default Keil pack root and the public index XML URL matches the default root,
1879+
// the function switches to the cache URL for downloading.
1880+
// - The function downloads the PDSC file, temporarily saves it, and then moves it to the target location,
1881+
// ensuring proper file permissions are set before and after the operation.
1882+
func (p *PacksInstallationType) downloadPdscFile(pdscTag xml.PdscTag, skipInstalledPdscFiles, noProgressBar bool, timeout int) error {
18761883
basePdscFile := fmt.Sprintf("%s.pdsc", pdscTag.VName())
18771884
pdscFilePath := filepath.Join(p.WebDir, basePdscFile)
18781885

@@ -1902,7 +1909,7 @@ func (p *PacksInstallationType) downloadPdscFile(pdscTag xml.PdscTag, skipInstal
19021909

19031910
pdscFileURL.Path = path.Join(pdscFileURL.Path, basePdscFile)
19041911

1905-
localFileName, err := utils.DownloadFile(pdscFileURL.String(), timeout)
1912+
localFileName, err := utils.DownloadFile(pdscFileURL.String(), noProgressBar, timeout)
19061913
defer os.Remove(localFileName)
19071914

19081915
if err != nil {
@@ -1964,7 +1971,7 @@ func (p *PacksInstallationType) loadPdscFile(pdscTag xml.PdscTag, timeout int) e
19641971
return nil
19651972
}
19661973

1967-
localFileName, err := utils.DownloadFile(pdscFileURL.String(), timeout)
1974+
localFileName, err := utils.DownloadFile(pdscFileURL.String(), false, timeout)
19681975
defer os.Remove(localFileName)
19691976

19701977
if err != nil {

cmd/utils/utils.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,25 @@ var (
106106
DirModeRW = fs.FileMode(0777)
107107
)
108108

109-
// DownloadFile downloads a file from an URL and saves it locally under destionationFilePath
110-
func DownloadFile(URL string, timeout int) (string, error) {
109+
// DownloadFile downloads a file from the specified URL and saves it to the cache directory.
110+
// If the file already exists in the cache, it skips the download and returns the cached file path.
111+
//
112+
// Parameters:
113+
// - URL: The URL of the file to download.
114+
// - noProgressBar: A boolean flag to disable the progress bar during the download.
115+
// - timeout: The timeout in seconds for the HTTP request. If set to 0, no timeout is applied.
116+
//
117+
// Returns:
118+
// - string: The file path of the downloaded (or cached) file.
119+
// - error: An error if the download or file creation fails.
120+
//
121+
// Behavior:
122+
// - If the file exists in the cache, it is reused without downloading.
123+
// - For HTTPS requests to "https://127.0.0.1", TLS verification is skipped.
124+
// - Handles HTTP redirects, cookies, and retries for specific HTTP status codes (e.g., 404, 403).
125+
// - Displays a progress bar unless disabled via the noProgressBar parameter or if the terminal is non-interactive.
126+
// - Ensures secure file writing and cleans up partially downloaded files in case of errors.
127+
func DownloadFile(URL string, noProgressBar bool, timeout int) (string, error) {
111128
parsedURL, _ := url.Parse(URL)
112129
fileBase := path.Base(parsedURL.Path)
113130
filePath := filepath.Join(CacheDir, fileBase)
@@ -200,7 +217,7 @@ func DownloadFile(URL string, timeout int) (string, error) {
200217
writers = append(writers, progressWriter)
201218
instCnt++
202219
} else {
203-
if IsTerminalInteractive() {
220+
if !noProgressBar && IsTerminalInteractive() {
204221
progressWriter := progressbar.DefaultBytes(length, "I:")
205222
writers = append(writers, progressWriter)
206223
}

cmd/utils/utils_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestDownloadFile(t *testing.T) {
4545
},
4646
),
4747
)
48-
_, err := utils.DownloadFile(goodServer.URL+"/file.txt", 0)
48+
_, err := utils.DownloadFile(goodServer.URL+"/file.txt", false, 0)
4949
assert.NotNil(err)
5050
assert.True(errs.Is(err, errs.ErrFailedCreatingFile))
5151
utils.CacheDir = oldCache
@@ -55,7 +55,7 @@ func TestDownloadFile(t *testing.T) {
5555
fileName := "file.txt"
5656
defer os.Remove(fileName)
5757

58-
_, err := utils.DownloadFile(fileName, 0)
58+
_, err := utils.DownloadFile(fileName, false, 0)
5959
assert.NotNil(err)
6060
assert.Equal(errors.Unwrap(err), errs.ErrFailedDownloadingFile)
6161
})
@@ -71,7 +71,7 @@ func TestDownloadFile(t *testing.T) {
7171
),
7272
)
7373

74-
_, err := utils.DownloadFile(notFoundServer.URL+"/"+fileName, 0)
74+
_, err := utils.DownloadFile(notFoundServer.URL+"/"+fileName, false, 0)
7575
assert.NotNil(err)
7676
assert.Equal(errors.Unwrap(err), errs.ErrBadRequest)
7777
assert.False(utils.FileExists(fileName))
@@ -89,7 +89,7 @@ func TestDownloadFile(t *testing.T) {
8989
),
9090
)
9191

92-
_, err := utils.DownloadFile(bodyErrorServer.URL+"/"+fileName, 0)
92+
_, err := utils.DownloadFile(bodyErrorServer.URL+"/"+fileName, false, 0)
9393
assert.NotNil(err)
9494
assert.True(errs.Is(err, errs.ErrFailedWrittingToLocalFile))
9595
})
@@ -106,7 +106,7 @@ func TestDownloadFile(t *testing.T) {
106106
),
107107
)
108108
url := goodServer.URL + "/" + fileName
109-
_, err1 := utils.DownloadFile(url, 0)
109+
_, err1 := utils.DownloadFile(url, false, 0)
110110
assert.Nil(err1)
111111
assert.True(utils.FileExists(fileName))
112112
bytes, err2 := os.ReadFile(fileName)
@@ -132,7 +132,7 @@ func TestDownloadFile(t *testing.T) {
132132
),
133133
)
134134
url := goodServer.URL + "/" + fileName
135-
_, err1 := utils.DownloadFile(url, 0)
135+
_, err1 := utils.DownloadFile(url, false, 0)
136136
assert.Nil(err1)
137137
assert.True(utils.FileExists(fileName))
138138
bytes, err2 := os.ReadFile(fileName)
@@ -161,7 +161,7 @@ func TestDownloadFile(t *testing.T) {
161161
),
162162
)
163163
url := goodServer.URL + "/" + fileName
164-
_, err1 := utils.DownloadFile(url, 0)
164+
_, err1 := utils.DownloadFile(url, false, 0)
165165
assert.Nil(err1)
166166
assert.True(utils.FileExists(fileName))
167167
bytes, err2 := os.ReadFile(fileName)
@@ -183,7 +183,7 @@ func TestDownloadFile(t *testing.T) {
183183
),
184184
)
185185
url := goodServer.URL + "/" + fileName
186-
_, err1 := utils.DownloadFile(url, 0)
186+
_, err1 := utils.DownloadFile(url, false, 0)
187187
assert.Nil(err1)
188188
assert.True(utils.FileExists(fileName))
189189
bytes, err2 := os.ReadFile(fileName)
@@ -192,7 +192,7 @@ func TestDownloadFile(t *testing.T) {
192192
assert.Equal(1, requestCount)
193193

194194
// Download it again, this time it shouldn't trigger any HTTP request
195-
_, err1 = utils.DownloadFile(url, 0)
195+
_, err1 = utils.DownloadFile(url, false, 0)
196196
assert.Nil(err1)
197197
assert.Equal(1, requestCount)
198198
})

0 commit comments

Comments
 (0)