Files
simslim-mirror/disk.go
Interlap ffd3a13d34 refactor: split into an importable library and a cmd/simslim CLI (#14)
The repo root becomes `package simslim`, an importable library holding every
piece of slimming logic; the CLI moves to `cmd/simslim`. Previously everything
was `package main`, so nothing here could be imported at all.

The library carries no external dependencies — urfave/cli stays on the CLI side,
along with all terminal-facing code: the interactive profile wizard, writeJSON,
truncate/humanBytes, fatal and usage. The library never writes to stdout; it
returns values and reports progress through the Reporter callback.

Renames forced by collisions with existing types: status() -> ReadStatus,
statusForDevice() -> ReadStatusForDevice, diskCleanupPlan() -> PlanDiskCleanup.
The shutdown/boot timeouts move to simctl.go as ShutdownTimeout/BootTimeout.

`go install` gains a path element: github.com/mobai-app/simslim/cmd/simslim.
The installed binary is still named simslim, and -X main.version still applies.
packaging/homebrew/simslim.rb.in is updated to match, since std_go_args passes
no package path.

Verified live against a real simulator: list, profiles, status, doctor, size,
disk-plan, measure, boot, shutdown, on, off, on --except, on --profile, clone,
rename, delete and the interactive wizard all behave identically to the previous
build. JSON output, human output, help text and exit codes are unchanged.
2026-07-23 12:39:20 +02:00

43 lines
1.2 KiB
Go

package simslim
import (
"context"
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
// deviceDiskUsage reports allocated filesystem blocks for one exact simulator
// device directory. It deliberately resolves the UDID through simctl first so
// aliases such as "all" can never become filesystem paths.
func DeviceDiskUsage(ctx context.Context, udid string) (DiskMeasurement, error) {
_, dataDirectory, err := simulatorDataDirectory(ctx, udid)
if err != nil {
return DiskMeasurement{}, err
}
devicePath := filepath.Dir(dataDirectory)
out, err := exec.CommandContext(ctx, "du", "-sk", devicePath).CombinedOutput()
if err != nil {
return DiskMeasurement{}, fmt.Errorf("measure simulator disk usage: %w: %s", err, strings.TrimSpace(string(out)))
}
bytes, err := parseDUDiskUsage(out)
if err != nil {
return DiskMeasurement{}, err
}
return DiskMeasurement{Bytes: bytes}, nil
}
func parseDUDiskUsage(output []byte) (int64, error) {
fields := strings.Fields(string(output))
if len(fields) == 0 {
return 0, fmt.Errorf("du returned no disk usage")
}
kib, err := strconv.ParseInt(fields[0], 10, 64)
if err != nil || kib < 0 {
return 0, fmt.Errorf("parse du disk usage %q", fields[0])
}
return kib * 1024, nil
}