operatingsystem_windows.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2020 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package machine
  15. import (
  16. "fmt"
  17. "golang.org/x/sys/windows/registry"
  18. )
  19. func getOperatingSystem() (string, error) {
  20. system := "Windows"
  21. k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
  22. if err != nil {
  23. return system, err
  24. }
  25. defer k.Close()
  26. productName, _, err := k.GetStringValue("ProductName")
  27. if err != nil {
  28. return system, nil
  29. }
  30. releaseId, _, err := k.GetStringValue("ReleaseId")
  31. if err != nil {
  32. return system, err
  33. }
  34. currentBuildNumber, _, err := k.GetStringValue("CurrentBuildNumber")
  35. if err != nil {
  36. return system, err
  37. }
  38. revision, _, err := k.GetIntegerValue("UBR")
  39. if err != nil {
  40. return system, err
  41. }
  42. system = fmt.Sprintf("%s Version %s (OS Build %s.%d)",
  43. productName, releaseId, currentBuildNumber, revision)
  44. return system, nil
  45. }