83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package platform
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
// LinuxPlatform Linux 平台实现
|
|
type LinuxPlatform struct {
|
|
homeDir string
|
|
}
|
|
|
|
// NewLinuxPlatform 创建 Linux 平台实例
|
|
func NewLinuxPlatform() *LinuxPlatform {
|
|
home, _ := os.UserHomeDir()
|
|
return &LinuxPlatform{homeDir: home}
|
|
}
|
|
|
|
func (p *LinuxPlatform) OS() string { return "linux" }
|
|
func (p *LinuxPlatform) Arch() string { return "amd64" } // TODO: 正确检测架构
|
|
func (p *LinuxPlatform) HomeDir() string { return p.homeDir }
|
|
|
|
func (p *LinuxPlatform) ConfigDir() string {
|
|
if dir := os.Getenv("XDG_CONFIG_HOME"); dir != "" {
|
|
return dir
|
|
}
|
|
return p.homeDir + "/.config"
|
|
}
|
|
|
|
func (p *LinuxPlatform) DataDir() string {
|
|
if dir := os.Getenv("XDG_DATA_HOME"); dir != "" {
|
|
return dir
|
|
}
|
|
return p.homeDir + "/.local/share"
|
|
}
|
|
|
|
func (p *LinuxPlatform) GetEnvVar(key string) string {
|
|
return os.Getenv(key)
|
|
}
|
|
|
|
func (p *LinuxPlatform) SetEnvVar(ctx context.Context, key, value string) error {
|
|
// TODO: 写入 Shell 配置文件以持久化
|
|
return os.Setenv(key, value)
|
|
}
|
|
|
|
func (p *LinuxPlatform) AddToPath(ctx context.Context, dir string) error {
|
|
// TODO: 添加到 Shell 配置文件
|
|
return nil
|
|
}
|
|
|
|
func (p *LinuxPlatform) IsAdmin() bool {
|
|
return os.Geteuid() == 0
|
|
}
|
|
|
|
func (p *LinuxPlatform) PackageManagers() []string {
|
|
var pms []string
|
|
if _, err := exec.LookPath("apt"); err == nil {
|
|
pms = append(pms, "apt")
|
|
}
|
|
if _, err := exec.LookPath("dnf"); err == nil {
|
|
pms = append(pms, "dnf")
|
|
}
|
|
if _, err := exec.LookPath("yum"); err == nil {
|
|
pms = append(pms, "yum")
|
|
}
|
|
if _, err := exec.LookPath("pacman"); err == nil {
|
|
pms = append(pms, "pacman")
|
|
}
|
|
if _, err := exec.LookPath("snap"); err == nil {
|
|
pms = append(pms, "snap")
|
|
}
|
|
return pms
|
|
}
|
|
|
|
func (p *LinuxPlatform) DefaultShell() string {
|
|
shell := os.Getenv("SHELL")
|
|
if shell != "" {
|
|
return shell
|
|
}
|
|
return "/bin/bash"
|
|
}
|