init
This commit is contained in:
82
internal/platform/linux.go
Normal file
82
internal/platform/linux.go
Normal file
@@ -0,0 +1,82 @@
|
||||
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: detect properly
|
||||
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: Add to shell profile
|
||||
return os.Setenv(key, value)
|
||||
}
|
||||
|
||||
func (p *LinuxPlatform) AddToPath(ctx context.Context, dir string) error {
|
||||
// TODO: Add to shell profile
|
||||
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"
|
||||
}
|
||||
Reference in New Issue
Block a user