github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/util/system/version_windows.go (about) 1 // +build windows 2 3 package system 4 5 import ( 6 "fmt" 7 // "fmt" 8 "golang.org/x/sys/windows/registry" 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 // we can not use GetVersion as that returns a version that the app sees (from its manifest) and not the current version 15 // and is missing some info. So use the registry e.g. 16 // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion -> 6.3 (used on older platforms) 17 // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName -> Windows 10 Pro 18 // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NcT\CurrentVersion\ReleaseId -> 1803 19 // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentBuild -> 17134 20 21 retVal := "unknown windows version" 22 23 regkey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) 24 if err != nil { 25 return retVal, err 26 } 27 defer regkey.Close() 28 29 pn, _, err := regkey.GetStringValue("ProductName") 30 if err != nil { 31 pn, err = getMajorMinorVersion(®key) 32 if err != nil { 33 return retVal, err 34 } 35 pn = fmt.Sprintf("Windows %s", pn) 36 } 37 38 rel, _, err := regkey.GetStringValue("ReleaseId") 39 if err != nil { 40 retVal = fmt.Sprintf("%s %s", pn, "unkown release") 41 } else { 42 retVal = fmt.Sprintf("%s %s", pn, rel) 43 } 44 45 build, _, err := regkey.GetStringValue("CurrentBuild") 46 if err != nil { 47 retVal = fmt.Sprintf("%s build %s", retVal, "unknown") 48 } else { 49 retVal = fmt.Sprintf("%s build %s", retVal, build) 50 } 51 return retVal, nil 52 } 53 54 func getMajorMinorVersion(regkey *registry.Key) (string, error) { 55 major, _, err := regkey.GetIntegerValue("CurrentMajorVersionNumber") 56 if err != nil { 57 // try currentVersion which will only be up to 8.1 58 ver, _, err := regkey.GetStringValue("CurrentVersion") 59 return ver, err 60 } 61 minor, _, err := regkey.GetIntegerValue("CurrentMinorVersionNumber") 62 if err != nil { 63 return fmt.Sprintf("unknwown windows (%d.unknown)", major), nil 64 } 65 return fmt.Sprintf("unknwown windows (%d.%d)", major, minor), nil 66 }