mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
Any work that can take noticeable time now runs under the UI's status line: a Reporter's Status with a label message, or a LoggedCmd step. The UI: - status lines are a stack: a Status or LoggedCmd inside another shows its own line, then the outer one is drawn again, where before it ended the outer one for good - a message (Info, Warn, Note, Say, data on Stdout/Stderr) clears the line, prints and draws it again below; an unended line holds it - a question (Ask, Confirm, Answer, Secret), the picker and ui.Terminal hold it until they're done - no status line starts once the run is interrupted, as nothing would clear it - the run log's path is read without the log's lock, as the status line reads it under outMu Covered, by site and label: - finder.target: finding_vm; Lima's limactl --version: checking_backend - bringUp's checks (seeds, instance, recordFolder, hostChecks, ssh, requirements) and a running VM's go-on (proxy, pins, mount check): checking_vm; startOnce after the agent: starting; the self-check's probes: checking_network; pinHostKeys: reading_host_keys - setUpVM (sync, ssh config, links, clone seed): setting_up_vm; each devcontainer command: running_script; makeMachine: creating; buildOnce: checking_image; paste's count and rm: reading_pasted - volumeOf's lookup (diskutil), for launch, adopt, move, ls and rm: looking_up_volume - vm.ListAll (devvm ls): reading_vms; devvm doctor's checks: doctor_checking - devvm agent import: agent_import_reading (walk, links, opencode), agent_store_saving (copy, marketplace git checks, tmutil); devvm agent save: agent_save_reading, agent_save_comparing, agent_store_saving - devvm stop (from its pins check on): checking_vm; a delete, an orphan's teardown and Lima's DeleteImage: deleting; devvm git-protect and clone on/off's .git work (leftBehind, mountFolder, unmountFolder): git_protecting; git-protect's re-read: finding_vm - devvm clone add: clone_reading, clone_adding; clone fetch: clone_fetching; planClone: clone_planning; the clone check before rm or a remake: clone_checking; history export and import: history_exporting, history_importing; a remake's home: saving_home, restoring_home - devvm upgrade's install: upgrade_installing; the shared tools' step back and .NET's downloads: resolving_tools; gc's caches: reading_space; the retention settings (daily and in gc): retaining Work that takes the terminal stays off: the shell, ssh-add's passphrase (checkAgent, Agent.Ensure, reloadAgent) and an agent's login. Tests and guard: - msg.Fake: OnStatus and Showing; its LoggedCmd shows its step - backendtest.Fake: OnCall - TestLaunchStatusLines: every backend call of a new, running and stopped VM's launch runs under a status line but the shell's and Running's (pid files) - TestSSHAddOffStatusLine: ssh-add, on the user's agent or devvm's, never runs under one; chflagsCalls fails a .git lock or unlock with none - the structure test's status-lines rule (statusSites, statusFree) - TestStatusLinesNest, TestMessagesClearTheStatusLine, TestQuestionsHoldTheStatusLine, TestSecretAndPickerHoldTheStatusLine, TestTerminalHoldsTheStatusLine, TestNoStatusLineOnceInterrupted, TestFakeStatus, TestAgentImportStatus, TestAgentSaveStatus, TestDoctorReport, TestProblems, TestList, TestListLooksUpVolumesOnce, TestVolumeOfCache, TestWithSDKDownloads, TestRetainStatus, TestSetCloneOn, TestTeardownLeavesNothing The rule is written in the cli skill (rule 14, checklist, review), the codebase-standards skill (Show what devvm waits for, its checklist line and rules.md's row), Contributing's CLI page (Status lines), its tests and structure pages, and the troubleshooting page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
170 lines
5.0 KiB
Go
170 lines
5.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/gitprotect"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/netpolicy"
|
|
"github.com/devvmhq/devvm/internal/ui"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// newLsCmd is devvm ls.
|
|
func newLsCmd(a *app) *cobra.Command {
|
|
u := a.u
|
|
var asJSON bool
|
|
cmd := &cobra.Command{
|
|
Use: "ls",
|
|
Annotations: about{docs: "using/vms#listing-vms", group: groupVM}.annotations(),
|
|
Short: "List the project VMs",
|
|
Long: "List the project VMs, sorted by name. " +
|
|
"Each row has the VM's status, image, .git protection, network tier, links to your other VMs and folder.",
|
|
Example: examples(
|
|
"devvm ls", "list every project VM",
|
|
"devvm ls --json", "the same list as JSON, for scripts",
|
|
),
|
|
Args: noArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
ls, err := listVMs(cmd, a)
|
|
if err != nil {
|
|
return failed(err, msg.LsFailed)
|
|
}
|
|
if asJSON {
|
|
return printJSON(u, lsJSON(ls))
|
|
}
|
|
return writeList(u.Table(), ls)
|
|
},
|
|
}
|
|
jsonFlag(cmd, &asJSON)
|
|
return cmd
|
|
}
|
|
|
|
// lsOutput is devvm ls --json: the project VMs, by name. What a VM's
|
|
// state doesn't have is "".
|
|
type lsOutput struct {
|
|
VMs []vmJSON `json:"vms"`
|
|
}
|
|
|
|
type vmJSON struct {
|
|
Name vmname.VM `json:"name"`
|
|
Status string `json:"status"`
|
|
Image string `json:"image"`
|
|
// Tools is its shared tools: node, claude.
|
|
Tools []string `json:"tools"`
|
|
From string `json:"from,omitempty"`
|
|
Git string `json:"git"`
|
|
Net string `json:"net"`
|
|
// LearningSince is since when it learns the hosts it reaches, on the
|
|
// internet tier, when it does.
|
|
LearningSince string `json:"learning_since,omitempty"`
|
|
Backend string `json:"backend"`
|
|
Folder string `json:"folder"`
|
|
// FolderState is what's at Folder's path: there, gone, or
|
|
// not_connected (vm.FolderState); devvm rm VM deletes a VM whose
|
|
// folder is gone or not connected.
|
|
FolderState string `json:"folder_state"`
|
|
// Links is how many links the VM has to other VMs' ports.
|
|
Links int `json:"links"`
|
|
}
|
|
|
|
func lsJSON(ls []vm.Listed) lsOutput {
|
|
out := lsOutput{VMs: make([]vmJSON, 0, len(ls))}
|
|
for _, l := range ls {
|
|
out.VMs = append(out.VMs, vmJSON{
|
|
Name: l.Name, Status: l.Status, Image: l.Image, Tools: append([]string{}, l.Tools...), From: l.From, Git: l.Git, Net: l.Net,
|
|
LearningSince: jsonTime(l.Learning), Backend: l.Backend, Folder: l.Folder, FolderState: string(l.FolderState), Links: l.Links,
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// listVMs lists the VMs every backend has, warning of each state file it
|
|
// can't read.
|
|
func listVMs(cmd *cobra.Command, a *app) ([]vm.Listed, error) {
|
|
env, err := a.vmEnv()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ls, unreadable, err := vm.ListAll(cmd.Context(), env)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, k := range unreadable {
|
|
a.u.Warn(msg.VMStateUnreadable(k.Path).Because(k.Err))
|
|
}
|
|
return ls, nil
|
|
}
|
|
|
|
// writeList writes the VMs as a table, a header first; a value a VM lacks
|
|
// is -, and so are TOOLS for a VM without shared tools and LINKS for one
|
|
// without links. A VM's status, .git protection and network tier are
|
|
// colored by what they mean, and a folder that's gone or not connected is
|
|
// yellow, with (gone) or (not connected) after it.
|
|
func writeList(tab *ui.Table, ls []vm.Listed) error {
|
|
tab.Header("NAME", "STATUS", "IMAGE", "TOOLS", "GIT", "NET", "LINKS", "FOLDER")
|
|
for _, l := range ls {
|
|
image := l.Image
|
|
if l.From != "" {
|
|
image = tab.Text(msg.ImageFrom(l.From, l.Image))
|
|
}
|
|
links := ""
|
|
if l.Links > 0 {
|
|
links = strconv.Itoa(l.Links)
|
|
}
|
|
folder := cell(l.Folder)
|
|
switch l.FolderState {
|
|
case vm.FolderGone:
|
|
folder = ui.Cell{Text: l.Folder + " (gone)", Role: ui.Attention}
|
|
case vm.FolderNotConnected:
|
|
folder = ui.Cell{Text: l.Folder + " (not connected)", Role: ui.Attention}
|
|
case vm.FolderThere:
|
|
}
|
|
net := cellAs(l.Net, netModes)
|
|
if shown := netpolicy.Shown(l.Net, !l.Learning.IsZero()); shown != l.Net {
|
|
net = ui.Cell{Text: shown, Role: ui.Attention}
|
|
}
|
|
tab.Row(cell(l.Name.String()), cellAs(l.Status, vmStatuses), cell(image), cell(strings.Join(l.Tools, ",")), cellAs(l.Git, gitModes),
|
|
net, cell(links), folder)
|
|
}
|
|
return tab.Flush()
|
|
}
|
|
|
|
// vmStatuses, gitModes and netModes color devvm ls's STATUS, GIT and NET
|
|
// columns: a status other than running or stopped (Broken) is red, and so
|
|
// is a tier that isn't enforced; a confined tier is green.
|
|
func vmStatuses(s string) ui.Role {
|
|
switch backend.Status(s) {
|
|
case backend.StatusRunning:
|
|
return ui.Good
|
|
case backend.StatusStopped:
|
|
return ui.Attention
|
|
case "":
|
|
return ui.Plain
|
|
}
|
|
return ui.Bad
|
|
}
|
|
|
|
func gitModes(s string) ui.Role {
|
|
switch s {
|
|
case gitprotect.On, gitprotect.Clone:
|
|
return ui.Good
|
|
}
|
|
return ui.Plain
|
|
}
|
|
|
|
func netModes(s string) ui.Role {
|
|
switch t := netpolicy.Tier(s); {
|
|
case s == netpolicy.CheckUnenforced:
|
|
return ui.Bad
|
|
case t.Valid() && t.Confined():
|
|
return ui.Good
|
|
}
|
|
return ui.Plain
|
|
}
|