mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
vmname.VM is the name of every machine devvm runs on a backend: a project VM, an image's machine (devvm-img-<key>), a build VM and a remake's aside. Fields, parameters, results and map keys that held a VM's name as a string now hold a VM, from the backend interface down to the CLI's --json structs, whose output is byte for byte the same. Boundary parsers, the only places a string becomes a VM: - vmname.Parse: a project VM's name (state's vm.json name, export file names, devvm _links's argument, the forwarder's -vm, project.Nth) - vmname.ParseListed: one path element, for what a backend lists - VM.UnmarshalText: JSON (Lima's list, proxy config and events, links' targets in state, link lists); empty stays no VM - vmname.Image(key), VM.Aside(), and typed constants for Lima's build VMs - msg.M.VM: the VM a message's function took, read back vmname's functions on names became methods (Hash, Slug, Short, Aside, AsideHash, ImageKey, IsProject); Lima's image prefix moved to vmname; vmname.Join joins VM lists. Messages' vm and vm_name fields are typed by the generator. Seven messages whose field took something else became what= (backend_lacks, backend_unreviewed, backend_unreviewed_used, starting, deleting, configuring, running_script) and rm_vm_failed's name=: the text is unchanged, the names-mode scripts follow. Review.RequireFor names an image or the tools being built. Typed sites per package (added lines with vmname.VM, non-test): msg 347 (generated), vm 124, lima 48, backendtest 32, vmssh 31, netpolicy 22, backend 16, gitprotect 14, proxy 11, cli 11, project 10, state 8, ports 6, paths 3, sshagent 2, sshconfig 2, tools 2. The structure test's owners rule now fails on a field, parameter or result named for a VM (vm, vms, otherVM, vmName) typed string, []string or map[string]…, and on converting anything but a literal to vmname.VM outside the files it lists; internal/stubtest is free. depguard holds internal/vmname to the standard library, and the proxy may import it. ParseListed and UnmarshalText refuse a name starting with -, which would read as a flag in limactl's argv, and vmname.Image refuses a key that's empty, holds a / or NUL, or makes a project VM's name, so Lima's ImageSize and DeleteImage no longer check it themselves. The check also fails on a *string VM name and an alias of vmname.VM, and says what it doesn't see. Co-Authored-By: Claude Opus 5.5 <[email protected]>
144 lines
4.7 KiB
Go
144 lines
4.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/settings"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// portsOutput is devvm ports --json: which ports the VM forwards, and
|
|
// those of its listed ports other VMs forward now, by VM.
|
|
type portsOutput struct {
|
|
Ports string `json:"ports"`
|
|
HeldBy []portsHeldBy `json:"held_by,omitempty"`
|
|
}
|
|
|
|
// portsHeldBy is some of a VM's listed ports, which another VM forwards
|
|
// now.
|
|
type portsHeldBy struct {
|
|
VM vmname.VM `json:"vm"`
|
|
Ports string `json:"ports"`
|
|
}
|
|
|
|
func portsJSON(s vm.PortsShown) portsOutput {
|
|
out := portsOutput{Ports: s.List}
|
|
for _, h := range s.Held {
|
|
out.HeldBy = append(out.HeldBy, portsHeldBy(h))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// newPortsCmd is devvm ports [all|none|LIST], and add and rm LIST, each
|
|
// with -g for what new VMs forward.
|
|
func newPortsCmd(a *app) *cobra.Command {
|
|
u := a.u
|
|
var global, asJSON bool
|
|
// run shows the ports without a change, else makes it.
|
|
run := func(cmd *cobra.Command, c *vm.PortsChange) error {
|
|
var (
|
|
ports vm.PortsShown
|
|
def settings.Value
|
|
label string
|
|
)
|
|
err := a.env(func(env *vm.Env) error {
|
|
s := vm.Settings{Env: env}
|
|
var err error
|
|
switch {
|
|
case c != nil:
|
|
c.Global = global
|
|
return s.SetPorts(cmd.Context(), *c)
|
|
case global:
|
|
def, err = s.PortsDefault()
|
|
label = def.Source.Label(env.Dirs)
|
|
default:
|
|
ports, err = s.Ports(cmd.Context())
|
|
}
|
|
return err
|
|
}, msg.PortsFailed)
|
|
switch {
|
|
case err != nil || c != nil:
|
|
return err
|
|
case global:
|
|
return printDefault(u, asJSON, "ports", def, label)
|
|
}
|
|
if asJSON {
|
|
return printJSON(u, portsJSON(ports))
|
|
}
|
|
return printValue(u, false, "ports", ports.List)
|
|
}
|
|
cmd := &cobra.Command{
|
|
Use: "ports [all|none|LIST]",
|
|
Annotations: about{docs: "using/ports-size#ports", group: groupChange}.annotations(),
|
|
Short: "Show or change which ports this folder's VM forwards",
|
|
Long: "Show which ports this folder's VM forwards to the same port on this machine's localhost, " +
|
|
"or change them at once to all, none or a LIST of ports and ranges like 5173,8000-8010. " +
|
|
"With -g, show or change what new VMs forward instead.",
|
|
Example: examples(
|
|
"devvm ports", "show what the VM forwards",
|
|
"devvm ports 5173,8000-8010", "forward only these ports and ranges",
|
|
"devvm ports none", "forward nothing",
|
|
"devvm ports -g none", "make new VMs forward nothing",
|
|
),
|
|
Args: showsOnly(cobra.MatchAll(maxArgs(1), func(_ *cobra.Command, args []string) error {
|
|
if len(args) == 0 {
|
|
return nil
|
|
}
|
|
_, err := settings.ParsePorts(args[0])
|
|
return err
|
|
})),
|
|
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
|
if len(args) > 0 {
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
}
|
|
return []cobra.Completion{settings.PortsAll, settings.PortsNone}, cobra.ShellCompDirectiveNoFileComp
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
var c *vm.PortsChange
|
|
if len(args) == 1 {
|
|
c = &vm.PortsChange{List: args[0]}
|
|
}
|
|
return run(cmd, c)
|
|
},
|
|
}
|
|
cmd.Flags().BoolVarP(&global, "global", "g", false, "show or change what new VMs forward instead")
|
|
jsonFlag(cmd, &asJSON)
|
|
sub := func(op, short, long, example string) *cobra.Command {
|
|
c := &cobra.Command{
|
|
Use: op + " LIST",
|
|
Annotations: about{docs: "using/ports-size#ports", noJSON: "it changes a setting"}.annotations(),
|
|
Short: short,
|
|
Long: long,
|
|
Example: example,
|
|
Args: cobra.MatchAll(exactArgs(1), func(_ *cobra.Command, args []string) error {
|
|
_, err := vm.ParsePortList(args[0])
|
|
return err
|
|
}),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return run(cmd, &vm.PortsChange{Op: op, List: args[0]})
|
|
},
|
|
}
|
|
c.Flags().BoolVarP(&global, "global", "g", false, "change what new VMs forward instead")
|
|
return addFolderFlags(c, a)
|
|
}
|
|
cmd.AddCommand(
|
|
sub("add", "Forward more ports from this folder's VM", "Add LIST's ports and ranges, such as 5173,8000-8010, "+
|
|
"to those this folder's VM forwards, or with -g to those new VMs forward. A VM that forwards "+
|
|
"every port keeps doing so.",
|
|
examples(
|
|
"devvm ports add 5432", "forward one more port",
|
|
"devvm ports add 8000-8010", "forward a range",
|
|
)),
|
|
sub("rm", "Stop forwarding some ports from this folder's VM", "Remove LIST's ports and ranges, each "+
|
|
"as listed, from those this folder's VM forwards, or with -g from those new VMs forward. Removing "+
|
|
"the last leaves none.",
|
|
examples(
|
|
"devvm ports rm 5173", "stop forwarding one port",
|
|
"devvm ports rm -g 5173", "stop new VMs forwarding it",
|
|
)),
|
|
)
|
|
return addFolderFlags(cmd, a)
|
|
}
|