common.go (706B)
1 package common // import "hhvn.uk/hbspbar/common" 2 3 import ( 4 "os" 5 "io" 6 "fmt" 7 "strconv" 8 "strings" 9 ) 10 11 func Perror(function string, err error) error { 12 return fmt.Errorf("%s(): %w", function, err) 13 } 14 15 func Error(format string, a ... any) (int, error) { 16 fmt.Fprintf(os.Stderr, "error: ") 17 return fmt.Fprintf(os.Stderr, format, a...) 18 } 19 20 func Intify(s string) (int, error) { 21 i, err := strconv.ParseInt(s, 0, 0) 22 return int(i), err 23 } 24 25 func FileAsLines(file string) ([]string, error) { 26 f, err := os.Open(file) 27 if err != nil { return nil, err } 28 29 content, err := io.ReadAll(f) 30 if err != nil { return nil, err } 31 32 lines := strings.Split(strings.TrimSuffix(string(content), "\n"), "\n") 33 34 return lines, nil 35 }