File: util.go

package info (click to toggle)
golang-github-astroprofundis-sysinfo 0.0~git20240112.ed54df1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212 kB
  • sloc: asm: 21; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 963 bytes parent folder | download
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
// Copyright © 2016 Zlatko Čalušić
//
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.

package sysinfo

import (
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

// Read one-liner text files, strip newline.
func slurpFile(path string) string {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return ""
	}

	return strings.TrimSpace(string(data))
}

// Write one-liner text files, add newline, ignore errors (best effort).
func spewFile(path string, data string, perm os.FileMode) {
	_ = ioutil.WriteFile(path, []byte(data+"\n"), perm)
}

func SlurpFile(path string) string {
	return slurpFile(path)
}

func parseMemSize(key, memInfo string) uint64 {
	for _, line := range strings.Split(memInfo, "\n") {
		if !strings.Contains(line, key) {
			continue
		}
		fields := strings.Fields(line)
		size, err := strconv.ParseUint(fields[1], 10, 64)
		if err != nil {
			return 0
		}
		return size
	}
	return 0
}