github.com/editorconfig-checker/editorconfig-checker@v0.0.0-20231102090242-ddae3e68851e/pkg/utils/utils.go (about) 1 // Package utils provides functions that are considered utility for being project independent 2 package utils 3 4 import ( 5 "os" 6 "path/filepath" 7 ) 8 9 // GetEolChar returns the end of line character used in regexp 10 // depending on the end_of_line parameter 11 func GetEolChar(endOfLine string) string { 12 switch endOfLine { 13 case "lf": 14 return "\n" 15 16 case "cr": 17 return "\r" 18 case "crlf": 19 return "\r\n" 20 } 21 22 return "\n" 23 } 24 25 // IsRegularFile returns whether a file is a regular file or not 26 func IsRegularFile(filePath string) bool { 27 absolutePath, firstErr := filepath.Abs(filePath) 28 fi, secondErr := os.Stat(absolutePath) 29 return firstErr == nil && secondErr == nil && fi.Mode().IsRegular() 30 } 31 32 // IsDirectory returns whether a file is a directory or not 33 func IsDirectory(filePath string) bool { 34 absolutePath, firstErr := filepath.Abs(filePath) 35 fi, secondErr := os.Stat(absolutePath) 36 return firstErr == nil && secondErr == nil && fi.Mode().IsDir() 37 } 38 39 // FileExists returns whether a file exists or not 40 func FileExists(filePath string) bool { 41 absolutePath, _ := filepath.Abs(filePath) 42 fi, secondErr := os.Stat(absolutePath) 43 return fi != nil && secondErr == nil 44 }