@@ -21,7 +21,6 @@ import (
21
21
"bufio"
22
22
"bytes"
23
23
"fmt"
24
- "io/ioutil"
25
24
"os"
26
25
"path/filepath"
27
26
"regexp"
@@ -50,12 +49,13 @@ var (
50
49
51
50
// familyMap contains a mapping of family -> []platforms.
52
51
var familyMap = map [string ][]string {
52
+ "arch" : {"arch" , "antergos" , "manjaro" },
53
53
"redhat" : {
54
54
"redhat" , "fedora" , "centos" , "scientific" , "oraclelinux" , "ol" ,
55
55
"amzn" , "rhel" , "almalinux" , "openeuler" , "rocky" ,
56
56
},
57
57
"debian" : {"debian" , "ubuntu" , "raspbian" , "linuxmint" },
58
- "suse" : {"suse" , "sles" , "opensuse" , "opensuse-leap" , "opensuse-tumbleweed" },
58
+ "suse" : {"suse" , "sles" , "opensuse" },
59
59
}
60
60
61
61
var platformToFamilyMap map [string ]string
@@ -99,9 +99,9 @@ func getOSInfo(baseDir string) (*types.OSInfo, error) {
99
99
}
100
100
101
101
func getOSRelease (baseDir string ) (* types.OSInfo , error ) {
102
- lsbRel , _ := ioutil .ReadFile (filepath .Join (baseDir , lsbRelease ))
102
+ lsbRel , _ := os .ReadFile (filepath .Join (baseDir , lsbRelease ))
103
103
104
- osRel , err := ioutil .ReadFile (filepath .Join (baseDir , osRelease ))
104
+ osRel , err := os .ReadFile (filepath .Join (baseDir , osRelease ))
105
105
if err != nil {
106
106
return nil , err
107
107
}
@@ -150,21 +150,15 @@ func parseOSRelease(content []byte) (*types.OSInfo, error) {
150
150
func makeOSInfo (osRelease map [string ]string ) (* types.OSInfo , error ) {
151
151
os := & types.OSInfo {
152
152
Type : "linux" ,
153
- Platform : osRelease [ "ID" ] ,
154
- Name : osRelease [ "NAME" ] ,
155
- Version : osRelease [ "VERSION" ] ,
153
+ Platform : firstOf ( osRelease , "ID" , "DISTRIB_ID" ) ,
154
+ Name : firstOf ( osRelease , "NAME" , "PRETTY_NAME" ) ,
155
+ Version : firstOf ( osRelease , "VERSION" , "VERSION_ID" , "DISTRIB_RELEASE" ) ,
156
156
Build : osRelease ["BUILD_ID" ],
157
- Codename : osRelease ["VERSION_CODENAME" ],
158
- }
159
-
160
- if os .Version == "" {
161
- // Fallback to VERSION_ID if VERSION is empty.
162
- os .Version = osRelease ["VERSION_ID" ]
157
+ Codename : firstOf (osRelease , "VERSION_CODENAME" , "DISTRIB_CODENAME" ),
163
158
}
164
159
165
160
if os .Codename == "" {
166
- // Some OSes uses their own CODENAME keys (e.g UBUNTU_CODENAME) or we
167
- // can get the DISTRIB_CODENAME value from the lsb-release data.
161
+ // Some OSes use their own CODENAME keys (e.g UBUNTU_CODENAME).
168
162
for k , v := range osRelease {
169
163
if strings .Contains (k , "CODENAME" ) {
170
164
os .Codename = v
@@ -174,10 +168,19 @@ func makeOSInfo(osRelease map[string]string) (*types.OSInfo, error) {
174
168
}
175
169
176
170
if os .Platform == "" {
177
- // Fallback to the first word of the NAME field.
178
- parts := strings .SplitN (os .Name , " " , 2 )
179
- if len (parts ) > 0 {
180
- os .Platform = strings .ToLower (parts [0 ])
171
+ // Fallback to the first word of the Name field.
172
+ os .Platform , _ , _ = strings .Cut (os .Name , " " )
173
+ }
174
+
175
+ os .Family = linuxFamily (os .Platform )
176
+ if os .Family == "" {
177
+ // ID_LIKE is a space-separated list of OS identifiers that this
178
+ // OS is similar to. Use this to figure out the Linux family.
179
+ for _ , id := range strings .Fields (osRelease ["ID_LIKE" ]) {
180
+ os .Family = linuxFamily (id )
181
+ if os .Family != "" {
182
+ break
183
+ }
181
184
}
182
185
}
183
186
@@ -200,7 +203,6 @@ func makeOSInfo(osRelease map[string]string) (*types.OSInfo, error) {
200
203
}
201
204
}
202
205
203
- os .Family = platformToFamilyMap [strings .ToLower (os .Platform )]
204
206
return os , nil
205
207
}
206
208
@@ -231,7 +233,7 @@ func findDistribRelease(baseDir string) (*types.OSInfo, error) {
231
233
}
232
234
233
235
func getDistribRelease (file string ) (* types.OSInfo , error ) {
234
- data , err := ioutil .ReadFile (file )
236
+ data , err := os .ReadFile (file )
235
237
if err != nil {
236
238
return nil , err
237
239
}
@@ -277,6 +279,40 @@ func parseDistribRelease(platform string, content []byte) (*types.OSInfo, error)
277
279
}
278
280
}
279
281
280
- os .Family = platformToFamilyMap [ strings . ToLower (os .Platform )]
282
+ os .Family = linuxFamily (os .Platform )
281
283
return os , nil
282
284
}
285
+
286
+ // firstOf returns the first non-empty value found in the map while
287
+ // iterating over keys.
288
+ func firstOf (kv map [string ]string , keys ... string ) string {
289
+ for _ , key := range keys {
290
+ if v := kv [key ]; v != "" {
291
+ return v
292
+ }
293
+ }
294
+ return ""
295
+ }
296
+
297
+ // linuxFamily returns the linux distribution family associated to the OS platform.
298
+ // If there is no family associated then it returns an empty string.
299
+ func linuxFamily (platform string ) string {
300
+ if platform == "" {
301
+ return ""
302
+ }
303
+
304
+ platform = strings .ToLower (platform )
305
+
306
+ // First try a direct lookup.
307
+ if family , found := platformToFamilyMap [platform ]; found {
308
+ return family
309
+ }
310
+
311
+ // Try prefix matching (e.g. opensuse matches opensuse-tumpleweed).
312
+ for platformPrefix , family := range platformToFamilyMap {
313
+ if strings .HasPrefix (platform , platformPrefix ) {
314
+ return family
315
+ }
316
+ }
317
+ return ""
318
+ }
0 commit comments