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

114 lines
2.4 KiB
Go

//go:build darwin
package platform
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
)
func OpenSettings() error {
return exec.Command("open", "x-apple.systempreferences:com.apple.settings.Storage").Run()
}
func GetSystemInfo() (*SystemInfo, error) {
// Structs for parsing system_profiler JSON
type HardwareItem struct {
MachineName string `json:"machine_name"`
ChipType string `json:"chip_type"`
PhysicalMemory string `json:"physical_memory"`
}
type SoftwareItem struct {
OSVersion string `json:"os_version"`
}
type SystemProfile struct {
Hardware []HardwareItem `json:"SPHardwareDataType"`
Software []SoftwareItem `json:"SPSoftwareDataType"`
}
cmd := exec.Command("system_profiler", "SPHardwareDataType", "SPSoftwareDataType", "-json")
output, err := cmd.Output()
if err != nil {
return nil, err
}
var profile SystemProfile
if err := json.Unmarshal(output, &profile); err != nil {
return nil, err
}
info := &SystemInfo{
Model: "Unknown",
Chip: "Unknown",
Memory: "Unknown",
OS: "Unknown",
}
if len(profile.Hardware) > 0 {
info.Model = profile.Hardware[0].MachineName
info.Chip = profile.Hardware[0].ChipType
info.Memory = profile.Hardware[0].PhysicalMemory
}
if len(profile.Software) > 0 {
info.OS = profile.Software[0].OSVersion
}
return info, nil
}
func EmptyTrash() error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
trashPath := filepath.Join(home, ".Trash")
entries, err := os.ReadDir(trashPath)
if err != nil {
return err
}
for _, entry := range entries {
itemPath := filepath.Join(trashPath, entry.Name())
os.RemoveAll(itemPath)
}
return nil
}
func GetCachePath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, "Library", "Caches"), nil
}
func GetDockerPath() (string, error) {
dockerPath, err := exec.LookPath("docker")
if err != nil {
// Try common locations
commonPaths := []string{
"/usr/local/bin/docker",
"/opt/homebrew/bin/docker",
"/Applications/Docker.app/Contents/Resources/bin/docker",
}
for _, p := range commonPaths {
if _, e := os.Stat(p); e == nil {
dockerPath = p
return dockerPath, nil
}
}
}
if dockerPath != "" {
return dockerPath, nil
}
return "", fmt.Errorf("docker not found")
}
func OpenBrowser(url string) error {
return exec.Command("open", url).Start()
}