forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
58 lines (51 loc) · 1.21 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
51
52
53
54
55
56
57
58
//
// 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"
)
const (
disableWarningsEnv = "GHW_DISABLE_WARNINGS"
)
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{}) {
if _, ok := os.LookupEnv(disableWarningsEnv); ok {
return
}
_, _ = 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 warning
// message is printed to STDERR and -1 is returned.
func safeIntFromFile(path string) int {
msg := "failed to read int from file: %s\n"
buf, err := ioutil.ReadFile(path)
if err != nil {
warn(msg, err)
return -1
}
contents := strings.TrimSpace(string(buf))
res, err := strconv.Atoi(contents)
if err != nil {
warn(msg, err)
return -1
}
return res
}