mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
Size.Backend dropped strconv's errors, so a VM's size in vm.json that isn't a number, such as "cpus": "four" after an edit by hand, read as 0, the image's, and the VM started with the image's size without a word. Backend now reads each part with the check that took it (settings.CPUCount, settings.GiB, which ParseCPUs and ParseGiB now use), and returns an error naming the key and value. storedSize turns a stored one into size_stored_invalid, naming the VM. Every start (bringUp, before anything runs), a restart's stop (stopToStart, so a running VM keeps running, as a refused proxy start leaves it), devvm size and a remake's check refuse with it; devvm size --KEY sets it again. ParseCPUs and ParseGiB also refuse a number too big for an int or a float64, which for --disk reached Lima as +Inf. Lima's originOf read base's disk with ParseFloat and dropped its error: "NaN", "Inf" and "1e400" passed its check of a size above 0. Only devvm's own Origin writes that step (gibString of BaseDiskGiB), so no user, VM or limactl output reaches it; it now reads it with settings.GiB and fails naming the value. Every other discarded strconv error outside tests goes too: - hostgit.Bool reads a boolean as git does (yes, on, whole numbers) and says when a value isn't one; GIT_CONFIG_NOSYSTEM, which strconv.ParseBool read as false for "yes", uses it, as does the clone's copy of a repo's config (gitBool, which had its own copy). - image.GlibcOlder checks both versions itself and reports ok, so one that isn't a version is never read as 0. - leaktest and state's lock order panic on a goroutine number runtime.Stack doesn't print today, rather than reading it as 0. - cli's test-build overrides fail on a number that isn't one (a test build's panic), the stub limactl refuses a size it can't read, and opencodeMajor's fallback to 0 is explicit. Tests: TestSizeBackend, TestStoredSizeInvalid (a stopped VM and a running one's restart), TestSizeNumbers, new TestParseSizes and TestOriginOfRefuses cases, TestParseUnknownFormat, TestBool, TestUserFilesOf's git booleans, TestGlibc's invalid versions; each fails with its fix reverted. Co-Authored-By: Claude Opus 5.5 <[email protected]>
100 lines
3.0 KiB
Go
100 lines
3.0 KiB
Go
package settings
|
|
|
|
import (
|
|
"regexp"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
)
|
|
|
|
var (
|
|
wholeNumber = regexp.MustCompile(`^[1-9][0-9]*$`)
|
|
gibNumber = regexp.MustCompile(`^[0-9]+(\.[0-9]+)?$`)
|
|
)
|
|
|
|
// ParseCPUs checks a CPU count, as flag (--cpus, or config.toml's cpus)
|
|
// gives it.
|
|
func ParseCPUs(flag, v string) (string, error) {
|
|
if _, err := CPUCount(flag, v); err != nil {
|
|
return "", err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// CPUCount is ParseCPUs's check, returning the count: a whole number
|
|
// from 1 that fits an int, so one too big for it is refused rather than
|
|
// read as another.
|
|
func CPUCount(flag, v string) (int, error) {
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil || !wholeNumber.MatchString(v) {
|
|
return 0, msg.CpusNotNumber(flag, strconv.Quote(v)).Because(err)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// ParseGiB checks a size in GiB, as flag (--memory, --disk, or
|
|
// config.toml's memory or disk) gives it: N, NG or NGiB, decimals
|
|
// allowed, more than 0 and short of a float's infinity. It returns N.
|
|
func ParseGiB(flag, v string) (string, error) {
|
|
n := strings.TrimSuffix(v, "iB")
|
|
n = strings.TrimSuffix(strings.TrimSuffix(n, "G"), "g")
|
|
if _, err := strconv.ParseFloat(n, 64); err != nil || !gibNumber.MatchString(n) || !strings.ContainsAny(n, "123456789") {
|
|
return "", msg.SizeNotGib(flag, strconv.Quote(v)).Because(err)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// GiB is ParseGiB's check, returning the size in GiB.
|
|
func GiB(flag, v string) (float64, error) {
|
|
n, err := ParseGiB(flag, v)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return strconv.ParseFloat(n, 64)
|
|
}
|
|
|
|
// Ports values: every port the VM listens on is forwarded, or none.
|
|
const (
|
|
PortsAll = "all"
|
|
PortsNone = "none"
|
|
)
|
|
|
|
// ParsePorts checks which ports to forward, as --ports, config.toml or
|
|
// settings.json give them: all, none, or ports and ranges
|
|
// (5173,8000-8010, backend.ParsePortRanges). It returns the value without
|
|
// spaces or repeats.
|
|
func ParsePorts(v string) (string, error) {
|
|
if s := strings.ReplaceAll(v, " ", ""); s == PortsAll || s == PortsNone {
|
|
return s, nil
|
|
}
|
|
rs, err := backend.ParsePortRanges(v)
|
|
if err != nil {
|
|
return "", msg.PortInvalid(strconv.Quote(v)).Because(err)
|
|
}
|
|
return backend.FormatPortRanges(rs), nil
|
|
}
|
|
|
|
// NetworkDefault is new VMs' network tier unless one is set:
|
|
// netpolicy.TierDefault, which netpolicy's tests check it matches.
|
|
const NetworkDefault = "allowlist"
|
|
|
|
// NetworkChoices are the network defaults, the tiers, in the order devvm
|
|
// shows them, lowest to highest. They are netpolicy's (netpolicy.Tiers,
|
|
// which imports this package), and netpolicy's tests check the two lists
|
|
// match. Learning the hosts a VM needs is no default: each VM is made to
|
|
// learn on its own.
|
|
func NetworkChoices() []string {
|
|
return []string{"open", "internet", NetworkDefault, "none"}
|
|
}
|
|
|
|
// checkNetwork refuses a network default that isn't a tier.
|
|
func checkNetwork(v string) error {
|
|
if !slices.Contains(NetworkChoices(), v) {
|
|
return errNotChoice(v, NetworkChoices())
|
|
}
|
|
return nil
|
|
}
|