forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
50 lines (44 loc) · 1.07 KB
/
utils.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
type closer interface {
Close() error
}
func safeClose(c closer) {
err := c.Close()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to close: %s", err)
}
}
func warn(msg string, args ...interface{}) {
_, _ = fmt.Fprint(os.Stderr, "WARNING: ")
_, _ = fmt.Fprintf(os.Stderr, msg, args...)
}
// Reads a supplied filepath and converts the contents to an integer. Returns
// -1 if there were file permissions or existence errors or if the contents
// could not be successfully converted to an integer. In any error, a message
// is printed to STDERR, but -1 is returned.
func safeIntFromFile(path string) int {
buf, err := ioutil.ReadFile(path)
if err != nil {
fmt.Fprint(os.Stderr, err)
return -1
}
contents := strings.TrimSpace(string(buf))
res, err := strconv.Atoi(contents)
if err != nil {
fmt.Fprint(os.Stderr, err)
return -1
}
return res
}