mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-05 20:09:32 -10:00
A group's links are links in each member's state, marked as the group's (a sixth field, ,group), so the link session, the proxy's list and every connection check treat them as any link. Membership is each member's own state (group, group_as), so a VM made again is in none. - internal/vm/groups.go: Groups (Show, Join, Leave), groupLinks (pure: the user's links kept and taking precedence, a link per published port of each other member, one address per member kept across changes), sync/syncVM/change (records, names what each member gains and loses, applies to running members), clashes warned. - All or none: join refused by network policy (group_policy_on/off); devvm network on/off refused for a member (group_policy_mixed). - One group at a time (group_other); names unique in a group (group_name_taken, group_name_host); a member whose hostname is another's name gets no links to it (group_link_name_host). - CheckLink refuses a group's link once the two aren't in one group (ungrouped); leave removes the leaver's own group links first; join again works every member's links out again; link rm refuses a group's link that still applies (link_from_group). - Publish/unpublish, link add/rm and devvm rm reach the group's links. - Docs: using/links.md Groups, the Commands page, safety/network.md, internals/ports.md, the design note's Groups section and threat check; the CLI reference. - Tests: groups_test.go, testdata/script/group.txtar, JSON shape; e2e: three VMs in a group and the all-or-none refusals in the link section on every backend. Co-Authored-By: Claude Opus 5.5 <[email protected]>
134 lines
4.6 KiB
Go
134 lines
4.6 KiB
Go
// Package cli holds devvm's commands.
|
|
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/w3cj/devvm/internal/msg"
|
|
"github.com/w3cj/devvm/internal/paths"
|
|
"github.com/w3cj/devvm/internal/state"
|
|
"github.com/w3cj/devvm/internal/ui"
|
|
)
|
|
|
|
// NewRoot returns the devvm command with every subcommand registered, each
|
|
// printing and asking through u. devvm itself is up: a shell in this
|
|
// folder's VM.
|
|
func NewRoot(u *ui.UI) *cobra.Command {
|
|
uf := new(upFlags)
|
|
fl := new(ui.Flags)
|
|
root := &cobra.Command{
|
|
Use: "devvm [IMAGE]",
|
|
Short: "Open a shell in this folder's VM, creating it first",
|
|
Long: "Open a shell in this folder's VM, at the same path, and create the VM first when the folder has none. " +
|
|
"A new VM gets IMAGE, such as node+claude, or else the toolchains the folder's files call for with any agents IMAGE names, " +
|
|
"and devvm builds the image first if it isn't built. " +
|
|
"The VM mounts only this folder read-write, and with --clone it gets its own clone of the repo instead.",
|
|
Example: examples(
|
|
"devvm", "open this folder's VM, creating it with the toolchains the folder needs",
|
|
"devvm node+claude", "create the VM with node and Claude Code",
|
|
"devvm --clone", "create the VM with its own clone of this repo",
|
|
"devvm ls", "list every VM",
|
|
),
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
|
u.SetFlags(*fl)
|
|
if wantsJSON(cmd) {
|
|
u.DataOnly() // stdout is the JSON's alone
|
|
}
|
|
makePrivate(u)
|
|
if !skipsRetention(cmd) {
|
|
autoRetain(cmd.Context(), u)
|
|
}
|
|
},
|
|
Args: cobra.MatchAll(maxArgs(1), func(cmd *cobra.Command, args []string) error {
|
|
if len(args) == 0 {
|
|
return nil
|
|
}
|
|
return checkWord(cmd, args[0])
|
|
}),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return up(cmd.Context(), u, uf, args)
|
|
},
|
|
SuggestionsMinimumDistance: suggestDistance,
|
|
}
|
|
root.SetOut(u.Stdout())
|
|
root.PersistentFlags().BoolVarP(&fl.Verbose, "verbose", "v", false, "show the output of the commands devvm runs instead of a status line")
|
|
root.PersistentFlags().BoolVar(&fl.NoInput, "no-input", false, "never ask, and fail where devvm would ask, naming the flag that answers it")
|
|
addUpFlags(root, uf)
|
|
addVersion(root)
|
|
root.AddCommand(newBuildCmd(u), newRebuildCmd(u), newImagesCmd(u), newGCCmd(u), newSpaceCmd(u), newLayerCmd(u), newSetupCmd(u), newMsgCmd(u),
|
|
newRestartCmd(u), newStopCmd(u), newToolsCmd(u), newAdoptCmd(u), newHistoryCmd(u), newRmCmd(u), newLsCmd(u), newNetworkCmd(u), newHostCmd(u), newCodeCmd(u), newDesktopCmd(u), newSSHAgentCmd(u), newGitProtectCmd(u), newPortsCmd(u), newSizeCmd(u), newDoctorCmd(u), newLimactlCmd(u), newQEMUCmd(u), newProxyCmd(u), newLimaPortsCmd(u), newSmolvmPortsCmd(u), newDockerPortsCmd(u), newDockerConnectCmd(u), newRefreshToolsCmd(u), newSecretCmd(u),
|
|
newLinkCmd(u), newGroupCmd(u), newLinksCmd(u), newRetentionCmd(u), newVersionCmd(u), newCompletionCmd(u))
|
|
addCompletions(root, u)
|
|
checkCommands(root)
|
|
addHelp(root)
|
|
addGroups(root)
|
|
addDocs(root)
|
|
return root
|
|
}
|
|
|
|
// makePrivate makes devvm's data and settings folders the user's alone on
|
|
// every run, whatever mode they had. Without a HOME there are no folders,
|
|
// and the command says so itself.
|
|
func makePrivate(u *ui.UI) {
|
|
d, err := paths.FromEnv()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if err := state.MakePrivate(d); err != nil {
|
|
u.Warn(msg.FoldersNotPrivate(d.Config, d.Data).Because(err))
|
|
}
|
|
}
|
|
|
|
// Execute runs devvm with args on u and returns its exit code: 0, 1 for a
|
|
// failure, usageCode for a usage error (a bad argument or flag, an
|
|
// unknown command), which is followed by usage_help, naming the
|
|
// command's own --help.
|
|
func Execute(ctx context.Context, u *ui.UI, args []string) int {
|
|
return execute(ctx, u, NewRoot(u), args)
|
|
}
|
|
|
|
// execute runs root with args, as Execute does.
|
|
func execute(ctx context.Context, u *ui.UI, root *cobra.Command, args []string) (code int) {
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
w, ok := p.(wordPanic)
|
|
if !ok {
|
|
panic(p)
|
|
}
|
|
printUsage(u, root, w.err)
|
|
code = usageCode
|
|
}
|
|
}()
|
|
root.SetArgs(args)
|
|
cmd, err := root.ExecuteContextC(ctx)
|
|
if err == nil {
|
|
return 0
|
|
}
|
|
if cmd == nil {
|
|
cmd = root
|
|
}
|
|
if e, ok := errors.AsType[ranError](err); ok {
|
|
m, ok := errors.AsType[msg.M](e.err)
|
|
if !ok {
|
|
m = msg.OutputFailed(e.err.Error()).Because(e.err)
|
|
}
|
|
u.Error(m)
|
|
return 1
|
|
}
|
|
// Anything else is the parser's, before the command ran. A word that's
|
|
// neither a command nor an image is devvm's, whichever command (help)
|
|
// refused it.
|
|
e := usage(err)
|
|
if strings.HasPrefix(e.m.ID(), "no_command_or_image") {
|
|
cmd = root
|
|
}
|
|
printUsage(u, cmd, e)
|
|
return usageCode
|
|
}
|