treewide: introduce osbase package and move things around
All except localregistry moved from metropolis/pkg to osbase,
localregistry moved to metropolis/test as its only used there anyway.
Change-Id: If1a4bf377364bef0ac23169e1b90379c71b06d72
Reviewed-on: https://review.monogon.dev/c/monogon/+/3079
Tested-by: Jenkins CI
Reviewed-by: Serge Bazanski <serge@monogon.tech>
diff --git a/metropolis/test/launch/prefixed_stdio.go b/metropolis/test/launch/prefixed_stdio.go
new file mode 100644
index 0000000..c851c44
--- /dev/null
+++ b/metropolis/test/launch/prefixed_stdio.go
@@ -0,0 +1,40 @@
+package launch
+
+import (
+ "fmt"
+ "io"
+ "strings"
+
+ "source.monogon.dev/osbase/logbuffer"
+)
+
+// prefixedStdio is a io.ReadWriter which splits written bytes into lines,
+// prefixes them with some known prefix, and spits them to os.Stdout.
+//
+// io.Reader is implemented for compatibility with code which expects an
+// io.ReadWriter, but always returns EOF.
+type prefixedStdio struct {
+ *logbuffer.LineBuffer
+}
+
+// newPrefixedStdio returns a prefixedStdio that prefixes all lines with <num>|,
+// used to distinguish different VMs used within the launch codebase.
+func newPrefixedStdio(num int) prefixedStdio {
+ return prefixedStdio{
+ logbuffer.NewLineBuffer(1024, func(l *logbuffer.Line) {
+ s := strings.TrimRight(l.String(), " \t\n\r")
+ s = strings.TrimLeft(s, "\n\r\t")
+ // TODO(q3k): don't just skip lines containing escape sequences, strip the
+ // sequences out. Or stop parsing qemu logs and instead dial log endpoint in
+ // spawned nodes.
+ if strings.Contains(s, "\u001b") {
+ return
+ }
+ fmt.Printf("%02d| %s\n", num, s)
+ }),
+ }
+}
+
+func (p prefixedStdio) Read(_ []byte) (int, error) {
+ return 0, io.EOF
+}