aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/turbopath/relative_system_path.go
diff options
context:
space:
mode:
author简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
committer简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
commitdd84b9d64fb98746a230cd24233ff50a562c39c9 (patch)
treeb583261ef00b3afe72ec4d6dacb31e57779a6faf /cli/internal/turbopath/relative_system_path.go
parent0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff)
downloadHydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz
HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip
Diffstat (limited to 'cli/internal/turbopath/relative_system_path.go')
-rw-r--r--cli/internal/turbopath/relative_system_path.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/cli/internal/turbopath/relative_system_path.go b/cli/internal/turbopath/relative_system_path.go
new file mode 100644
index 0000000..d6115db
--- /dev/null
+++ b/cli/internal/turbopath/relative_system_path.go
@@ -0,0 +1,44 @@
+package turbopath
+
+import (
+ "fmt"
+ "path/filepath"
+)
+
+// RelativeSystemPath is a relative path using system separators.
+type RelativeSystemPath string
+
+// CheckedToRelativeSystemPath inspects a string and determines if it is a relative path.
+func CheckedToRelativeSystemPath(s string) (RelativeSystemPath, error) {
+ if filepath.IsAbs(s) {
+ return "", fmt.Errorf("%v is not a relative path", s)
+ }
+ return RelativeSystemPath(filepath.Clean(s)), nil
+}
+
+// MakeRelativeSystemPath joins the given segments in a system-appropriate way
+func MakeRelativeSystemPath(segments ...string) RelativeSystemPath {
+ return RelativeSystemPath(filepath.Join(segments...))
+}
+
+// ToString returns a string represenation of this Path.
+// Used for interfacing with APIs that require a string.
+func (p RelativeSystemPath) ToString() string {
+ return string(p)
+}
+
+// ToSystemPath returns itself.
+func (p RelativeSystemPath) ToSystemPath() RelativeSystemPath {
+ return p
+}
+
+// ToUnixPath converts from RelativeSystemPath to RelativeUnixPath.
+func (p RelativeSystemPath) ToUnixPath() RelativeUnixPath {
+ return RelativeUnixPath(filepath.ToSlash(p.ToString()))
+}
+
+// Join appends relative path segments to this RelativeSystemPath.
+func (p RelativeSystemPath) Join(additional ...RelativeSystemPath) RelativeSystemPath {
+ cast := RelativeSystemPathArray(additional)
+ return RelativeSystemPath(filepath.Join(p.ToString(), filepath.Join(cast.ToStringArray()...)))
+}