| | | 1 | | // Package domain contains the core data types and pure business logic for neospec. |
| | | 2 | | // Nothing in this package performs I/O or depends on external packages — it is |
| | | 3 | | // the stable heart of the application that all other packages point toward. |
| | | 4 | | package domain |
| | | 5 | | |
| | | 6 | | import ( |
| | | 7 | | "fmt" |
| | | 8 | | "runtime" |
| | | 9 | | ) |
| | | 10 | | |
| | | 11 | | // OS represents a target operating system. |
| | | 12 | | type OS string |
| | | 13 | | |
| | | 14 | | // Supported operating systems. |
| | | 15 | | const ( |
| | | 16 | | OSLinux OS = "linux" |
| | | 17 | | OSDarwin OS = "darwin" |
| | | 18 | | OSWindows OS = "windows" |
| | | 19 | | ) |
| | | 20 | | |
| | | 21 | | // Arch represents a CPU architecture. |
| | | 22 | | type Arch string |
| | | 23 | | |
| | | 24 | | // Supported CPU architectures. |
| | | 25 | | const ( |
| | | 26 | | ArchAMD64 Arch = "x86_64" |
| | | 27 | | ArchARM64 Arch = "arm64" |
| | | 28 | | ) |
| | | 29 | | |
| | | 30 | | // Platform combines an OS and architecture for use in Neovim release asset selection. |
| | | 31 | | type Platform struct { |
| | | 32 | | OS OS |
| | | 33 | | Arch Arch |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | // CurrentPlatform returns the Platform for the running process. |
| | | 37 | | func CurrentPlatform() (Platform, error) { |
| | | 38 | | return parsePlatform(runtime.GOOS, runtime.GOARCH) |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | // parsePlatform maps a GOOS/GOARCH pair to a Platform. It is the testable |
| | | 42 | | // inner function for CurrentPlatform — callers that need to simulate unusual |
| | | 43 | | // or unsupported platforms can call it directly in white-box tests. |
| | | 44 | | func parsePlatform(goos, goarch string) (Platform, error) { |
| | | 45 | | var os OS |
| | | 46 | | switch goos { |
| | | 47 | | case "linux": |
| | | 48 | | os = OSLinux |
| | | 49 | | case "darwin": |
| | | 50 | | os = OSDarwin |
| | | 51 | | case "windows": |
| | | 52 | | os = OSWindows |
| | | 53 | | default: |
| | | 54 | | return Platform{}, fmt.Errorf("unsupported OS: %s", goos) |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | var arch Arch |
| | | 58 | | switch goarch { |
| | | 59 | | case "amd64": |
| | | 60 | | arch = ArchAMD64 |
| | | 61 | | case "arm64": |
| | | 62 | | arch = ArchARM64 |
| | | 63 | | default: |
| | | 64 | | return Platform{}, fmt.Errorf("unsupported architecture: %s", goarch) |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | return Platform{OS: os, Arch: arch}, nil |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // String returns a human-readable platform string, e.g. "linux/x86_64". |
| | 10 | 71 | | func (p Platform) String() string { |
| | 10 | 72 | | return fmt.Sprintf("%s/%s", p.OS, p.Arch) |
| | 10 | 73 | | } |