Files
Cyrene_v2/internal/platform/platform.go
T

109 lines
2.6 KiB
Go

// Package platform centralizes OS detection, path resolution, signal handling,
// and runtime system information. Other packages must never call syscall or
// hard-code OS paths; they resolve everything through this package.
package platform
import (
"os"
"runtime"
"strconv"
)
// Info describes the host platform.
type Info struct {
Name string // human-readable platform name
GOOS string
GOARCH string
Kind string // platform family: desktop / server / mobile / unknown
CPUs int
Host string
GoVer string
}
// Detect returns runtime-derived platform information.
func Detect() Info {
return Info{
Name: platformName(runtime.GOOS),
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
Kind: platformKind(runtime.GOOS),
CPUs: runtime.NumCPU(),
Host: hostname(),
GoVer: runtime.Version(),
}
}
// GOOS returns the operating system name (runtime.GOOS).
func GOOS() string { return runtime.GOOS }
// GOARCH returns the CPU architecture name (runtime.GOARCH).
func GOARCH() string { return runtime.GOARCH }
// CPUs returns the number of logical CPUs visible to the runtime.
func CPUs() int { return runtime.NumCPU() }
// IsWindows reports whether the host is Windows.
func IsWindows() bool { return runtime.GOOS == "windows" }
// IsDarwin reports whether the host is macOS.
func IsDarwin() bool { return runtime.GOOS == "darwin" }
// IsLinux reports whether the host is Linux.
func IsLinux() bool { return runtime.GOOS == "linux" }
// IsUnix reports whether the host is a Unix-like OS (linux, darwin, etc.).
func IsUnix() bool { return runtime.GOOS != "windows" }
// CPUArch converts runtime.GOARCH to a compact architecture label.
func CPUArch() string { return archLabel(runtime.GOARCH) }
// archLabel maps a GOARCH value to a compact human-readable label. Unknown
// values pass through unchanged.
func archLabel(goarch string) string {
switch goarch {
case "amd64":
return "x86_64"
case "386":
return "x86"
default:
return goarch
}
}
// Describe renders a short log-ready summary of the runtime.
func (i Info) Describe() string {
return i.Name + "/" + i.GOOS + "/" + i.GOARCH + " cpu=" + strconv.Itoa(i.CPUs) + " go=" + i.GoVer
}
func platformName(goos string) string {
switch goos {
case "windows":
return "Windows"
case "darwin":
return "macOS"
case "linux":
return "Linux"
default:
return goos
}
}
func platformKind(goos string) string {
switch goos {
case "windows", "darwin":
return "desktop"
case "linux":
return "server"
default:
return "unknown"
}
}
func hostname() string {
h, err := os.Hostname()
if err != nil {
return "unknown"
}
return h
}