Categorygithub.com/brad-jones/goprefix/v2

# Packages

No description provided by the author
No description provided by the author

# README

goprefix

PkgGoDev GoReport GoLang .github/workflows/main.yml semantic-release Conventional Commits KeepAChangelog License

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

Quick Start

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