Browse Source

pkg/platform: use const for OSType, improve GoDoc

It was not immediately clear why we were not using runtime.GOARCH for
these (with a conversion to other formats, such as x86_64). These docs
are based on comments that were posted when implementing this package;

- https://github.com/moby/moby/pull/13921#issuecomment-130106474
- https://github.com/moby/moby/pull/13921#issuecomment-140270124

Some links were now redirecting to a new location, so updated them to
not depend on the redirect.

While at it, also updated a call to logrus to use structured formatting
(WithError()).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 2 years ago
parent
commit
790dd8cc92
2 changed files with 18 additions and 10 deletions
  1. 13 8
      pkg/platform/platform.go
  2. 5 2
      pkg/platform/platform_windows.go

+ 13 - 8
pkg/platform/platform.go

@@ -8,18 +8,23 @@ import (
 	"github.com/sirupsen/logrus"
 	"github.com/sirupsen/logrus"
 )
 )
 
 
-var (
-	// Architecture holds the runtime architecture of the process.
-	Architecture string
-	// OSType holds the runtime operating system type (Linux, …) of the process.
-	OSType string
-)
+// Architecture holds the runtime architecture of the process.
+//
+// Unlike [runtime.GOARCH] (which refers to the compiler platform),
+// Architecture refers to the running platform.
+//
+// For example, Architecture reports "x86_64" as architecture, even
+// when running a "linux/386" compiled binary on "linux/amd64" hardware.
+var Architecture string
+
+// OSType holds the runtime operating system type of the process. It is
+// an alias for [runtime.GOOS].
+const OSType = runtime.GOOS
 
 
 func init() {
 func init() {
 	var err error
 	var err error
 	Architecture, err = runtimeArchitecture()
 	Architecture, err = runtimeArchitecture()
 	if err != nil {
 	if err != nil {
-		logrus.Errorf("Could not read system architecture info: %v", err)
+		logrus.WithError(err).Error("Could not read system architecture info")
 	}
 	}
-	OSType = runtime.GOOS
 }
 }

+ 5 - 2
pkg/platform/platform_windows.go

@@ -13,7 +13,7 @@ var (
 	procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
 	procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
 )
 )
 
 
-// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx
+// see https://learn.microsoft.com/en-gb/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
 type systeminfo struct {
 type systeminfo struct {
 	wProcessorArchitecture      uint16
 	wProcessorArchitecture      uint16
 	wReserved                   uint16
 	wReserved                   uint16
@@ -28,7 +28,10 @@ type systeminfo struct {
 	wProcessorRevision          uint16
 	wProcessorRevision          uint16
 }
 }
 
 
-// Constants
+// Windows processor architectures.
+//
+// see https://github.com/microsoft/go-winio/blob/v0.6.0/wim/wim.go#L48-L65
+// see https://learn.microsoft.com/en-gb/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
 const (
 const (
 	ProcessorArchitecture64    = 9  // PROCESSOR_ARCHITECTURE_AMD64
 	ProcessorArchitecture64    = 9  // PROCESSOR_ARCHITECTURE_AMD64
 	ProcessorArchitectureIA64  = 6  // PROCESSOR_ARCHITECTURE_IA64
 	ProcessorArchitectureIA64  = 6  // PROCESSOR_ARCHITECTURE_IA64