path.go 529 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package helper
  2. import (
  3. "fmt"
  4. "os"
  5. "os/user"
  6. "strings"
  7. )
  8. func PathNormalize(path string) string {
  9. path = strings.Trim(path, " ")
  10. if len(path) == 0 {
  11. return path
  12. }
  13. if path[0] == '/' {
  14. return path
  15. }
  16. // Expand home path
  17. if path[0] == '~' {
  18. u, err := user.Current()
  19. if err != nil {
  20. // TODO: log
  21. return path
  22. }
  23. return fmt.Sprintf("%s%s", u.HomeDir, path[1:])
  24. }
  25. // Expand relative path
  26. d, err := os.Getwd()
  27. if err != nil {
  28. // TODO: log
  29. return path
  30. }
  31. return fmt.Sprintf("%s/%s", d, path)
  32. }