Source file src/cmd/internal/osinfo/os_sysctl.go

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build darwin || dragonfly || freebsd || netbsd || openbsd
     6  
     7  package osinfo
     8  
     9  import (
    10  	"strings"
    11  	"syscall"
    12  )
    13  
    14  // Version returns the OS version name/number.
    15  func Version() (string, error) {
    16  	sysname, err := syscall.Sysctl("kern.ostype")
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  	release, err := syscall.Sysctl("kern.osrelease")
    21  	if err != nil {
    22  		return "", err
    23  	}
    24  	version, err := syscall.Sysctl("kern.version")
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  
    29  	// The version might have newlines or tabs; convert to spaces.
    30  	version = strings.ReplaceAll(version, "\n", " ")
    31  	version = strings.ReplaceAll(version, "\t", " ")
    32  	version = strings.TrimSpace(version)
    33  
    34  	machine, err := syscall.Sysctl("hw.machine")
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  
    39  	ret := sysname + " " + release + " " + version + " " + machine
    40  	return ret, nil
    41  }
    42  

View as plain text