rpaste/modules/utility/file.go
2021-12-27 21:27:10 -07:00

49 lines
916 B
Go

// file utility area
//
// This part of the utility package is primarily for file or path checking.
package utility
import (
_ "errors"
"io/ioutil"
"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 := ioutil.ReadFile(path)
if err != nil {
return ""
}
return strings.TrimSpace(string(data))
}
// todo: add string checking, we can't paste binary