mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
util: refactor checking if path exists
This commit is contained in:
parent
e1d49e17a1
commit
036a5d3ab7
1 changed files with 6 additions and 14 deletions
|
|
@ -2,6 +2,7 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -13,15 +14,9 @@ import (
|
||||||
//
|
//
|
||||||
// Returns whether the path exists and no error if successful,
|
// Returns whether the path exists and no error if successful,
|
||||||
// otherwise, it returns false with an error.
|
// otherwise, it returns false with an error.
|
||||||
func PathExists(path string) (bool, error) {
|
func PathExists(path string) (fs.FileInfo, bool) {
|
||||||
_, err := os.Stat(path)
|
fi, err := os.Stat(path)
|
||||||
if err == nil {
|
return fi, !os.IsNotExist(err)
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SplitPathForViper() is an utility function to split a path into 3 parts:
|
// SplitPathForViper() is an utility function to split a path into 3 parts:
|
||||||
|
|
@ -51,17 +46,14 @@ func MakeOutputDirectory(path string, overwrite bool) (string, error) {
|
||||||
final := path + "/" + dirname
|
final := path + "/" + dirname
|
||||||
|
|
||||||
// check if path is valid and directory
|
// check if path is valid and directory
|
||||||
pathExists, err := PathExists(final)
|
_, pathExists := PathExists(final)
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to check for existing path: %v", err)
|
|
||||||
}
|
|
||||||
if pathExists && !overwrite {
|
if pathExists && !overwrite {
|
||||||
// make sure it is directory with 0o644 permissions
|
// make sure it is directory with 0o644 permissions
|
||||||
return "", fmt.Errorf("found existing path: %v", final)
|
return "", fmt.Errorf("found existing path: %v", final)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create directory with data + time
|
// create directory with data + time
|
||||||
err = os.MkdirAll(final, 0766)
|
err := os.MkdirAll(final, 0766)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to make directory: %v", err)
|
return "", fmt.Errorf("failed to make directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue