filepath.go 582 B

123456789101112131415161718192021222324252627
  1. package filepathext
  2. import (
  3. "path/filepath"
  4. "runtime"
  5. "strings"
  6. )
  7. // SmartJoin joins two paths, treating the second path as absolute if it is an
  8. // absolute path.
  9. func SmartJoin(one, two string) string {
  10. if SmartIsAbs(two) {
  11. return two
  12. }
  13. return filepath.Join(one, two)
  14. }
  15. // SmartIsAbs checks if a path is absolute, considering both OS-specific and
  16. // Unix-style paths.
  17. func SmartIsAbs(path string) bool {
  18. switch runtime.GOOS {
  19. case "windows":
  20. return filepath.IsAbs(path) || strings.HasPrefix(filepath.ToSlash(path), "/")
  21. default:
  22. return filepath.IsAbs(path)
  23. }
  24. }