A simple library to help prefix streams of text, useful to create "docker-compose" like interfaces.
The prefixer
will read a stream and prefix each line with the given prefix.
Given a string the colorchooser
will return a string colored with a random
16-bit color, given the same string again, it will be returned in the same color.
Looking for v1, see the master branch
go get -u github.com/brad-jones/goprefix/v2/pkg/...
package main
import (
"os"
"os/exec"
"github.com/brad-jones/goprefix/v2/pkg/colorchooser"
"github.com/brad-jones/goprefix/v2/pkg/prefixer"
)
func main() {
p1 := prefixer.New(colorchooser.Sprint("ping 1.1.1.1") + " | ")
cmd1 := exec.Command("ping", "-c", "4", "1.1.1.1")
stdOutPipe1, err := cmd1.StdoutPipe()
if err != nil {
panic(err)
}
defer stdOutPipe1.Close()
stdErrPipe1, err := cmd1.StderrPipe()
if err != nil {
panic(err)
}
defer stdErrPipe1.Close()
if err := cmd1.Start(); err != nil {
panic(err)
}
go func() { p1.ReadFrom(stdOutPipe1).WriteTo(os.Stdout) }()
go func() { p1.ReadFrom(stdErrPipe1).WriteTo(os.Stderr) }()
if err := cmd1.Wait(); err != nil {
panic(err)
}
}
Also see further working examples under: https://github.com/brad-jones/goexec/tree/v2/examples