mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
msg.Reporter is what every package below cli prints and asks through: Info, Say, Note, Warn, Status, Ask, Confirm, Answer, the terminal checks and CanPick, the run log (LogNote, LogWriter, LoggedCmd, WithSteps), Stdout, Stderr, AddCleanup and Interrupted, under the UI's signatures. *ui.UI implements it (a compile-time check in ui), and msg.Fake is the tests': quiet as its zero value, its On funcs and fields record and answer. The six UI interfaces (tools, netpolicy, lima, registry, sshagent, vm) are gone, and the copied output and prompt func fields of tools.Env, tools.Making, gitprotect.Protector, secrets.Store, netpolicy.Proxy, from.Lookup, backend.Review, lima.Lima, sshagent.Agent, vm.Env, vm.SizeChoice, vm.AgentsChoice, vm.LayersChoice, vm.NetworkChoice, vmssh.Forwards, build.Env and sshconfig.Config: each embeds the Reporter. Lima's and the shared tools' Warn, which says where the run log is too, is a method of their own over it; vm's interactive check is a function over it. The pickers (build.Env.Pick, vm.Env's PickFeatures and PickParts, the choices' Pick) and vm.Env.CanAsk stay fields: they carry types of their own or cli's answer. The test UIs of tools, netpolicy, lima, registry, build and vm become msg.Fake. Coverage baselines raised: internal/backend/lima, internal/msg, internal/tools. Co-Authored-By: Claude Opus 5.5 <[email protected]>
75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
package msg
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
// IDOf returns the ID of the message err is or wraps, or "" when it
|
|
// carries none.
|
|
func IDOf(err error) string {
|
|
if m, ok := errors.AsType[M](err); ok {
|
|
return m.ID()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// NamesOf returns the names of the message err is or wraps (no_vm
|
|
// dir=/x), err's own text when it carries none, or "" for nil.
|
|
func NamesOf(err error) string {
|
|
if m, ok := errors.AsType[M](err); ok {
|
|
return m.Names()
|
|
}
|
|
if err == nil {
|
|
return ""
|
|
}
|
|
return err.Error()
|
|
}
|
|
|
|
// A Recorder keeps the messages its Add is given, in order, for a test
|
|
// to check: its Add is a Fake's OnInfo or OnWarn. It's safe
|
|
// for concurrent use; its zero value is empty and ready.
|
|
type Recorder struct {
|
|
mu sync.Mutex
|
|
ms []M
|
|
}
|
|
|
|
// Add keeps m.
|
|
func (r *Recorder) Add(m M) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.ms = append(r.ms, m)
|
|
}
|
|
|
|
// Messages returns the messages kept so far.
|
|
func (r *Recorder) Messages() []M {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
return append([]M(nil), r.ms...)
|
|
}
|
|
|
|
// Names returns each kept message's names, in order: nil for none.
|
|
func (r *Recorder) Names() []string {
|
|
return r.each(M.Names)
|
|
}
|
|
|
|
// IDs returns each kept message's ID, in order: nil for none.
|
|
func (r *Recorder) IDs() []string {
|
|
return r.each(M.ID)
|
|
}
|
|
|
|
// Reset forgets every message kept so far.
|
|
func (r *Recorder) Reset() {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.ms = nil
|
|
}
|
|
|
|
func (r *Recorder) each(f func(M) string) []string {
|
|
var out []string
|
|
for _, m := range r.Messages() {
|
|
out = append(out, f(m))
|
|
}
|
|
return out
|
|
}
|