/
data_linux.go
106 lines (98 loc) · 2.35 KB
/
data_linux.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package chezmoi
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"unicode"
"github.com/twpayne/go-vfs"
)
// KernelInfo returns the kernel information parsed from /proc/sys/kernel.
func KernelInfo(fs vfs.FS) (map[string]string, error) {
const procSysKernel = "/proc/sys/kernel"
info, err := fs.Stat(procSysKernel)
switch {
case os.IsNotExist(err):
return nil, nil
case os.IsPermission(err):
return nil, nil
case err != nil:
return nil, err
case !info.Mode().IsDir():
return nil, nil
}
kernelInfo := make(map[string]string)
for _, filename := range []string{
"osrelease",
"ostype",
"version",
} {
data, err := fs.ReadFile(filepath.Join(procSysKernel, filename))
switch {
case os.IsNotExist(err):
continue
case os.IsPermission(err):
continue
case err != nil:
return nil, err
}
kernelInfo[filename] = string(bytes.TrimSpace(data))
}
return kernelInfo, nil
}
// OSRelease returns the operating system identification data as defined by the
// os-release specification.
func OSRelease(fs vfs.FS) (map[string]string, error) {
for _, filename := range []string{
"/usr/lib/os-release",
"/etc/os-release",
} {
data, err := fs.ReadFile(filename)
if os.IsNotExist(err) {
continue
} else if err != nil {
return nil, err
}
m, err := parseOSRelease(bytes.NewBuffer(data))
if err != nil {
return nil, err
}
return m, nil
}
return nil, os.ErrNotExist
}
// maybeUnquote removes quotation marks around s.
func maybeUnquote(s string) string {
// Try to unquote.
if s, err := strconv.Unquote(s); err == nil {
return s
}
// Otherwise return s, unchanged.
return s
}
// parseOSRelease parses operating system identification data from r as defined
// by the os-release specification.
func parseOSRelease(r io.Reader) (map[string]string, error) {
result := make(map[string]string)
s := bufio.NewScanner(r)
for s.Scan() {
// Trim all leading whitespace, but not necessarily trailing whitespace.
token := strings.TrimLeftFunc(s.Text(), unicode.IsSpace)
// If the line is empty or starts with #, skip.
if len(token) == 0 || token[0] == '#' {
continue
}
fields := strings.SplitN(token, "=", 2)
if len(fields) != 2 {
return nil, fmt.Errorf("%s: parse error", token)
}
key := fields[0]
value := maybeUnquote(fields[1])
result[key] = value
}
return result, s.Err()
}