84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package platform
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
// WindowsPlatform Windows 平台实现
|
|
type WindowsPlatform struct {
|
|
homeDir string
|
|
}
|
|
|
|
// NewWindowsPlatform 创建 Windows 平台实例
|
|
func NewWindowsPlatform() *WindowsPlatform {
|
|
return &WindowsPlatform{
|
|
homeDir: os.Getenv("USERPROFILE"),
|
|
}
|
|
}
|
|
|
|
func (p *WindowsPlatform) OS() string { return "windows" }
|
|
func (p *WindowsPlatform) Arch() string { return os.Getenv("PROCESSOR_ARCHITECTURE") }
|
|
|
|
func (p *WindowsPlatform) HomeDir() string {
|
|
return p.homeDir
|
|
}
|
|
|
|
func (p *WindowsPlatform) ConfigDir() string {
|
|
if dir := os.Getenv("APPDATA"); dir != "" {
|
|
return dir
|
|
}
|
|
return p.homeDir + "\\AppData\\Roaming"
|
|
}
|
|
|
|
func (p *WindowsPlatform) DataDir() string {
|
|
if dir := os.Getenv("LOCALAPPDATA"); dir != "" {
|
|
return dir
|
|
}
|
|
return p.homeDir + "\\AppData\\Local"
|
|
}
|
|
|
|
func (p *WindowsPlatform) GetEnvVar(key string) string {
|
|
return os.Getenv(key)
|
|
}
|
|
|
|
func (p *WindowsPlatform) SetEnvVar(ctx context.Context, key, value string) error {
|
|
cmd := exec.CommandContext(ctx, "setx", key, value)
|
|
return cmd.Run()
|
|
}
|
|
|
|
func (p *WindowsPlatform) AddToPath(ctx context.Context, dir string) error {
|
|
currentPath := os.Getenv("PATH")
|
|
newPath := dir + ";" + currentPath
|
|
return p.SetEnvVar(ctx, "PATH", newPath)
|
|
}
|
|
|
|
func (p *WindowsPlatform) IsAdmin() bool {
|
|
// 检查 Windows 上是否以管理员身份运行
|
|
cmd := exec.Command("net", "session")
|
|
err := cmd.Run()
|
|
return err == nil
|
|
}
|
|
|
|
func (p *WindowsPlatform) PackageManagers() []string {
|
|
var pms []string
|
|
if _, err := exec.LookPath("scoop"); err == nil {
|
|
pms = append(pms, "scoop")
|
|
}
|
|
if _, err := exec.LookPath("choco"); err == nil {
|
|
pms = append(pms, "chocolatey")
|
|
}
|
|
if _, err := exec.LookPath("winget"); err == nil {
|
|
pms = append(pms, "winget")
|
|
}
|
|
return pms
|
|
}
|
|
|
|
func (p *WindowsPlatform) DefaultShell() string {
|
|
if _, err := exec.LookPath("pwsh"); err == nil {
|
|
return "pwsh" // PowerShell 7+ 版本
|
|
}
|
|
return "powershell" // Windows PowerShell 5.1
|
|
}
|