MCPcopy
hub / github.com/jaypipes/ghw / SafeIntFromFile

Function SafeIntFromFile

pkg/util/util.go:38–52  ·  view source on GitHub ↗

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.

(ctx context.Context, path string)

Source from the content-addressed store, hash-verified

36// could not be successfully converted to an integer. In any error, a warning
37// message is printed to STDERR and -1 is returned.
38func SafeIntFromFile(ctx context.Context, path string) int {
39 msg := "failed to read int from file: %s\n"
40 buf, err := os.ReadFile(path)
41 if err != nil {
42 log.Warn(ctx, msg, err)
43 return -1
44 }
45 contents := strings.TrimSpace(string(buf))
46 res, err := strconv.Atoi(contents)
47 if err != nil {
48 log.Warn(ctx, msg, err)
49 return -1
50 }
51 return res
52}
53
54// ConcatStrings concatenate strings in a larger one. This function
55// addresses a very specific ghw use case. For a more general approach,

Callers 7

gpuFillNUMANodesFunction · 0.92
getDeviceNUMANodeFunction · 0.92
processorsGetFunction · 0.92
CoresForNodeFunction · 0.92
diskIsRotationalFunction · 0.92

Calls 1

WarnFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…