2022-07-15 11:49:53 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/juju/errors"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// IsGouickDirectory will check if we are in the same directory as the Gouick binary.
|
2022-07-25 20:43:53 +00:00
|
|
|
func IsGouickDirectory(path string) (bool, error) {
|
2022-07-16 22:16:29 +00:00
|
|
|
binPath, err := os.Executable()
|
2022-07-15 11:49:53 +00:00
|
|
|
if err != nil {
|
2022-07-16 22:16:29 +00:00
|
|
|
return false, errors.Trace(err)
|
2022-07-15 11:49:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 20:43:53 +00:00
|
|
|
gouickPath := filepath.Dir(binPath)
|
2022-07-16 22:16:29 +00:00
|
|
|
|
2022-07-25 20:43:53 +00:00
|
|
|
if gouickPath != path {
|
2022-07-16 22:16:29 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// IsGouickProject will check if a gouick config file exist and readeable.
|
2022-07-25 20:43:53 +00:00
|
|
|
func IsGouickProject(path string) (bool, error) {
|
2022-07-16 22:16:29 +00:00
|
|
|
exist, err := fileExists(filepath.Join(path, configFile))
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Trace(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exist {
|
2022-07-15 11:49:53 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// fileExists will check if a file exist and is readeable.
|
2022-07-16 22:16:29 +00:00
|
|
|
func fileExists(path string) (bool, error) {
|
|
|
|
fileInfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, fs.ErrPermission) {
|
|
|
|
return false, errors.NewForbidden(err, "file is not readeable")
|
|
|
|
} else if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
return false, errors.Trace(err)
|
|
|
|
}
|
2022-07-15 11:49:53 +00:00
|
|
|
}
|
2022-07-16 22:16:29 +00:00
|
|
|
if fileInfo.IsDir() {
|
|
|
|
return false, errors.NotValidf("%s is actually a directory", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
2022-07-15 11:49:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// isDirectoryWritable will check if a directory path is writable.
|
2022-07-15 11:49:53 +00:00
|
|
|
func isDirectoryWritable(path string) (bool, error) {
|
|
|
|
dirInfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, fs.ErrPermission) {
|
|
|
|
return false, errors.NewForbidden(err, "directory is not readeable")
|
|
|
|
} else if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return false, errors.NewNotFound(err, "directory does not exit")
|
|
|
|
} else {
|
|
|
|
return false, errors.Trace(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !dirInfo.IsDir() {
|
|
|
|
return false, errors.NotSupportedf("given path is not a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirInfo.Mode().Perm()&(1<<(uint(7))) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// createDirectory will create a new directory along with necessessary parents.
|
2022-07-15 11:49:53 +00:00
|
|
|
func createDirectory(path string) error {
|
2022-08-03 13:36:13 +00:00
|
|
|
// no need to check if path exist.
|
|
|
|
// MkdirAll will do it for us.
|
2022-07-15 11:49:53 +00:00
|
|
|
err := os.MkdirAll(path, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Trace(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckAndCreateDir will check if path is writable and create directory if needed
|
2022-08-03 13:36:13 +00:00
|
|
|
// return a bool true if directory created, false if not.
|
2022-08-02 20:30:46 +00:00
|
|
|
func CheckAndCreateDir(path string) (bool, error) {
|
2022-07-15 11:49:53 +00:00
|
|
|
dirWritable, err := isDirectoryWritable(path)
|
|
|
|
if dirWritable {
|
2022-08-02 20:30:46 +00:00
|
|
|
return false, nil
|
2022-07-15 11:49:53 +00:00
|
|
|
} else if err != nil && !errors.Is(err, errors.NotFound) {
|
|
|
|
log.Warnf("impossible to check if the directory is writable")
|
2022-08-02 20:30:46 +00:00
|
|
|
return false, err
|
2022-07-15 11:49:53 +00:00
|
|
|
} else if err == nil && !dirWritable {
|
|
|
|
log.Warnf("directory is not writable")
|
2022-08-02 20:30:46 +00:00
|
|
|
return false, errors.Forbiddenf("directory is not writable")
|
2022-07-15 11:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = createDirectory(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("impossible to create directory (%s), please try again", err.Error())
|
2022-08-02 20:30:46 +00:00
|
|
|
return false, err
|
2022-07-15 11:49:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:30:46 +00:00
|
|
|
return true, nil
|
2022-07-15 11:49:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:36:13 +00:00
|
|
|
// RemoveDirectoryContent will delete the content of a provided directory path.
|
2022-07-15 11:49:53 +00:00
|
|
|
func RemoveDirectoryContent(path string) error {
|
2022-08-03 11:17:15 +00:00
|
|
|
dir, err := os.ReadDir(path)
|
2022-07-15 11:49:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Trace(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dir {
|
|
|
|
err := os.RemoveAll(
|
2022-07-16 22:16:29 +00:00
|
|
|
filepath.Join(path, d.Name()),
|
2022-07-15 11:49:53 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Trace(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|