rpaste/modules/utility/file.go

49 lines
916 B
Go
Raw Normal View History

2021-12-21 01:06:08 +00:00
// file utility area
//
// This part of the utility package is primarily for file or path checking.
package utility
import (
2021-12-24 09:01:22 +00:00
_ "errors"
2021-12-28 04:27:10 +00:00
"io/ioutil"
2021-12-24 09:01:22 +00:00
"os"
_ "path"
_ "path/filepath"
2021-12-28 04:27:10 +00:00
"strings"
2021-12-21 01:06:08 +00:00
)
// Check if this is a file
func IsFile(filePath string) (bool, error) {
2021-12-24 09:01:22 +00:00
f, err := os.Stat(filePath)
if err == nil {
return !f.IsDir(), nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
2021-12-21 01:06:08 +00:00
}
// Check if this even exists
func IsExist(path string) (bool, error) {
2021-12-24 09:01:22 +00:00
_, err := os.Stat(path)
if err == nil || os.IsExist(err) {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
2021-12-21 01:06:08 +00:00
}
2021-12-28 04:27:10 +00:00
// 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))
}
2021-12-21 01:06:08 +00:00
// todo: add string checking, we can't paste binary