github.com/wtfutil/wtf@v0.43.0/modules/system/system_info.go (about)

     1  //go:build !windows
     2  
     3  package system
     4  
     5  import (
     6  	"os/exec"
     7  	"runtime"
     8  	"strings"
     9  
    10  	"github.com/wtfutil/wtf/utils"
    11  )
    12  
    13  type SystemInfo struct {
    14  	ProductName    string
    15  	ProductVersion string
    16  	BuildVersion   string
    17  }
    18  
    19  func NewSystemInfo() *SystemInfo {
    20  	m := make(map[string]string)
    21  
    22  	arg := []string{}
    23  
    24  	var cmd *exec.Cmd
    25  	switch runtime.GOOS {
    26  	case "linux":
    27  		arg = append(arg, "-a")
    28  		cmd = exec.Command("lsb_release", arg...)
    29  	case "darwin":
    30  		cmd = exec.Command("sw_vers", arg...)
    31  	default:
    32  		cmd = exec.Command("sw_vers", arg...)
    33  	}
    34  
    35  	raw := utils.ExecuteCommand(cmd)
    36  
    37  	for _, row := range strings.Split(raw, "\n") {
    38  		parts := strings.Split(row, ":")
    39  		if len(parts) < 2 {
    40  			continue
    41  		}
    42  
    43  		m[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
    44  	}
    45  
    46  	var sysInfo *SystemInfo
    47  	switch runtime.GOOS {
    48  	case "linux":
    49  		sysInfo = &SystemInfo{
    50  			ProductName:    m["Distributor ID"],
    51  			ProductVersion: m["Description"],
    52  			BuildVersion:   m["Release"],
    53  		}
    54  	default:
    55  		sysInfo = &SystemInfo{
    56  			ProductName:    m["ProductName"],
    57  			ProductVersion: m["ProductVersion"],
    58  			BuildVersion:   m["BuildVersion"],
    59  		}
    60  
    61  	}
    62  	return sysInfo
    63  }