Add a stub library for glazier/os. Add more ProductTypes.

PiperOrigin-RevId: 404523885
This commit is contained in:
Matt LaPlante
2021-10-20 07:12:03 -07:00
committed by Copybara-Service
parent e60a0495f2
commit 79a2756d29
3 changed files with 75 additions and 23 deletions

View File

@@ -12,18 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Package os supports querying information about the local operating system.
package os
import (
"errors"
"github.com/StackExchange/wmi"
)
import "errors"
var (
// ErrWMIEmptyResult indicates a condition where WMI failed to return the expected values.
ErrWMIEmptyResult = errors.New("WMI returned without error, but zero results")
// ErrNotImplemented is returned for unimplemented calls
ErrNotImplemented = errors.New("call is not implemented on this platform")
)
// Win32_OperatingSystem models the WMI object of the same name.
@@ -37,23 +34,10 @@ type Type string
var (
// Client represents a client operating system (eg Windows 10)
Client Type = "client"
// DomainController represents a server operating system acting as a domain controller
DomainController Type = "domain controller"
// Server represents a server operating system (eg Windows Server 2019)
Server Type = "server"
// Unknown represents an unsupported type
Unknown Type = "unknown"
)
// GetType attempts to distinguish between client and server OS.
func GetType() (Type, error) {
var result []Win32_OperatingSystem
if err := wmi.Query(wmi.CreateQuery(&result, ""), &result); err != nil {
return Server, err
}
if len(result) < 1 {
return Server, ErrWMIEmptyResult
}
if result[0].ProductType == 1 {
return Client, nil
}
return Server, nil
}

23
go/os/os_stub.go Normal file
View File

@@ -0,0 +1,23 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !windows
// +build !windows
package os
// GetType attempts to distinguish between client and server OS.
func GetType() (Type, error) {
return Unknown, ErrNotImplemented
}

45
go/os/os_windows.go Normal file
View File

@@ -0,0 +1,45 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
// +build windows
// Package os supports querying information about the local operating system.
package os
import (
"github.com/StackExchange/wmi"
)
// GetType attempts to distinguish between client and server OS.
func GetType() (Type, error) {
var result []Win32_OperatingSystem
if err := wmi.Query(wmi.CreateQuery(&result, ""), &result); err != nil {
return Server, err
}
if len(result) < 1 {
return Server, ErrWMIEmptyResult
}
switch result[0].ProductType {
case 1:
return Client, nil
case 2:
return DomainController, nil
case 3:
return Server, nil
default:
return Unknown, nil
}
}