utils

package
v0.4.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DebugLevel = zerolog.DebugLevel
	InfoLevel  = zerolog.InfoLevel
	WarnLevel  = zerolog.WarnLevel
)

Log levels to be used by main, or other entry points in this project

View Source
const (
	ColorRed           = "\033[0;31m"
	ColorGreen         = "\033[0;32m"
	ColorYellow        = "\033[0;33m"
	ColorBlue          = "\033[0;34m"
	ColorMagenta       = "\033[0;35m"
	ColorCyan          = "\033[0;36m"
	ColorBrightRed     = "\033[1;31m"
	ColorBrightGreen   = "\033[1;32m"
	ColorBrightYellow  = "\033[1;33m"
	ColorBrightBlue    = "\033[1;34m"
	ColorBrightMagenta = "\033[1;35m"
	ColorBrightCyan    = "\033[1;36m"

	ColorDarkGrey = "\033[1;30m"
	ColorWhite    = "\033[1;37m"

	ColorReset = "\033[0m"

	ColorOutput  = ColorWhite
	ColorSuccess = ColorBrightCyan
	ColorWarning = ColorBrightYellow
	ColorError   = ColorBrightRed
)

Variables

View Source
var ErrValidationFailed = errors.New("validation error")
View Source
var IsColorOutputSupported bool
View Source
var WithStderrLogger = func(logger zerolog.Logger, level zerolog.Level, cb ...func(*zerolog.Event)) func(*WrappedCmd) error {
	return WithStderrReader(LineLogger(logger, level, cb...))
}
View Source
var WithStderrReader = func(cb func(lines chan []byte)) func(cmd *WrappedCmd) error {
	return func(cmd *WrappedCmd) error {
		stderr, err := cmd.StderrPipe()
		if err != nil {
			return err
		}

		return cmd.startReader(stderr, cb)
	}
}

WithStderrLogger returns a command option which sends stderr to channel in callback

View Source
var WithStdin = func(stdin io.Reader) func(cmd *WrappedCmd) error {
	return func(cmd *WrappedCmd) error {
		cmd.Stdin = stdin
		return nil
	}
}
View Source
var WithStdoutLogger = func(logger zerolog.Logger, level zerolog.Level, cb ...func(*zerolog.Event)) func(*WrappedCmd) error {
	return WithStdoutReader(LineLogger(logger, level, cb...))
}
View Source
var WithStdoutReader = func(cb func(lines chan []byte)) func(cmd *WrappedCmd) error {
	return func(cmd *WrappedCmd) error {
		stdout, err := cmd.StdoutPipe()
		if err != nil {
			return err
		}

		return cmd.startReader(stdout, cb)
	}
}

WithStdoutLogger returns a command option which sends stdout to channel in callback

Functions

func Colorize

func Colorize(color, text string) string

func ColorizeJSON

func ColorizeJSON(data []byte) []byte

func FormatBoolean

func FormatBoolean(b bool) string

func FormatEventSeverity

func FormatEventSeverity(severity string) string

func FormatEventType

func FormatEventType(eventType string) string

func FormatNodeID

func FormatNodeID(nodeID string) string

func FormatTime

func FormatTime(t time.Time) string

func FormatUUID

func FormatUUID(uuid string) string

func GetLogger

func GetLogger(component string) zerolog.Logger

GetLogger returns a new logger instance with the specified component

func IsFileExists

func IsFileExists(name string) bool

func IsSetP

func IsSetP[T comparable](v *T) bool

IsSetP returns true if pointer is not nil and value is not empty

func LineLogger

func LineLogger(logger zerolog.Logger, level zerolog.Level, cb ...func(*zerolog.Event)) func(chan []byte)

func ParseTime

func ParseTime(text string) (time.Time, error)

func RenderTable

func RenderTable(headers []string, populate func(table *tablewriter.Table))

func RenderTableRows

func RenderTableRows(headers []string, nextRow func() []string)

func SetLoggingLevel

func SetLoggingLevel(level zerolog.Level)

SetLoggingLevel should be invoked once by each entry point

func URLSafe

func URLSafe(u *url.URL) *url.URL

func UnescapeUnicodeCharactersInJSON

func UnescapeUnicodeCharactersInJSON(_jsonRaw json.RawMessage) (json.RawMessage, error)

func UserError

func UserError(msg string, format ...interface{})

UserError prints a colorized error message to stderr, and terminates with a non-zero exit code

func UserNote

func UserNote(msg string, format ...interface{})

UserNote prints a colorized info message to stderr. Use this function to output a neutral or positive message to the user.

func UserOutput

func UserOutput(msg string, format ...interface{})

UserOutput prints text to stdout. Use this function to output a command's return value.

func UserOutputJSON

func UserOutputJSON(data []byte)

UserOutputJSON is like UserOutput, but for JSON

func UserWarning

func UserWarning(msg string, format ...interface{})

UserWarning prints a colorized warning message to stderr

Types

type TableRenderer

type TableRenderer struct {
	Headers  []string
	Populate func(table *tablewriter.Table)
}

func (*TableRenderer) Render

func (tr *TableRenderer) Render()

type TableRow

type TableRow struct {
	Cells []string
	// contains filtered or unexported fields
}

func NewTableRow

func NewTableRow(numCells int) *TableRow

func (*TableRow) Append

func (row *TableRow) Append(cells ...string)

type WrappedCmd

type WrappedCmd struct {
	*exec.Cmd
	// contains filtered or unexported fields
}

func ExecCommand

func ExecCommand(ctx context.Context, name string, args []string, opts ...commandOpt) (*WrappedCmd, error)

func (*WrappedCmd) Wait

func (c *WrappedCmd) Wait() error

type WriterFunc

type WriterFunc struct {
	*sync.WaitGroup

	io.Writer
	io.Closer
	ErrCloser interface {
		CloseWithError(err error) error
	}
}

func NewWritterFunc

func NewWritterFunc(readers ...func([]byte)) WriterFunc

NewWritterFunc returns a helper which runs callback for each line, written to WriterFunc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL