kv-clearnup/backend/internal/platform/platform_windows.go

106 lines
3 KiB
Go

//go:build windows
package platform
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func OpenSettings() error {
// Open Windows Settings -> Storage
// ms-settings:storagesense
return exec.Command("cmd", "/c", "start", "ms-settings:storagesense").Run()
}
func GetSystemInfo() (*SystemInfo, error) {
// Use systeminfo or wmic
// simpler: generic info
info := &SystemInfo{
Model: "PC",
Chip: "Unknown",
Memory: "Unknown",
OS: "Windows",
}
// Helper to run powershell and get string result
runPS := func(cmd string) string {
out, err := exec.Command("powershell", "-NoProfile", "-Command", cmd).Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
// 1. Get OS Name (Simplified)
// Get-CimInstance Win32_OperatingSystem | Select-Object -ExpandProperty Caption
osName := runPS("(Get-CimInstance Win32_OperatingSystem).Caption")
if osName != "" {
info.OS = strings.TrimPrefix(osName, "Microsoft ")
}
// 2. Get Memory (in GB)
// [math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB)
mem := runPS("[math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB)")
if mem != "" {
info.Memory = mem + " GB"
}
// 3. Get CPU Name
// (Get-CimInstance Win32_Processor).Name
cpu := runPS("(Get-CimInstance Win32_Processor).Name")
if cpu != "" {
// Cleanup CPU string (remove extra spaces)
info.Chip = strings.Join(strings.Fields(cpu), " ")
}
return info, nil
}
func EmptyTrash() error {
// PowerShell to empty Recycle Bin
// Clear-RecycleBin -Force -ErrorAction SilentlyContinue
// PowerShell to empty Recycle Bin
// Clear-RecycleBin -Force -ErrorAction SilentlyContinue
// We use ExecutionPolicy Bypass to avoid permission issues.
// We also catch errors to prevent 500s on empty bins.
cmd := exec.Command("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", "Clear-RecycleBin -Force -ErrorAction SilentlyContinue")
// If it returns an error, it might be due to permissions or being already empty.
// We can ignore the error for now to check if that fixes the User's 500.
err := cmd.Run()
if err != nil {
// Log it but return nil effectively?
// For now, let's return nil because 'Empty Trash' is best-effort.
// If the user really has a permission issue, it acts as a no-op which is better than a crash.
fmt.Printf("EmptyTrash warning: %v\n", err)
return nil
}
return nil
}
func GetCachePath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, "AppData", "Local", "Temp"), nil
}
func GetDockerPath() (string, error) {
path, err := exec.LookPath("docker")
if err == nil {
return path, nil
}
// Common Windows path?
return "", fmt.Errorf("docker not found")
}
func OpenBrowser(url string) error {
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
}