os_release_darwin.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package resource // import "go.opentelemetry.io/otel/sdk/resource"
  15. import (
  16. "encoding/xml"
  17. "fmt"
  18. "io"
  19. "os"
  20. )
  21. type plist struct {
  22. XMLName xml.Name `xml:"plist"`
  23. Dict dict `xml:"dict"`
  24. }
  25. type dict struct {
  26. Key []string `xml:"key"`
  27. String []string `xml:"string"`
  28. }
  29. // osRelease builds a string describing the operating system release based on the
  30. // contents of the property list (.plist) system files. If no .plist files are found,
  31. // or if the required properties to build the release description string are missing,
  32. // an empty string is returned instead. The generated string resembles the output of
  33. // the `sw_vers` commandline program, but in a single-line string. For more information
  34. // about the `sw_vers` program, see: https://www.unix.com/man-page/osx/1/SW_VERS.
  35. func osRelease() string {
  36. file, err := getPlistFile()
  37. if err != nil {
  38. return ""
  39. }
  40. defer file.Close()
  41. values, err := parsePlistFile(file)
  42. if err != nil {
  43. return ""
  44. }
  45. return buildOSRelease(values)
  46. }
  47. // getPlistFile returns a *os.File pointing to one of the well-known .plist files
  48. // available on macOS. If no file can be opened, it returns an error.
  49. func getPlistFile() (*os.File, error) {
  50. return getFirstAvailableFile([]string{
  51. "/System/Library/CoreServices/SystemVersion.plist",
  52. "/System/Library/CoreServices/ServerVersion.plist",
  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. err := xml.NewDecoder(file).Decode(&v)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if len(v.Dict.Key) != len(v.Dict.String) {
  65. return nil, fmt.Errorf("the number of <key> and <string> elements doesn't match")
  66. }
  67. properties := make(map[string]string, len(v.Dict.Key))
  68. for i, key := range v.Dict.Key {
  69. properties[key] = v.Dict.String[i]
  70. }
  71. return properties, nil
  72. }
  73. // buildOSRelease builds a string describing the OS release based on the properties
  74. // available on the provided map. It tries to find the `ProductName`, `ProductVersion`
  75. // and `ProductBuildVersion` properties. If some of these properties are not found,
  76. // it returns an empty string.
  77. func buildOSRelease(properties map[string]string) string {
  78. productName := properties["ProductName"]
  79. productVersion := properties["ProductVersion"]
  80. productBuildVersion := properties["ProductBuildVersion"]
  81. if productName == "" || productVersion == "" || productBuildVersion == "" {
  82. return ""
  83. }
  84. return fmt.Sprintf("%s %s (%s)", productName, productVersion, productBuildVersion)
  85. }