func OpenFile(filePath string, perm os.FileMode) (*os.File, error) {
	file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, perm)
	if err != nil {
		return nil, errors.Wrapf(err, "error while opening file:%s", filePath)
	}
	if err := SyncDir(filepath.Dir(filePath)); err != nil {
		if closeErr := file.Close(); closeErr != nil {
			log.Printf("error while closing the file [%s]", file.Name())
		}
		return nil, errors.Wrap(err, "error while synching the opened file")
	}
	return file, nil
}
OpenFile opens an existing file. If the file does not exist, it creates
a new one.
package fileops
import (
	"io"
	"io/ioutil"
	"log"
	"os"
	"path"
	"path/filepath"
	"strings"
	"github.com/pkg/errors"
)
func IsDirEmpty(dirPath string) (bool, error) {
	f, err := os.Open(dirPath)
	if err != nil {
		return false, errors.WithMessagef(err, "error opening dir [%s]", dirPath)
	}
	defer f.Close()
	_, err = f.Readdir(1)
	if err == io.EOF {
		return true, nil
	}
	if err != nil {
		return false, errors.WithMessagef(err, "error checking if dir [%s] is empty", dirPath)
	}
	return false, nil
}
func ListSubdirs(dirPath string) ([]string, error) {
	var subdirs []string
	files, err := ioutil.ReadDir(dirPath)
	if err != nil {
		return nil, errors.Wrapf(err, "error reading dir [%s]", dirPath)
	}
	for _, f := range files {
		if f.IsDir() {
			subdirs = append(subdirs, f.Name())
		}
	}
	return subdirs, nil
}
func Exists(filePath string) (bool, error) {
	_, err := os.Stat(filePath)
	if os.IsNotExist(err) {
		return false, nil
	}
	if err != nil {
		return false, errors.Wrapf(err, "error checking if file [%s] exists", filePath)
	}
	return true, nil
}
func CreateDir(dirPath string) error {
	if !strings.HasSuffix(dirPath, "/") {
		dirPath = dirPath + "/"
	}
	if err := os.MkdirAll(path.Dir(dirPath), 0755); err != nil {
		return err
	}
	return SyncDir(filepath.Dir(dirPath))
}