diff --git a/core/path.go b/core/path.go new file mode 100644 index 00000000..c2cf597a --- /dev/null +++ b/core/path.go @@ -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 = "" + } + } +} diff --git a/core/path_test.go b/core/path_test.go new file mode 100644 index 00000000..7f1d78da --- /dev/null +++ b/core/path_test.go @@ -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()) + } + }) + } +}