github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/util/system/version_darwin.go (about)

     1  // +build darwin
     2  
     3  package system
     4  
     5  import (
     6  	"fmt"
     7  	"os/exec"
     8  	"strings"
     9  )
    10  
    11  // GetOsVersion returns a human friendly string of the current OS
    12  // in the case of an error this still returns a valid string for the details that can be found.
    13  func GetOsVersion() (string, error) {
    14  	retVal := "unknown OSX version"
    15  
    16  	// you can not run the command once to get all this :-(
    17  	pn, err := runCommand("sw_vers", "-productName")
    18  	if err != nil {
    19  		return retVal, err
    20  	}
    21  	pv, err := runCommand("sw_vers", "-productVersion")
    22  	if err != nil {
    23  		retVal = fmt.Sprintf("%s unknown version", pn)
    24  	} else {
    25  		retVal = fmt.Sprintf("%s %s", pn, pv)
    26  	}
    27  
    28  	pb, err := runCommand("sw_vers", "-buildVersion")
    29  	if err != nil {
    30  		return fmt.Sprintf("%s unknown build", retVal), err
    31  	}
    32  	return fmt.Sprintf("%s build %s", retVal, pb), nil
    33  }
    34  
    35  func runCommand(command string, args ...string) (string, error) {
    36  	e := exec.Command(command, args...)
    37  	data, err := e.CombinedOutput()
    38  	text := string(data)
    39  	text = strings.TrimSpace(text)
    40  	if err != nil {
    41  		return "", fmt.Errorf("command failed '%s %s': %s %s\n", command, strings.Join(args, " "), text, err)
    42  	}
    43  	return text, err
    44  }