Skip to content

Commit

Permalink
fix: check same name for downloading files (#98)
Browse files Browse the repository at this point in the history
* fix: check same name for downloading files

* chore: adjust code
  • Loading branch information
fireyun committed May 29, 2024
1 parent 6347aac commit a154fac
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions cmd/bscp/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,22 +333,26 @@ func runDownloadFile(bscp client.Client, app string, match []string) error {

dstFiles := make([]string, len(release.FileItems))
var dstFile string
var existFiles []string
fileNames := make(map[string][]string)
for idx, f := range release.FileItems {
if ignoreDir {
dstFile = path.Join(downloadDir, f.Name)
// check if file exists when --ignore-dir is enabled
if _, err := os.Stat(dstFile); err == nil {
existFiles = append(existFiles, dstFile)
}
// used to check files with the same name when --ignore-dir is enabled
fileNames[f.Name] = append(fileNames[f.Name], path.Join(f.Path, f.Name))
} else {
dstFile = path.Join(downloadDir, f.Path, f.Name)
}
dstFiles[idx] = dstFile
}
if len(existFiles) > 0 {
return fmt.Errorf("the file in %v already exists, "+
"you can remove the arg --ignore-dir or delete the existed files or make your other choices", existFiles)
var sameFiles [][]string
for _, names := range fileNames {
if len(names) >= 2 {
sameFiles = append(sameFiles, names)
}
}
if len(sameFiles) > 0 {
return fmt.Errorf("the file names are same for files in %v, we don't download and override directly, "+
"you can remove the arg --ignore-dir or make your other choices", sameFiles)
}

// save content to dst file
Expand Down

0 comments on commit a154fac

Please sign in to comment.