-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metadata.go
89 lines (78 loc) · 1.56 KB
/
Metadata.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
package main
import (
// "fmt"
"os"
"io/ioutil"
"encoding/json"
"syscall"
)
var distFiles = []string {
"/etc/redhat-release",
"/etc/lsb-release",
"/etc/fedora-release",
"/etc/slackware-release",
"/etc/debian_release",
"/etc/debian_version",
"/etc/mandrake-release",
"/etc/yellowdog-release",
"/etc/sun-release",
"/etc/release",
"/etc/gentoo-release",
"/etc/UnitedLinux-release",
"/etc/SUSE-release",
"/etc/SuSE-release",
}
type metadata map[string]string
func (m metadata) gethostname() (error) {
hostn, err := os.Hostname()
if err != nil {
return err
}
m["hostname"] = hostn
return nil
}
func (m metadata) getosrelease() (error) {
for _, distf := range distFiles {
release, err := ioutil.ReadFile(distf)
if err == nil {
if release[len(release)-1] == 10 {
m["osrelease"] = string(release[0:len(release)-1])
} else {
m["osrelease"] = string(release)
}
return nil
}
}
m["osrelease"] = "Unknown"
return nil
}
func (m metadata) getplatform() (error) {
uts := new(syscall.Utsname)
err := syscall.Uname(uts)
if err != nil {
return err
}
s := make([]byte, len(uts.Machine))
i := 0
for ; i < len(uts.Machine); i++ {
if uts.Machine[i] == 0 {
break
}
s[i] = uint8(uts.Machine[i])
}
m["platform"] = string(s[0:i])
return nil
}
func metadataContructor() (metadata) {
md := make(metadata)
md.gethostname()
md.getosrelease()
md.getplatform()
return md
}
func main() {
metad := metadataContructor()
// metad['timestamp'] = yadda-yadda-yadda
output, _ := json.MarshalIndent(metad, "", " ")
os.Stdout.Write(output)
}