summaryrefslogtreecommitdiff
path: root/common/common.go
blob: 1156ee8cb7036acd2291dd81134e0f4d461b4f58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package common // import "hhvn.uk/hbspbar/common"

import (
	"os"
	"io"
	"fmt"
	"strconv"
	"strings"
)

func Perror(function string, err error) error {
	return fmt.Errorf("%s(): %w", function, err)
}

func Error(format string, a ... any) (int, error) {
	fmt.Fprintf(os.Stderr, "error: ")
	return fmt.Fprintf(os.Stderr, format, a...)
}

func Intify(s string) (int, error) {
	i, err := strconv.ParseInt(s, 0, 0)
	return int(i), err
}

func FileAsLines(file string) ([]string, error) {
	f, err := os.Open(file)
	if err != nil { return nil, err }

	content, err := io.ReadAll(f)
	if err != nil { return nil, err }

	lines := strings.Split(string(content), "\n")

	return lines, nil
}