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)
| 36 | // could not be successfully converted to an integer. In any error, a warning |
| 37 | // message is printed to STDERR and -1 is returned. |
| 38 | func 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, |
no test coverage detected
searching dependent graphs…