mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-09 17:22:21 +08:00
Implements Path.PopLastSegment().
This allows a path (/ipfs/foo/bar) to be separated between its head (/ipfs/foo) and its tail (bar). License: MIT Signed-off-by: Stephen Whitmore <noffle@ipfs.io>
This commit is contained in:
18
path/path.go
18
path/path.go
@ -50,6 +50,24 @@ func (p Path) IsJustAKey() bool {
|
||||
return (len(parts) == 2 && parts[0] == "ipfs")
|
||||
}
|
||||
|
||||
// PopLastSegment returns a new Path without its final segment, and the final
|
||||
// segment, separately. If there is no more to pop (the path is just a key),
|
||||
// the original path is returned.
|
||||
func (p Path) PopLastSegment() (Path, string, error) {
|
||||
|
||||
if p.IsJustAKey() {
|
||||
return p, "", nil
|
||||
}
|
||||
|
||||
segs := p.Segments()
|
||||
newPath, err := ParsePath("/" + strings.Join(segs[:len(segs)-1], "/"))
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return newPath, segs[len(segs)-1], nil
|
||||
}
|
||||
|
||||
func FromSegments(prefix string, seg ...string) (Path, error) {
|
||||
return ParsePath(prefix + strings.Join(seg, "/"))
|
||||
}
|
||||
|
Reference in New Issue
Block a user