Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Dec 11, 2024
2 parents 55d9577 + 0e16366 commit ef65828
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions filex/filex.go
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
}

0 comments on commit ef65828

Please sign in to comment.