-
Notifications
You must be signed in to change notification settings - Fork 5
/
os_unix.go
143 lines (132 loc) · 3.33 KB
/
os_unix.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2016 Joel Scoble and The JoeFriday authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package os provides OS Release information, /etc/os-release.
package os
import (
"io"
"sync"
joe "github.com/mohae/joefriday"
)
const etcFile = "/etc/os-release"
// OS holds information about the OS release.
type OS struct {
Name string `json:"name"`
ID string `json:"id"`
IDLike string `json:"id_like"`
PrettyName string `json:"pretty_name"`
Version string `json:"version"`
VersionID string `json:"version_id"`
HomeURL string `json:"home_url"`
BugReportURL string `json:"bug_report_url"`
}
// Profiler processes the OS release information, /etc/os-release.
type Profiler struct {
joe.Procer
*joe.Buffer
}
// Returns an initialized Profiler; ready to use.
func NewProfiler() (prof *Profiler, err error) {
proc, err := joe.NewProc(etcFile)
if err != nil {
return nil, err
}
return &Profiler{Procer: proc, Buffer: joe.NewBuffer()}, nil
}
// Reset resources: after reset, the profiler is ready to be used again.
func (prof *Profiler) Reset() error {
prof.Buffer.Reset()
return prof.Procer.Reset()
}
// Get gets the OS release information, the /etc/os-release.
func (prof *Profiler) Get() (os *OS, err error) {
var (
i, keyLen int
v byte
)
os = &OS{}
err = prof.Reset()
if err != nil {
return nil, err
}
for {
prof.Line, err = prof.ReadSlice('\n')
if err != nil {
if err == io.EOF {
break
}
return nil, &joe.ReadError{Err: err}
}
// The key is everything up to '='; 0x3D
for i, v = range prof.Line {
if v == 0x3D {
prof.Val = prof.Line[:i]
keyLen = len(prof.Val)
// see if the value has quotes; if it does, elide them
if prof.Line[i+1] == 0x22 {
prof.Val = append(prof.Val, prof.Line[i+2:len(prof.Line)-2]...)
} else {
prof.Val = append(prof.Val, prof.Line[i+1:len(prof.Line)-1]...)
}
break
}
}
v = prof.Val[0]
if v == 'I' {
if prof.Val[2] == '_' {
os.IDLike = string(prof.Val[keyLen:])
continue
}
os.ID = string(prof.Val[keyLen:])
continue
}
if v == 'V' {
if prof.Val[7] == '_' {
os.VersionID = string(prof.Val[keyLen:])
continue
}
os.Version = string(prof.Val[keyLen:])
continue
}
if v == 'N' {
os.Name = string(prof.Val[keyLen:])
continue
}
if v == 'P' {
os.PrettyName = string(prof.Val[keyLen:])
continue
}
if v == 'H' {
os.HomeURL = string(prof.Val[keyLen:])
continue
}
if v == 'B' {
os.BugReportURL = string(prof.Val[keyLen:])
continue
}
}
return os, nil
}
var std *Profiler
var stdMu sync.Mutex
// Get gets the OS release information using the package's global Profiler.
func Get() (os *OS, err error) {
stdMu.Lock()
defer stdMu.Unlock()
if std == nil {
std, err = NewProfiler()
if err != nil {
return nil, err
}
}
return std.Get()
}