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]>
126 lines
4.6 KiB
Go
126 lines
4.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/ui"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// newGroupCmd is devvm group: the group this folder's VM is in, shown,
|
|
// with join and leave.
|
|
func newGroupCmd(a *app) *cobra.Command {
|
|
u := a.u
|
|
var asJSON bool
|
|
cmd := &cobra.Command{
|
|
Use: "group",
|
|
Annotations: about{docs: "using/links#groups", group: groupLinks}.annotations(),
|
|
Short: "Show the group this folder's VM is in",
|
|
Long: "Show the group this folder's VM is in, with each of its VMs, the name the others reach it by, its folder and the ports it publishes. " +
|
|
"The VMs in a group reach each other's published ports by name, through links the group makes in each of them.",
|
|
Example: examples(
|
|
"devvm group", "show this folder's VM's group",
|
|
"devvm group join web", "join the group web, made when its first VM joins",
|
|
"devvm group leave", "leave it, removing the links the group made",
|
|
),
|
|
Args: noArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
var g vm.GroupShown
|
|
if err := a.env(func(env *vm.Env) error {
|
|
var err error
|
|
g, err = vm.Groups{Env: env}.Show(cmd.Context())
|
|
return err
|
|
}, msg.GroupFailed); err != nil {
|
|
return err
|
|
}
|
|
if asJSON {
|
|
return printJSON(u, groupJSON(g))
|
|
}
|
|
if g.Group == "" {
|
|
u.Info(msg.GroupNone(g.VM))
|
|
return nil
|
|
}
|
|
return writeGroup(u.Table(), g)
|
|
},
|
|
}
|
|
jsonFlag(cmd, &asJSON)
|
|
cmd.AddCommand(newGroupJoinCmd(a), newGroupLeaveCmd(a))
|
|
return addFolderFlags(cmd, a)
|
|
}
|
|
|
|
// groupOutput is devvm group --json: the VM's group, "" for none, and its
|
|
// members, each with the name the others reach it by, its folder and its
|
|
// published ports (none for none).
|
|
type groupOutput struct {
|
|
Group string `json:"group"`
|
|
Members []groupMemberJSON `json:"members"`
|
|
}
|
|
|
|
type groupMemberJSON struct {
|
|
Name string `json:"name"`
|
|
VM vmname.VM `json:"vm"`
|
|
Folder string `json:"folder"`
|
|
Published string `json:"published"`
|
|
}
|
|
|
|
func groupJSON(g vm.GroupShown) groupOutput {
|
|
out := groupOutput{Group: g.Group, Members: make([]groupMemberJSON, 0, len(g.Members))}
|
|
for _, m := range g.Members {
|
|
out.Members = append(out.Members, groupMemberJSON{Name: m.Name, VM: m.VM, Folder: m.Folder, Published: m.Published})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// writeGroup writes the group's members as a table, a header first.
|
|
func writeGroup(tab *ui.Table, g vm.GroupShown) error {
|
|
tab.Header("NAME", "VM", "FOLDER", "PUBLISHED")
|
|
for _, m := range g.Members {
|
|
tab.Row(cell(m.Name), cell(m.VM.String()), cell(m.Folder), cell(m.Published))
|
|
}
|
|
return tab.Flush()
|
|
}
|
|
|
|
// groupNameArg checks devvm group join's NAME.
|
|
func groupNameArg(_ *cobra.Command, args []string) error { return vm.CheckGroupName(args[0]) }
|
|
|
|
func newGroupJoinCmd(a *app) *cobra.Command {
|
|
var as string
|
|
cmd := &cobra.Command{
|
|
Use: "join NAME",
|
|
Annotations: about{docs: "using/links#joining-a-group", noJSON: "it changes a setting"}.annotations(),
|
|
Short: "Put this folder's VM in a group of VMs",
|
|
Long: "Put this folder's VM in the group NAME, which its first VM makes. " +
|
|
"Each VM in the group then reaches the ports the others publish with devvm link publish, " +
|
|
"by each one's folder name or the name it joined with, as in db:5432 and db.devvm.internal:5432.",
|
|
Example: examples(
|
|
"devvm group join web", "join the group web",
|
|
"devvm group join web --as api2", "join it as api2 instead of this folder's name",
|
|
),
|
|
Args: cobra.MatchAll(exactArgs(1), groupNameArg),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return a.env(func(env *vm.Env) error { return vm.Groups{Env: env}.Join(cmd.Context(), args[0], as) }, msg.GroupFailed)
|
|
},
|
|
}
|
|
cmd.Flags().StringVar(&as, "as", "", "the `NAME` the group's other VMs reach this VM by (default: its folder name)")
|
|
return addFolderFlags(cmd, a)
|
|
}
|
|
|
|
func newGroupLeaveCmd(a *app) *cobra.Command {
|
|
return addFolderFlags(&cobra.Command{
|
|
Use: "leave",
|
|
Annotations: about{docs: "using/links#leaving-a-group", noJSON: "it changes a setting"}.annotations(),
|
|
Short: "Take this folder's VM out of its group",
|
|
Long: "Take this folder's VM out of its group, and remove the links the group made, in this VM and in the group's other VMs, naming them. " +
|
|
"The links you made with devvm link add stay, and the group goes with its last VM.",
|
|
Example: examples(
|
|
"devvm group leave", "leave this folder's VM's group",
|
|
),
|
|
Args: noArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
return a.env(func(env *vm.Env) error { return vm.Groups{Env: env}.Leave(cmd.Context()) }, msg.GroupFailed)
|
|
},
|
|
}, a)
|
|
}
|