mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
An export leaves out opencode's worktree folder, as a git repository's config can name a command that git runs, so the uncommitted changes in the worktrees opencode makes there (~/.local/share/opencode/worktree/<project>/<name>, opencode v2.4.1's worktree/strategies.ts) went with the VM's disk on devvm rm, and on a remake with --clean-home, with nothing to say so. The registry gets Worktrees (opencode's worktree/*/*, below its HistoryOwn, TestDefsComplete holding each to a plain pattern there). rm's unsaved-work check, cloneCheck, becomes workCheck: the clone (cloneLost, as before) and the agents' worktrees (agentTrees), one question for both. agentTreesState reads them in the running VM in one command, in its home folder, each * of a pattern sent as *, .[!.]* and ..?* (treeGlobs), so a name that starts with a dot is found: each folder with a .git, its git status and a detached HEAD's commit nothing else has; past 100 it says more. devvm reads at most 8 MiB of the answer, cancelling the command past it, and waits at most treesWait (2 minutes). parseAgentTrees takes the answer as untrusted: whole records of a glob asked for, each path clean, matched by its pattern and once, at most 100, each with one status. Dirty, detached, unreadable (wfail) and more are shown and asked about (delete_anyway or remake_anyway), which --force never answers; refused without a terminal (no_terminal_delete_trees, naming --no-worktree-check, or the clone's no_terminal_delete when it has work too). --no-worktree-check skips the check and warns (trees_check_skipped). A failed, late or hostile answer keeps the VM (trees_check_failed), and so does a VM that doesn't start for it (trees_check_not_started). Rm and rmGone run it, and rmGone refuses a stopped VM with opencode, which starting would make its folder again (rm_gone_stopped_trees, naming --no-export --no-worktree-check); stillGone takes the refusal to give. remaking.checkWork runs it before a remake with --clean-home (remakeTo, Update, SetClone, whose clone check it joins), its messages naming the command as typed (remakeInVM.typed), with --clean-home, which --no-worktree-check needs (needs), so devvm clone off --clean-home's clone check messages name it too. A remake's flag has its own help. The stub guest runs the read in vmhome-<vm>. Tests that fail without the check: TestRmAgentTrees, TestRmVMAgentTrees, TestRemakeAgentTrees, TestUpdateAgentTrees, TestSetCloneAgentTrees and the rm_agent_trees script; TestAgentTreesBounded (an endless answer cancelled, a silent VM given up on, in a synctest bubble); TestAgentTreesState runs the script under sh on real worktrees, a dot-named one included; TestParseAgentTrees holds the answer's checks, TestCloneGit the stub's. TestSetCloneOff names the command with --clean-home. e2e's network section makes a worktree in opencode's folder in the real VM, and devvm rm refuses, then --no-worktree-check deletes it. Docs: coding agents' opencode's worktrees, How it works' opencode's worktrees, deleting a VM, a VM whose folder is gone, images, clone, the overview's remake, Commands, extending and rm's help; the changelog's What to do step 4 names --no-worktree-check for scripts and devvm rm VM of a stopped VM, and opencode's block gets the check; the release page has its section. Co-Authored-By: Claude Opus 5.5 <[email protected]>
145 lines
6.0 KiB
Go
145 lines
6.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/devvmhq/devvm/internal/build"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
)
|
|
|
|
// newLayerCmd is devvm layer: new NAME, add LAYER... and rm LAYER....
|
|
func newLayerCmd(a *app) *cobra.Command {
|
|
u := a.u
|
|
cmd := &cobra.Command{
|
|
Use: "layer",
|
|
Annotations: about{
|
|
docs: "using/customizing#your-own-layers", group: groupImages,
|
|
noJSON: "it prints its help",
|
|
}.annotations(),
|
|
Short: "Make image layers of your own, or change this folder's VM's layers",
|
|
Long: "Make image layers of your own, which are scripts in ~/.config/devvm/layers that an image runs on top of base, or add layers to this folder's VM and take them out. " +
|
|
"An image names them as it names devvm's built-in layers, bottom first, such as docker+NAME, and a new VM gets them with --layers.",
|
|
Example: examples(
|
|
"devvm layer new mailpit", "start a layer named mailpit",
|
|
"devvm layer add docker", "make this folder's VM again with the docker layer added",
|
|
"devvm layer rm docker", "make this folder's VM again without it",
|
|
),
|
|
Args: noArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error { return cmd.Help() },
|
|
}
|
|
cmd.AddCommand(newLayerAddCmd(a), newLayerRmCmd(a))
|
|
cmd.AddCommand(&cobra.Command{
|
|
Use: "new NAME",
|
|
Annotations: about{
|
|
docs: "using/customizing#your-own-layers", group: groupImages,
|
|
noJSON: "it writes a file",
|
|
}.annotations(),
|
|
Short: "Start a layer of your own",
|
|
Long: "Start a layer of your own, ~/.config/devvm/layers/NAME.sh, a script to edit, and NAME.policy next to it, " +
|
|
"the hosts that VMs with the layer can reach on the allowlist tier. " +
|
|
"NAME is lowercase letters, digits and dashes, and not base or a built-in layer's or shared tool's name.",
|
|
Example: examples(
|
|
"devvm layer new mailpit", "start the mailpit layer, then edit its script",
|
|
"devvm build mailpit", "build its image",
|
|
"devvm --layers mailpit", "create a VM with the layer",
|
|
),
|
|
Args: cobra.MatchAll(exactArgs(1), func(_ *cobra.Command, args []string) error {
|
|
return build.CheckLayerName(args[0])
|
|
}),
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
|
dirs, err := a.dirs()
|
|
var script, policy string
|
|
if err == nil {
|
|
script, policy, err = build.NewLayer(dirs, args[0])
|
|
}
|
|
if err != nil {
|
|
return failed(err, msg.LayerNewFailed)
|
|
}
|
|
u.Info(msg.LayerCreated(args[0], policy, script))
|
|
return nil
|
|
},
|
|
})
|
|
return cmd
|
|
}
|
|
|
|
// newLayerAddCmd is devvm layer add LAYER... [--force] [--no-export]
|
|
// [--clean-home [--no-worktree-check]].
|
|
func newLayerAddCmd(a *app) *cobra.Command {
|
|
var o vm.RemakeOptions
|
|
cmd := &cobra.Command{
|
|
Use: "add LAYER...",
|
|
Annotations: about{
|
|
docs: "using/images#adding-layers-to-a-vm", group: groupChange,
|
|
noJSON: "it makes a VM again, then opens a shell",
|
|
}.annotations(),
|
|
Short: "Make this folder's VM again with more layers, then open a shell in it",
|
|
Long: "Make this folder's VM again from an image with each LAYER added on top, in the order given, then open a shell in it, after asking in a terminal without --no-input. " +
|
|
"devvm keeps its home folder, the project folder and its settings, and everything else on its disk is lost. " +
|
|
"When its image has an agent, devvm first exports the agents' history, unless you give --no-export.",
|
|
Example: examples(
|
|
"devvm layer add desktop", "add the desktop layer to the VM, after asking",
|
|
"devvm layer add docker nix", "add docker, then nix on top of it",
|
|
"devvm layer add desktop --force", "add it without asking, as a script must",
|
|
"devvm layer add docker --clean-home", "add docker, and start from the image's home folder",
|
|
),
|
|
Args: cobra.MatchAll(minArgs(1), func(_ *cobra.Command, args []string) error {
|
|
dirs, err := a.dirs()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return vm.CheckLayers(args, (&build.Env{Dirs: dirs}).Layers()[1:])
|
|
}),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
command := "devvm layer add " + strings.Join(args, " ")
|
|
return a.env(func(env *vm.Env) error {
|
|
return vm.Lifecycle{Env: env}.AddLayers(cmd.Context(), command, args, o)
|
|
}, msg.RemakeFailed)
|
|
},
|
|
ValidArgsFunction: completeLayerNames(a),
|
|
}
|
|
addRemakeFlags(cmd, &o)
|
|
return addFolderFlags(cmd, a)
|
|
}
|
|
|
|
// newLayerRmCmd is devvm layer rm LAYER... [--force] [--no-export]
|
|
// [--clean-home [--no-worktree-check]].
|
|
func newLayerRmCmd(a *app) *cobra.Command {
|
|
var o vm.RemakeOptions
|
|
cmd := &cobra.Command{
|
|
Use: "rm LAYER...",
|
|
Annotations: about{
|
|
docs: "using/images#taking-layers-out-of-a-vm", group: groupChange,
|
|
noJSON: "it makes a VM again, then opens a shell",
|
|
}.annotations(),
|
|
Short: "Make this folder's VM again without some of its layers, then open a shell in it",
|
|
Long: "Make this folder's VM again from an image without each LAYER, its other layers kept in their order, then open a shell in it, after asking in a terminal without --no-input. " +
|
|
"devvm keeps its home folder, the project folder and its settings, and everything else on its disk is lost. " +
|
|
"When its image has an agent, devvm first exports the agents' history, unless you give --no-export.",
|
|
Example: examples(
|
|
"devvm layer rm desktop", "take the desktop layer out of the VM, after asking",
|
|
"devvm layer rm docker nix", "take out docker and nix",
|
|
"devvm layer rm desktop --force", "take it out without asking, as a script must",
|
|
"devvm layer rm docker --clean-home", "take out docker, and start from the image's home folder",
|
|
),
|
|
Args: cobra.MatchAll(minArgs(1), func(_ *cobra.Command, args []string) error {
|
|
dirs, err := a.dirs()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return vm.CheckLayerNames(args, (&build.Env{Dirs: dirs}).Layers()[1:])
|
|
}),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
command := "devvm layer rm " + strings.Join(args, " ")
|
|
return a.env(func(env *vm.Env) error {
|
|
return vm.Lifecycle{Env: env}.RemoveLayers(cmd.Context(), command, args, o)
|
|
}, msg.RemakeFailed)
|
|
},
|
|
ValidArgsFunction: completeLayerNames(a),
|
|
}
|
|
addRemakeFlags(cmd, &o)
|
|
return addFolderFlags(cmd, a)
|
|
}
|