Skip to content

Commit

Permalink
fix: make cross-plattform compatible filepaths/keys (#4040)
Browse files Browse the repository at this point in the history
  • Loading branch information
lippertmarkus authored Sep 18, 2020
1 parent e94b964 commit e2f4966
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions workflow/artifacts/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func downloadObjects(client *storage.Client, bucket, key, path string) error {
// download an object from the bucket
func downloadObject(client *storage.Client, bucket, key, objName, path string) error {
objPrefix := filepath.Clean(key)
if os.PathSeparator == '\\' {
objPrefix = strings.ReplaceAll(objPrefix, "\\", "/")
}

relObjPath := strings.TrimPrefix(objName, objPrefix)
localPath := filepath.Join(path, relObjPath)
objectDir, _ := filepath.Split(localPath)
Expand Down Expand Up @@ -179,7 +183,7 @@ func listFileRelPaths(path string, relPath string) ([]string, error) {
}
for _, file := range files {
if file.IsDir() {
fs, err := listFileRelPaths(path+file.Name()+"/", relPath+file.Name()+"/")
fs, err := listFileRelPaths(path+file.Name()+string(os.PathSeparator), relPath+file.Name()+string(os.PathSeparator))
if err != nil {
return nil, err
}
Expand All @@ -198,20 +202,29 @@ func uploadObjects(client *storage.Client, bucket, key, path string) error {
return fmt.Errorf("test if %s is a dir: %v", path, err)
}
if isDir {
dirName := filepath.Clean(path) + "/"
dirName := filepath.Clean(path) + string(os.PathSeparator)
keyPrefix := filepath.Clean(key) + "/"
fileRelPaths, err := listFileRelPaths(dirName, "")
if err != nil {
return err
}
for _, relPath := range fileRelPaths {
err = uploadObject(client, bucket, keyPrefix+relPath, dirName+relPath)
fullKey := keyPrefix + relPath
if os.PathSeparator == '\\' {
fullKey = strings.ReplaceAll(fullKey, "\\", "/")
}

err = uploadObject(client, bucket, fullKey, dirName+relPath)
if err != nil {
return fmt.Errorf("upload %s: %v", dirName+relPath, err)
}
}
} else {
err = uploadObject(client, bucket, filepath.Clean(key), path)
objectKey := filepath.Clean(key)
if os.PathSeparator == '\\' {
objectKey = strings.ReplaceAll(objectKey, "\\", "/")
}
err = uploadObject(client, bucket, objectKey, path)
if err != nil {
return fmt.Errorf("upload %s: %v", path, err)
}
Expand Down

0 comments on commit e2f4966

Please sign in to comment.