mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -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]>
54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package cli
|
|
|
|
import (
|
|
"runtime/debug"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/w3cj/devvm/internal/ui"
|
|
)
|
|
|
|
// version is devvm's version. Release builds set it:
|
|
// -ldflags "-X github.com/w3cj/devvm/internal/cli.version=0.1.0".
|
|
var version string
|
|
|
|
// devvmVersion is devvm's version: versionOf the release's.
|
|
func devvmVersion() string { return versionOf(version) }
|
|
|
|
// versionOf is release, else the version Go recorded for the build (a
|
|
// checkout's pseudo-version), without the v; (devel) when there's neither.
|
|
func versionOf(release string) string {
|
|
if release != "" {
|
|
return release
|
|
}
|
|
if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Version != "" {
|
|
return strings.TrimPrefix(bi.Main.Version, "v")
|
|
}
|
|
return "(devel)"
|
|
}
|
|
|
|
// addVersion gives root --version, which prints the version alone. The
|
|
// flag is declared here so Cobra doesn't add -v.
|
|
func addVersion(root *cobra.Command) {
|
|
root.Version = devvmVersion()
|
|
root.SetVersionTemplate("{{.Version}}\n")
|
|
root.Flags().Bool("version", false, "print devvm's version")
|
|
}
|
|
|
|
// newVersionCmd is devvm version, the same as devvm --version. Hidden:
|
|
// --version is the one the help shows.
|
|
func newVersionCmd(u *ui.UI) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "version",
|
|
Short: "Print devvm's version",
|
|
Hidden: true,
|
|
Args: cobra.NoArgs,
|
|
ValidArgsFunction: cobra.NoFileCompletions,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
_, err := u.Stdout().Write([]byte(cmd.Root().Version + "\n"))
|
|
return err
|
|
},
|
|
}
|
|
}
|