-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/jzero-io/jzero-contrib
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package filex | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// FileExists check file exist | ||
func FileExists(path string) bool { | ||
info, err := os.Stat(path) | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
return !info.IsDir() | ||
} | ||
|
||
// IsYamlFile check YAML file | ||
func IsYamlFile(path string) bool { | ||
ext := strings.ToLower(filepath.Ext(path)) | ||
return ext == ".yaml" || ext == ".yml" | ||
} | ||
|
||
// EnsureDirExists create dir with check | ||
func EnsureDirExists(dirPath string) error { | ||
info, err := os.Stat(dirPath) | ||
if os.IsNotExist(err) { | ||
err = os.MkdirAll(dirPath, 0o755) | ||
if err != nil { | ||
return fmt.Errorf("failed to create directory: %w", err) | ||
} | ||
} else if err != nil { | ||
return fmt.Errorf("failed to check directory: %w", err) | ||
} else if !info.IsDir() { | ||
return fmt.Errorf("path exists but is not a directory: %s", dirPath) | ||
} | ||
return nil | ||
} |