64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
// Package platform detects the host OS/distro/WSL status.
|
|
package platform
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// Distro is a narrow enum of distros we care about.
|
|
type Distro string
|
|
|
|
const (
|
|
DistroArch Distro = "arch"
|
|
DistroUnknown Distro = "unknown"
|
|
)
|
|
|
|
// Platform holds detected host information.
|
|
type Platform struct {
|
|
Distro Distro
|
|
IsWSL bool
|
|
Arch string // e.g. "amd64", "arm64"
|
|
}
|
|
|
|
// Detect reads the live system. Returns DistroUnknown rather than an error
|
|
// when /etc/os-release is missing or unrecognized; callers decide what to do.
|
|
func Detect() (Platform, error) {
|
|
f, err := os.Open("/etc/os-release")
|
|
distro := DistroUnknown
|
|
if err == nil {
|
|
distro = parseDistro(f)
|
|
_ = f.Close()
|
|
} else if !os.IsNotExist(err) {
|
|
return Platform{}, fmt.Errorf("open /etc/os-release: %w", err)
|
|
}
|
|
|
|
wsl := false
|
|
if data, err := os.ReadFile("/proc/sys/kernel/osrelease"); err == nil {
|
|
lower := strings.ToLower(string(data))
|
|
wsl = strings.Contains(lower, "microsoft") || strings.Contains(lower, "wsl")
|
|
}
|
|
|
|
return Platform{Distro: distro, IsWSL: wsl, Arch: runtime.GOARCH}, nil
|
|
}
|
|
|
|
func parseDistro(r io.Reader) Distro {
|
|
sc := bufio.NewScanner(r)
|
|
for sc.Scan() {
|
|
line := sc.Text()
|
|
if !strings.HasPrefix(line, "ID=") {
|
|
continue
|
|
}
|
|
id := strings.Trim(strings.TrimPrefix(line, "ID="), `"`)
|
|
if id == "arch" {
|
|
return DistroArch
|
|
}
|
|
return DistroUnknown
|
|
}
|
|
return DistroUnknown
|
|
}
|