go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/detector/parser_macos_version.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package detector
     5  
     6  import (
     7  	"encoding/xml"
     8  	"regexp"
     9  )
    10  
    11  var DARWIN_RELEASE_REGEX = regexp.MustCompile(`(?m)^\s*(.+?)\s*:\s*(.+?)\s*$`)
    12  
    13  // ParseDarwinRelease will parse the output of `/usr/bin/sw_vers`
    14  func ParseDarwinRelease(content string) (map[string]string, error) {
    15  	return parseKeyValue(content, DARWIN_RELEASE_REGEX), nil
    16  }
    17  
    18  // parse macos system version property list
    19  type PropertyListDict struct {
    20  	Keys   []string `xml:"key"`
    21  	Values []string `xml:"string"`
    22  }
    23  
    24  type PropertyList struct {
    25  	XMLName xml.Name         `xml:"plist"`
    26  	Version string           `xml:"version,attr"`
    27  	Dict    PropertyListDict `xml:"dict"`
    28  }
    29  
    30  // parseMacOSSystemVersion will parse the content of
    31  // `/System/Library/CoreServices/SystemVersion.plist` and return the
    32  // result as structured values
    33  func ParseMacOSSystemVersion(content string) (map[string]string, error) {
    34  	v := PropertyList{}
    35  	err := xml.Unmarshal([]byte(content), &v)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	m := make(map[string]string)
    41  	for i := range v.Dict.Keys {
    42  		m[v.Dict.Keys[i]] = v.Dict.Values[i]
    43  	}
    44  	return m, nil
    45  }