-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_text.go
46 lines (36 loc) · 839 Bytes
/
get_text.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
package main
import (
"io/ioutil"
"os"
"runtime"
"strings"
)
/* Get the name of OS/Distro or piped input */
func getDisplayText() (displayText string) {
/* Check for piped input */
fi, _ := os.Stdin.Stat()
if (fi.Mode() & os.ModeCharDevice) == 0 {
bytes, _ := ioutil.ReadAll(os.Stdin)
return string(bytes)
}
/* In case of no piped input, get OS name */
f, err := ioutil.ReadFile("/etc/os-release")
if err != nil {
return strings.ToTitle(runtime.GOOS)
}
osRelease := make(map[string]string)
for _, l := range strings.Split(string(f), "\n") {
kv := strings.SplitN(l, "=", 2)
if len(kv) != 2 {
continue
}
osRelease[kv[0]] = strings.Trim(kv[1], `"`)
}
if v, ok := osRelease["NAME"]; ok {
return v
}
if v, ok := osRelease["PRETTY_NAME"]; ok {
return v
}
return strings.ToTitle(runtime.GOOS)
}