| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package resource // import "go.opentelemetry.io/otel/sdk/resource" |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/xml" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"os" |
| 12 |
) |
| 13 |
|
| 14 |
type plist struct { |
| 15 |
XMLName xml.Name `xml:"plist"` |
| 16 |
Dict dict `xml:"dict"` |
| 17 |
} |
| 18 |
|
| 19 |
type dict struct { |
| 20 |
Key []string `xml:"key"` |
| 21 |
String []string `xml:"string"` |
| 22 |
} |
| 23 |
|
| 24 |
// osRelease builds a string describing the operating system release based on the |
| 25 |
// contents of the property list (.plist) system files. If no .plist files are found, |
| 26 |
// or if the required properties to build the release description string are missing, |
| 27 |
// an empty string is returned instead. The generated string resembles the output of |
| 28 |
// the `sw_vers` commandline program, but in a single-line string. For more information |
| 29 |
// about the `sw_vers` program, see: https://www.unix.com/man-page/osx/1/SW_VERS. |
| 30 |
func osRelease() string { |
| 31 |
file, err := getPlistFile() |
| 32 |
if err != nil { |
| 33 |
return "" |
| 34 |
} |
| 35 |
|
| 36 |
defer file.Close() |
| 37 |
|
| 38 |
values, err := parsePlistFile(file) |
| 39 |
if err != nil { |
| 40 |
return "" |
| 41 |
} |
| 42 |
|
| 43 |
return buildOSRelease(values) |
| 44 |
} |
| 45 |
|
| 46 |
// getPlistFile returns a *os.File pointing to one of the well-known .plist files |
| 47 |
// available on macOS. If no file can be opened, it returns an error. |
| 48 |
func getPlistFile() (*os.File, error) { |
| 49 |
return getFirstAvailableFile([]string{ |
| 50 |
"/System/Library/CoreServices/SystemVersion.plist", |
| 51 |
"/System/Library/CoreServices/ServerVersion.plist", |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
// parsePlistFile process the file pointed by `file` as a .plist file and returns |
| 56 |
// a map with the key-values for each pair of correlated <key> and <string> elements |
| 57 |
// contained in it. |
| 58 |
func parsePlistFile(file io.Reader) (map[string]string, error) { |
| 59 |
var v plist |
| 60 |
|
| 61 |
err := xml.NewDecoder(file).Decode(&v) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
|
| 66 |
if len(v.Dict.Key) != len(v.Dict.String) { |
| 67 |
return nil, errors.New("the number of <key> and <string> elements doesn't match") |
| 68 |
} |
| 69 |
|
| 70 |
properties := make(map[string]string, len(v.Dict.Key)) |
| 71 |
for i, key := range v.Dict.Key { |
| 72 |
properties[key] = v.Dict.String[i] |
| 73 |
} |
| 74 |
|
| 75 |
return properties, nil |
| 76 |
} |
| 77 |
|
| 78 |
// buildOSRelease builds a string describing the OS release based on the properties |
| 79 |
// available on the provided map. It tries to find the `ProductName`, `ProductVersion` |
| 80 |
// and `ProductBuildVersion` properties. If some of these properties are not found, |
| 81 |
// it returns an empty string. |
| 82 |
func buildOSRelease(properties map[string]string) string { |
| 83 |
productName := properties["ProductName"] |
| 84 |
productVersion := properties["ProductVersion"] |
| 85 |
productBuildVersion := properties["ProductBuildVersion"] |
| 86 |
|
| 87 |
if productName == "" || productVersion == "" || productBuildVersion == "" { |
| 88 |
return "" |
| 89 |
} |
| 90 |
|
| 91 |
return fmt.Sprintf("%s %s (%s)", productName, productVersion, productBuildVersion) |
| 92 |
} |