rpaste/modules/utility/file.go
2024-06-09 20:27:38 -07:00

47 lines
899 B
Go

// file utility area
//
// This part of the utility package is primarily for file or path checking.
package utility
import (
_ "errors"
"os"
_ "path"
_ "path/filepath"
"strings"
)
// Check if this is a file
func IsFile(filePath string) (bool, error) {
f, err := os.Stat(filePath)
if err == nil {
return !f.IsDir(), nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Check if this even exists
func IsExist(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil || os.IsExist(err) {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Reads one-line text files (like for /sys or /proc) - Strips carriage returns
func Slurp(path string) string {
data, err := os.ReadFile(path)
if err != nil {
return ""
}
return strings.TrimSpace(string(data))
}
// todo: add string checking, we can't paste binary