github.com/chain5j/chain5j-pkg@v1.0.7/util/ioutil/ioutil.go (about) 1 // Package ioutil 2 // 3 // @author: xwc1125 4 package ioutil 5 6 import ( 7 "os" 8 "os/user" 9 "path/filepath" 10 "runtime" 11 ) 12 13 // PathExists 路径是否存在 14 func PathExists(path string) bool { 15 _, err := os.Stat(path) 16 if err == nil { 17 return true 18 } 19 if os.IsNotExist(err) { 20 return false 21 } 22 return false 23 } 24 25 // GetProjectPath 获取项目的路径 26 func GetProjectPath() (dir string, err error) { 27 return os.Getwd() 28 } 29 30 // DefaultDataDir 默认路径 31 func DefaultDataDir() string { 32 // Try to place the data folder in the user's home dir 33 home := homeDir() 34 if home != "" { 35 if runtime.GOOS == "darwin" { 36 return filepath.Join(home, "Library", "Chain5j") 37 } else if runtime.GOOS == "windows" { 38 return filepath.Join(home, "AppData", "Roaming", "Chain5j") 39 } else { 40 return filepath.Join(home, ".chain5j") 41 } 42 } 43 // As we cannot guess a stable location, return empty and handle later 44 return "" 45 } 46 47 // homeDir Home路径 48 func homeDir() string { 49 if home := os.Getenv("HOME"); home != "" { 50 return home 51 } 52 if usr, err := user.Current(); err == nil { 53 return usr.HomeDir 54 } 55 return "" 56 } 57 58 // MakeDirAll 创建文件夹 59 func MakeDirAll(path string) error { 60 return os.MkdirAll(path, os.ModePerm) 61 } 62 63 func MakeParentDir(filePath string) error { 64 dir, err := filepath.Abs(filepath.Dir(filePath)) 65 if err != nil { 66 return err 67 } 68 return MakeDirAll(dir) 69 }