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]>
104 lines
3.9 KiB
Go
104 lines
3.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// newPasteCmd is devvm paste: the clipboard's image, PDF or copied file
|
|
// into this folder's VM, its path copied back, and devvm paste rm.
|
|
func newPasteCmd(a *app) *cobra.Command {
|
|
u := a.u
|
|
var asJSON, sendFile bool
|
|
c := &cobra.Command{
|
|
Use: "paste",
|
|
Annotations: about{docs: "using/coding-agents#pasting-images", group: groupVM}.annotations(),
|
|
Short: "Send the clipboard's image, PDF or copied file to this folder's VM",
|
|
Long: "Put the image, the PDF, or the one copied file, that's on this machine's clipboard into this folder's VM, " +
|
|
"which must be running, as a new file in its paste folder, and copy that file's path to the clipboard. " +
|
|
"A copied file is sent after asking, showing its path on this machine, " +
|
|
"and only a file in this folder, which the VM already reads, is sent without asking, with --send-file. " +
|
|
"Then paste the path into the agent with your terminal's paste shortcut.",
|
|
Example: examples(
|
|
"devvm paste", "send the copied image, and copy its path in the VM",
|
|
"devvm paste --json", "the same, printing the path and what was sent as JSON",
|
|
"devvm paste --send-file", "send a copied file in this folder without asking, as a script must",
|
|
"devvm paste rm", "delete the files devvm paste left in the VM",
|
|
),
|
|
Args: noArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
var sent vm.PasteSent
|
|
err := a.env(func(env *vm.Env) error {
|
|
var err error
|
|
sent, err = vm.Paste{Env: env}.Send(cmd.Context(), sendFile)
|
|
return err
|
|
}, msg.PasteFailed)
|
|
switch {
|
|
case err != nil:
|
|
return err
|
|
case asJSON:
|
|
return printJSON(u, pasteJSON(sent))
|
|
case sent.Path == "": // you said no to sending the file
|
|
return nil
|
|
case !sent.Copied: // the warning said copying failed
|
|
u.Info(msg.PastePut(sent.Path, sent.VM))
|
|
return nil
|
|
}
|
|
u.Info(msg.PasteSent(sent.Path, sent.VM))
|
|
return nil
|
|
},
|
|
}
|
|
jsonFlag(c, &asJSON)
|
|
c.Flags().BoolVar(&sendFile, "send-file", false, "send a copied file in this folder without asking")
|
|
addFolderFlags(c, a)
|
|
c.AddCommand(newPasteRmCmd(a))
|
|
return c
|
|
}
|
|
|
|
// pasteOutput is devvm paste --json: whether anything was sent (false
|
|
// when you said no to sending a file), the VM, the file's path in it (""
|
|
// when nothing was sent), its kind (image, pdf or file), an image's or a
|
|
// PDF's type ("" for a file), a copied file's path on this machine (""
|
|
// otherwise), its size in bytes, and whether the path reached the
|
|
// clipboard.
|
|
type pasteOutput struct {
|
|
Sent bool `json:"sent"`
|
|
VM vmname.VM `json:"vm"`
|
|
Path string `json:"path"`
|
|
Kind string `json:"kind"`
|
|
Type string `json:"type"`
|
|
Source string `json:"source"`
|
|
Bytes int64 `json:"bytes"`
|
|
Copied bool `json:"copied"`
|
|
}
|
|
|
|
func pasteJSON(s vm.PasteSent) pasteOutput {
|
|
return pasteOutput{Sent: s.Path != "", VM: s.VM, Path: s.Path, Kind: s.Kind, Type: s.Type, Source: s.Source, Bytes: s.Bytes, Copied: s.Copied}
|
|
}
|
|
|
|
// newPasteRmCmd is devvm paste rm.
|
|
func newPasteRmCmd(a *app) *cobra.Command {
|
|
var force bool
|
|
c := &cobra.Command{
|
|
Use: "rm",
|
|
Annotations: about{docs: "using/coding-agents#pasting-images", noJSON: "it deletes"}.annotations(),
|
|
Short: "Delete the files devvm paste left in this folder's VM",
|
|
Long: "Delete the images and files that devvm paste put in this folder's VM's paste folder, after asking, " +
|
|
"and nothing else in that folder. The VM must be running.",
|
|
Example: examples(
|
|
"devvm paste rm", "delete them after asking",
|
|
"devvm paste rm --force", "delete them without asking, as a script must",
|
|
),
|
|
Args: noArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
return a.env(func(env *vm.Env) error { return vm.Paste{Env: env}.Rm(cmd.Context(), force) }, msg.PasteRmFailed)
|
|
},
|
|
}
|
|
addForceFlag(c.Flags(), &force)
|
|
addFolderFlags(c, a)
|
|
return c
|
|
}
|