GAAS 广汽安道拓GFrame金属件MOM项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
1.9 KiB

  1. package utils
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "strings"
  7. )
  8. func GetCurrentDir() string {
  9. file, err := exec.LookPath(os.Args[0])
  10. if err != nil {
  11. c, _ := os.Getwd()
  12. return c
  13. }
  14. return strings.Replace(filepath.Dir(file), "\\", "/", -1)
  15. }
  16. func GetAbsolutePath(path string) string {
  17. p := GetAbsolutePathNoFilter(path)
  18. p = strings.Replace(p, "\\", "/", -1) // 统一使用/
  19. for {
  20. a := strings.Index(p, "//")
  21. if a == -1 {
  22. break
  23. }
  24. p = strings.Replace(p, "//", "/", -1)
  25. }
  26. return p
  27. }
  28. func GetAbsolutePathNoFilter(path string) string {
  29. if strings.Index(path, "file:/") == 0 {
  30. path = path[5:]
  31. mpos := strings.Index(path, ":")
  32. if mpos >= 0 {
  33. //去除开头斜杠, 如: ///C:/test
  34. for {
  35. if len(path) > 0 && (path[0] == '/' || path[0] == '\\') {
  36. path = path[1:]
  37. } else {
  38. break
  39. }
  40. }
  41. return (path)
  42. }
  43. for {
  44. if len(path) > 1 && (path[0] == '/' || path[0] == '\\') && (path[1] == '/' || path[1] == '\\') {
  45. path = path[1:]
  46. } else {
  47. break
  48. }
  49. }
  50. path, _ = filepath.Abs(path)
  51. return (path)
  52. } else if strings.Index(path, "./") == 0 || strings.Index(path, ".\\") == 0 {
  53. r, _ := filepath.Abs(GetCurrentDir() + path[1:])
  54. return (r)
  55. }
  56. r, _ := filepath.Abs(path)
  57. return (r)
  58. }
  59. func GetDir(path string) string {
  60. if strings.Index(path, "./") == 0 || strings.Index(path, ".\\") == 0 {
  61. return filepath.ToSlash(filepath.Dir(GetCurrentDir() + path[1:]))
  62. }
  63. return filepath.ToSlash(filepath.Dir(path))
  64. }
  65. func EnsureDir(dir string) string {
  66. fullDir := GetAbsolutePath(dir)
  67. if IsExists(fullDir) {
  68. return fullDir
  69. }
  70. os.MkdirAll(fullDir, 777)
  71. return fullDir
  72. }
  73. func EnsureFilePath(filePath string) string {
  74. filePath = GetAbsolutePath(filePath)
  75. EnsureDir(GetDir(filePath))
  76. return filePath
  77. }
  78. func IsExists(path string) bool {
  79. if path == "" {
  80. return false
  81. }
  82. _, err := os.Stat(path)
  83. if err != nil && os.IsNotExist(err) {
  84. return false
  85. }
  86. return true
  87. }