2018-11-30 03:15:08 +00:00
|
|
|
package utils
|
2017-11-17 14:18:53 +00:00
|
|
|
|
|
|
|
import (
|
2017-11-17 15:07:03 +00:00
|
|
|
"errors"
|
2017-11-17 14:18:53 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-02-20 17:39:45 +00:00
|
|
|
"os/user"
|
2017-11-17 14:18:53 +00:00
|
|
|
"path"
|
2022-11-07 20:58:35 +00:00
|
|
|
"path/filepath"
|
2017-11-17 14:18:53 +00:00
|
|
|
)
|
|
|
|
|
2018-11-09 17:25:50 +00:00
|
|
|
func GetDefaultDBPath() string {
|
2017-11-17 14:18:53 +00:00
|
|
|
return "./"
|
|
|
|
}
|
|
|
|
|
2019-02-20 17:39:45 +00:00
|
|
|
func CheckDirExists(path string) (bool, error) {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return info.IsDir(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-11-09 17:25:50 +00:00
|
|
|
func CheckFileExists(file string) (bool, error) {
|
2017-11-17 15:07:03 +00:00
|
|
|
info, err := os.Stat(file)
|
|
|
|
if err == nil {
|
|
|
|
if info.IsDir() {
|
|
|
|
errMsg := fmt.Sprintf("'%s' is a directory", file)
|
|
|
|
return false, errors.New(errMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-11-09 17:25:50 +00:00
|
|
|
func CheckWriteable(dir string) error {
|
2017-11-17 14:18:53 +00:00
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if err == nil {
|
|
|
|
// dir exists, make sure we can write to it
|
|
|
|
testfile := path.Join(dir, "test")
|
|
|
|
fi, err := os.Create(testfile)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsPermission(err) {
|
|
|
|
return fmt.Errorf("%s is not writeable by the current user", dir)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("unexpected error while checking writeablility of repo root: %s", err)
|
|
|
|
}
|
|
|
|
fi.Close()
|
|
|
|
return os.Remove(testfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// dir doesnt exist, check that we can create it
|
|
|
|
return os.Mkdir(dir, 0775)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsPermission(err) {
|
|
|
|
return fmt.Errorf("cannot write to %s, incorrect permissions", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2019-02-20 17:39:45 +00:00
|
|
|
|
|
|
|
func GetHomeDir() string {
|
|
|
|
user, _ := user.Current()
|
|
|
|
return user.HomeDir
|
|
|
|
}
|
2022-11-07 20:58:35 +00:00
|
|
|
|
|
|
|
func ExpandPath(paths ...string) string {
|
|
|
|
return os.ExpandEnv(filepath.Join(paths...))
|
|
|
|
}
|