version.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // +build windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package winstats
  15. import (
  16. "fmt"
  17. "golang.org/x/sys/windows/registry"
  18. )
  19. //OSInfo is a convenience class for retrieving Windows OS information
  20. type OSInfo struct {
  21. BuildNumber, ProductName string
  22. MajorVersion, MinorVersion, UBR uint64
  23. }
  24. // GetOSInfo reads Windows version information from the registry
  25. func GetOSInfo() (*OSInfo, error) {
  26. k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
  27. if err != nil {
  28. return nil, err
  29. }
  30. defer k.Close()
  31. buildNumber, _, err := k.GetStringValue("CurrentBuildNumber")
  32. if err != nil {
  33. return nil, err
  34. }
  35. majorVersionNumber, _, err := k.GetIntegerValue("CurrentMajorVersionNumber")
  36. if err != nil {
  37. return nil, err
  38. }
  39. minorVersionNumber, _, err := k.GetIntegerValue("CurrentMinorVersionNumber")
  40. if err != nil {
  41. return nil, err
  42. }
  43. revision, _, err := k.GetIntegerValue("UBR")
  44. if err != nil {
  45. return nil, err
  46. }
  47. productName, _, err := k.GetStringValue("ProductName")
  48. if err != nil {
  49. return nil, nil
  50. }
  51. return &OSInfo{
  52. BuildNumber: buildNumber,
  53. ProductName: productName,
  54. MajorVersion: majorVersionNumber,
  55. MinorVersion: minorVersionNumber,
  56. UBR: revision,
  57. }, nil
  58. }
  59. //GetPatchVersion returns full OS version with patch
  60. func (o *OSInfo) GetPatchVersion() string {
  61. return fmt.Sprintf("%d.%d.%s.%d", o.MajorVersion, o.MinorVersion, o.BuildNumber, o.UBR)
  62. }
  63. //GetBuild returns OS version upto build number
  64. func (o *OSInfo) GetBuild() string {
  65. return fmt.Sprintf("%d.%d.%s", o.MajorVersion, o.MinorVersion, o.BuildNumber)
  66. }