go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/systemd/machineinfo.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package systemd
     5  
     6  import (
     7  	"io"
     8  	"regexp"
     9  	"strings"
    10  )
    11  
    12  var MACHINE_INFO_REGEX = regexp.MustCompile(`(?m)^\s*(.+?)\s*=\s*['"]?(.*?)['"]?\s*$`)
    13  
    14  type MachineInfo struct {
    15  	PrettyHostname string
    16  	IconName       string
    17  	Chassis        string
    18  	Deployment     string
    19  }
    20  
    21  // https://www.freedesktop.org/software/systemd/man/machine-info.html
    22  // ParseMachineInfo parses the content of/etc /machine-info as specified for systemd
    23  func ParseMachineInfo(r io.Reader) (MachineInfo, error) {
    24  	res := MachineInfo{}
    25  
    26  	content, err := io.ReadAll(r)
    27  	if err != nil {
    28  		return res, err
    29  	}
    30  
    31  	m := MACHINE_INFO_REGEX.FindAllStringSubmatch(string(content), -1)
    32  	for _, value := range m {
    33  		switch strings.ToLower(value[1]) {
    34  		case "pretty_hostname":
    35  			res.PrettyHostname = value[2]
    36  		case "icon_name":
    37  			res.IconName = value[2]
    38  		case "chassis":
    39  			res.Chassis = value[2]
    40  		case "deployment":
    41  			res.Deployment = value[2]
    42  		}
    43  	}
    44  	return res, nil
    45  }