-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package core | ||
|
||
import "strings" | ||
|
||
type Path struct { | ||
origin string | ||
fsType VFSType | ||
bucket string | ||
base string | ||
} | ||
|
||
func newPath(path string, fsType VFSType) Path { | ||
p := Path{ | ||
origin: path, | ||
fsType: fsType, | ||
} | ||
p.parse() | ||
return p | ||
} | ||
|
||
// Bucket returns bucket name | ||
func (p Path) Bucket() string { | ||
return p.bucket | ||
} | ||
|
||
// Base returns base path | ||
func (p Path) Base() string { | ||
return p.base | ||
} | ||
|
||
// String return the origin path | ||
func (p Path) String() string { | ||
return p.origin | ||
} | ||
|
||
func (p *Path) parse() { | ||
p.base = p.origin | ||
|
||
if p.fsType == MinIO { | ||
// protocol => bucket:path | ||
// example => mybucket:/workspace | ||
if strings.Contains(p.origin, ":") && !strings.HasPrefix(p.origin, "/") { | ||
list := strings.Split(p.origin, ":") | ||
p.bucket = list[0] | ||
p.base = list[1] | ||
} else { | ||
p.bucket = p.origin | ||
p.base = "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNewPath(t *testing.T) { | ||
testCases := []struct { | ||
path string | ||
fsType VFSType | ||
expectBucket string | ||
expectBasePath string | ||
}{ | ||
{"/workspace", Disk, "", "/workspace"}, | ||
{"/workspace", SFTP, "", "/workspace"}, | ||
{"myBucket:/workspace", MinIO, "myBucket", "/workspace"}, | ||
{"myBucket", MinIO, "myBucket", ""}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.path, func(t *testing.T) { | ||
p := newPath(tc.path, tc.fsType) | ||
if p.Bucket() != tc.expectBucket { | ||
t.Errorf("test new path error, expect bucket:%s, actual:%s", tc.expectBucket, p.Bucket()) | ||
return | ||
} | ||
if p.Base() != tc.expectBasePath { | ||
t.Errorf("test new path error, expect base path:%s, actual:%s", tc.expectBasePath, p.Base()) | ||
return | ||
} | ||
if p.String() != tc.path { | ||
t.Errorf("test new path error, expect path:%s, actual:%s", tc.path, p.String()) | ||
} | ||
}) | ||
} | ||
} |