-
Notifications
You must be signed in to change notification settings - Fork 6
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
4 changed files
with
129 additions
and
25 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
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
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
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,89 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// JoinPath makes the path from dir and file paths | ||
func JoinPath(dirPath string, filePath string) string { | ||
if strings.HasSuffix(dirPath, "/") { | ||
return fmt.Sprintf("%s/%s", dirPath[0:len(dirPath)-1], filePath) | ||
} | ||
return fmt.Sprintf("%s/%s", dirPath, filePath) | ||
} | ||
|
||
// SplitPath splits the path into dir and file | ||
func SplitPath(p string) (string, string) { | ||
return filepath.Split(p) | ||
} | ||
|
||
// GetDirname returns the dir of the path | ||
func GetDirname(p string) string { | ||
return filepath.Dir(p) | ||
} | ||
|
||
// GetFileName returns the filename of the path | ||
func GetFileName(p string) string { | ||
return filepath.Base(p) | ||
} | ||
|
||
// IsAbsolutePath returns true if the path is absolute | ||
func IsAbsolutePath(p string) bool { | ||
return strings.HasPrefix(p, "/") | ||
} | ||
|
||
// GetPathDepth returns depth of the path | ||
// "/" returns 0 | ||
// "abc" returns -1 | ||
// "/abc" returns 0 | ||
// "/a/b" returns 1 | ||
// "/a/b/c" returns 2 | ||
func GetPathDepth(p string) int { | ||
if !strings.HasPrefix(p, "/") { | ||
return -1 | ||
} | ||
|
||
cleanPath := path.Clean(p) | ||
|
||
if cleanPath == "/" { | ||
return 0 | ||
} | ||
|
||
pArr := strings.Split(p[1:], "/") | ||
return len(pArr) - 1 | ||
} | ||
|
||
// GetParentDirs returns all parent dirs | ||
func GetParentDirs(p string) []string { | ||
parents := []string{} | ||
|
||
if p == "/" { | ||
return parents | ||
} | ||
|
||
curPath := p | ||
for len(curPath) > 0 && curPath != "/" { | ||
curDir := GetDirname(curPath) | ||
if len(curDir) > 0 { | ||
parents = append(parents, curDir) | ||
} | ||
|
||
curPath = curDir | ||
} | ||
|
||
// sort | ||
sort.Slice(parents, func(i int, j int) bool { | ||
return len(parents[i]) < len(parents[j]) | ||
}) | ||
|
||
return parents | ||
} | ||
|
||
// GetRelativePath returns relative path | ||
func GetRelativePath(p1 string, p2 string) (string, error) { | ||
return filepath.Rel(p1, p2) | ||
} |