github.com/haraldrudell/parl@v0.4.176/punix/os-version-linux.go (about) 1 //go:build linux 2 3 /* 4 © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 5 ISC License 6 */ 7 8 package punix 9 10 import ( 11 "github.com/haraldrudell/parl/perrors" 12 "golang.org/x/exp/slices" 13 "golang.org/x/sys/unix" 14 ) 15 16 // OsVersion for linux returns version: "Linux 5.15.0-56-generic" 17 func OsVersion() (version string, hasVersion bool, err error) { 18 19 var uname unix.Utsname // Unix Time-Sharing System Name 20 if err = unix.Uname(&uname); perrors.Is(&err, "unix.Uname %w", err) { 21 return 22 } 23 hasVersion = true 24 version = sliceToString(uname.Sysname[:]) + "\x20" + sliceToString(uname.Release[:]) 25 26 // uname: map[ 27 // - Sysname:"Linux" 28 // - Nodename:"c34z" 29 // - Release:"5.15.0-56-generic" 30 // - Version:"#62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022" 31 // - Machine:"x86_64" 32 // - Domainname:"(none)" 33 // - ] 34 // m := map[string]string{ 35 // "Sysname": strconv.Quote(sliceToString(uname.Sysname[:])), 36 // "Nodename": strconv.Quote(sliceToString(uname.Nodename[:])), 37 // "Release": strconv.Quote(sliceToString(uname.Release[:])), 38 // "Version": strconv.Quote(sliceToString(uname.Version[:])), 39 // "Machine": strconv.Quote(sliceToString(uname.Machine[:])), 40 // "Domainname": strconv.Quote(sliceToString(uname.Domainname[:])), 41 // } 42 // parl.Out("uname: %v", m) 43 44 return 45 } 46 47 // sliceToString returns a string from a byte array that may contain zero-byte terminator 48 func sliceToString(b []byte) (s string) { 49 if index := slices.Index(b, 0); index != -1 { 50 b = b[:index] 51 } 52 s = string(b) 53 return 54 }