github.com/iDigitalFlame/xmt@v0.5.4/device/local/macos_no_crypt.go (about)

     1  //go:build (darwin || ios) && !crypt
     2  // +build darwin ios
     3  // +build !crypt
     4  
     5  // Copyright (C) 2020 - 2023 iDigitalFlame
     6  //
     7  // This program is free software: you can redistribute it and/or modify
     8  // it under the terms of the GNU General Public License as published by
     9  // the Free Software Foundation, either version 3 of the License, or
    10  // any later version.
    11  //
    12  // This program is distributed in the hope that it will be useful,
    13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  // GNU General Public License for more details.
    16  //
    17  // You should have received a copy of the GNU General Public License
    18  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    19  //
    20  
    21  package local
    22  
    23  import (
    24  	"os/exec"
    25  	"strings"
    26  
    27  	"github.com/iDigitalFlame/xmt/device/unix"
    28  )
    29  
    30  func sysID() []byte {
    31  	o, err := exec.Command("/usr/sbin/ioreg", "-rd1", "-c", "IOPlatformExpertDevice").CombinedOutput()
    32  	if err != nil || len(o) == 0 {
    33  		return nil
    34  	}
    35  	for _, v := range strings.Split(string(o), "\n") {
    36  		if !strings.Contains(v, `"IOPlatformUUID"`) {
    37  			continue
    38  		}
    39  		x := strings.IndexByte(v, '=')
    40  		if x < 14 || len(v)-x < 2 {
    41  			continue
    42  		}
    43  		c, s := len(v)-1, x+1
    44  		for ; c > x && (v[c] == '"' || v[c] == ' ' || v[c] == 9); c-- {
    45  		}
    46  		for ; s < c && (v[s] == '"' || v[s] == ' ' || v[s] == 9); s++ {
    47  		}
    48  		if s == c || s > len(v) || s > c {
    49  			continue
    50  		}
    51  		return []byte(v[s : c+1])
    52  	}
    53  	return nil
    54  }
    55  func version() string {
    56  	var b, n, v string
    57  	if o, err := exec.Command("/usr/bin/sw_vers").CombinedOutput(); err == nil {
    58  		m := make(map[string]string)
    59  		for _, v := range strings.Split(string(o), "\n") {
    60  			x := strings.IndexByte(v, ':')
    61  			if x < 1 || len(v)-x < 2 {
    62  				continue
    63  			}
    64  			c, s := len(v)-1, x+1
    65  			for ; c > x && (v[c] == ' ' || v[c] == 9); c-- {
    66  			}
    67  			for ; s < c && (v[s] == ' ' || v[s] == 9); s++ {
    68  			}
    69  			m[strings.ToUpper(v[:x])] = v[s : c+1]
    70  		}
    71  		n = m["PRODUCTNAME"]
    72  		b = m["BUILDVERSION"]
    73  		v = m["PRODUCTVERSION"]
    74  	}
    75  	if len(v) == 0 {
    76  		v = unix.Release()
    77  	}
    78  	switch {
    79  	case len(n) == 0 && len(b) == 0 && len(v) == 0:
    80  		return "MacOS"
    81  	case len(n) == 0 && len(b) > 0 && len(v) > 0:
    82  		return "MacOS (" + v + ", " + b + ")"
    83  	case len(n) == 0 && len(b) == 0 && len(v) > 0:
    84  		return "MacOS (" + v + ")"
    85  	case len(n) == 0 && len(b) > 0 && len(v) == 0:
    86  		return "MacOS (" + b + ")"
    87  	case len(n) > 0 && len(b) > 0 && len(v) > 0:
    88  		return n + " (" + v + ", " + b + ")"
    89  	case len(n) > 0 && len(b) == 0 && len(v) > 0:
    90  		return n + " (" + v + ")"
    91  	case len(n) > 0 && len(b) > 0 && len(v) == 0:
    92  		return n + " (" + b + ")"
    93  	}
    94  	return "MacOS"
    95  }