package utils import ( "fmt" "github.com/h2non/filetype" "github.com/h2non/filetype/types" "io/ioutil" "os" "os/user" "path/filepath" ) func FileType(filePath string) (types.Type, error) { file, _ := os.Open(filePath) // We only have to pass the file header = first 261 bytes head := make([]byte, 261) _, _ = file.Read(head) return filetype.Match(head) } func FileExists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { return true, nil } else if os.IsNotExist(err) { return false, err } else { panic(err) } } func DirExists(path string) (bool, error) { exists, _ := FileExists(path) fileInfo, _ := os.Stat(path) if !exists || !fileInfo.IsDir() { return false, fmt.Errorf("path either doesn't exist, or is not a directory <%s>", path) } return true, nil } func Touch(path string) error { var _, err = os.Stat(path) if os.IsNotExist(err) { var file, err = os.Create(path) if err != nil { return err } defer file.Close() } return nil } func EnsureDir(path string) error { exists, err := FileExists(path) if !exists { err = os.Mkdir(path, 0755) return err } return err } func RemoveDir(path string) error { return os.RemoveAll(path) } func EmptyDir(path string) error { d, err := os.Open(path) if err != nil { return err } defer d.Close() names, err := d.Readdirnames(-1) if err != nil { return err } for _, name := range names { err = os.RemoveAll(filepath.Join(path, name)) if err != nil { return err } } return nil } func ListDir(path string) []string { if path == "" { path = GetHomeDirectory() } absolutePath, err := filepath.Abs(path) if err == nil { path = absolutePath } files, err := ioutil.ReadDir(path) if err != nil { path = filepath.Dir(path) files, err = ioutil.ReadDir(path) } var dirPaths []string for _, file := range files { if !file.IsDir() { continue } abs, err := filepath.Abs(path) if err != nil { continue } dirPaths = append(dirPaths, filepath.Join(abs, file.Name())) } return dirPaths } func GetHomeDirectory() string { currentUser, err := user.Current() if err != nil { panic(err) } return currentUser.HomeDir }