init
This commit is contained in:
83
internal/platform/windows.go
Normal file
83
internal/platform/windows.go
Normal file
@@ -0,0 +1,83 @@
|
||||
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 {
|
||||
// Check if running as administrator on 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
|
||||
}
|
||||
Reference in New Issue
Block a user