mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 16:27:33 -10:00
internal/ui has no package state: ui.UI holds the streams, terminal flags, run log, status line, cleanup stack and interrupt flag, and its functions are methods. ui.Main makes the run's UI on devvm's own streams, owns signals, recovers Die and runs the cleanups; ui.New makes one on other streams. cmd/devvm passes it to cli.Execute, and every cli command gets it through its constructor. Only cmd/devvm and internal/cli import internal/ui (a depguard rule). Each other package defines the small UI interface it needs, which its constructor takes, filling fields tests set: - gitprotect.New, secrets.NewStore, from.NewLookup: a warn func - ubuntu.New: LoggedCmd, Note - netpolicy.NewProxy: Note, Warn, Ask, Confirm, StdinIsTerminal; its log lines drop control characters themselves (oneLine) - sshagent.New: Info, Note, Warn, LogWriter - tools.New: Info, Warn, LogNote, Status, LoggedCmd, AddCleanup - lima.New, smolvm.New: Info, Warn, LoggedCmd (their Runner's Run), WithSteps, AddCleanup, Interrupted; lima LogNote and ubuntu's too - backends.Registry: lima's and smolvm's - vm.NewEnv: all of the above, Say, Stdout, Stderr; Env has Stdout, LogNote, LogWriter, LoggedCmd, AddCleanup, Interrupted fields - build.Builder: Terminal and LogNote fields - sshconfig.New: its caller sets what it prints and asks ui's tests build their own UI over buffers and run in parallel; other packages' tests use small fakes; cli's unit tests a UI over buffers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
43 lines
1.4 KiB
Go
43 lines
1.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/w3cj/devvm/internal/build"
|
|
"github.com/w3cj/devvm/internal/msg"
|
|
"github.com/w3cj/devvm/internal/paths"
|
|
"github.com/w3cj/devvm/internal/ui"
|
|
)
|
|
|
|
// newLayerCmd is devvm layer new NAME.
|
|
func newLayerCmd(u *ui.UI) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "layer",
|
|
Short: "Your own image layers",
|
|
Long: "Your own image layers: scripts in ~/.config/devvm/layers that an image runs on top of base, " +
|
|
"named in images like the shared tools (node+NAME).",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error { return cmd.Help() },
|
|
}
|
|
cmd.AddCommand(&cobra.Command{
|
|
Use: "new NAME",
|
|
Short: "Start a layer of your own",
|
|
Long: "Start a layer: ~/.config/devvm/layers/NAME.sh, a script to edit, and NAME.policy next to it, " +
|
|
"the hosts VMs with the layer can reach with network policy (kept if it exists). NAME is " +
|
|
"lowercase letters, digits and dashes, and not base or a shared tool's. Then devvm build NAME " +
|
|
"builds its image, and devvm node+NAME makes a VM with it.",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
|
dirs, err := paths.FromEnv()
|
|
var script, policy string
|
|
if err == nil {
|
|
script, policy, err = build.NewLayer(dirs, args[0])
|
|
}
|
|
fail(u, err, msg.LayerNewFailed)
|
|
u.Info(msg.LayerCreated(args[0], policy, script))
|
|
return nil
|
|
},
|
|
})
|
|
return cmd
|
|
}
|