ui

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package ui is an abstraction on dealing with writing and reading from ui / cli. Do not want to clutter the pkg package with log entries as it makes it difficult to reuse. So ui package abstracts it away and leaves it to those use packages to implement a handler

Concept is based on github.com/mitchellh/cli package, but changed a bit to include headers etc

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ask

func Ask(query string) (string, error)

Ask for user input

func AskSecret

func AskSecret(query string) (string, error)

AskSecret will ask for a secret input, not showing what is typed

func Debug

func Debug(msg string, args ...interface{})

Debug will print a debug message, if debugging is activated

func Error

func Error(msg string, args ...interface{})

Error prints an error message

func Fatal

func Fatal(msg string, args ...interface{})

Fatal prints a fatal message and should always exit!

func Header(msg string)

Header prints a header, can be bold etc. Implementation can decide how a header should be made

func Info

func Info(msg string, args ...interface{})

Info will print an information message. Should not be formatted any special way

func NewLine

func NewLine()

NewLine adds a new line

func Output

func Output(msg string, args ...interface{})

Output writes to output channel

func Separator

func Separator(title string)

Separator between elements

func SetHandler

func SetHandler(hnd Handler)

SetHandler assigns a new handler overriding current (default is cli handler)

func SetLevel

func SetLevel(newLevel Level)

SetLevel changes the current output level

func Warn

func Warn(msg string, args ...interface{})

Warn prints a warning

Types

type CliHandler

type CliHandler struct {
	Reader       io.Reader
	OutputWriter io.Writer
	LogWriter    io.Writer
	// contains filtered or unexported fields
}

CliHandler is the default handler and will write to stdout / stderr

func (*CliHandler) Ask

func (hnd *CliHandler) Ask(query string) (string, error)

Ask user to input

func (*CliHandler) AskSecret

func (hnd *CliHandler) AskSecret(query string) (string, error)

AskSecret as for secret input

func (*CliHandler) Debug

func (hnd *CliHandler) Debug(msg string, args ...interface{})

Debug prints a debug message

func (*CliHandler) Error

func (hnd *CliHandler) Error(msg string, args ...interface{})

Error prints message

func (*CliHandler) Fatal

func (hnd *CliHandler) Fatal(msg string, args ...interface{})

Fatal prints message and makes sure to fail

func (*CliHandler) Header

func (hnd *CliHandler) Header(msg string)

Header does nothing

func (*CliHandler) Info

func (hnd *CliHandler) Info(msg string, args ...interface{})

Info prints message to standard out

func (*CliHandler) NewLine

func (hnd *CliHandler) NewLine()

NewLine creates a new line, except if previous line was a new line. Do not want 2 new lines after another

func (*CliHandler) Output

func (hnd *CliHandler) Output(msg string, args ...interface{})

Output prints to output writer

func (*CliHandler) Separator

func (hnd *CliHandler) Separator(title string)

Separator does nothing

func (*CliHandler) Warn

func (hnd *CliHandler) Warn(msg string, args ...interface{})

Warn prints message

type Handler

type Handler interface {
	// Ask for input from user
	Ask(query string) (string, error)

	// AskSecret will ask for a secret input, not showing what is typed
	AskSecret(query string) (string, error)

	Debug(msg string, args ...interface{})
	Info(msg string, args ...interface{})
	Warn(msg string, args ...interface{})
	Error(msg string, args ...interface{})
	Fatal(msg string, args ...interface{})

	// Output writes to stdout so it can be piped to subsequent commands
	Output(msg string, args ...interface{})

	// Header prints a header, can be bold etc. Implementation can decide how a header
	// should be made. Should always be printed as info message
	Header(msg string)

	// Separator between elements. Should always be printed as info message
	Separator(title string)

	// NewLine adds a new line if necessary
	NewLine()
}

Handler for ui commands. Can ask for input, output messages and enforce a specific format.

type Level

type Level int

Level of output

const (
	DebugLevel Level = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)

func ParseLevel

func ParseLevel(level string) Level

ParseLevel returns the level defined by string. If it is not able to parse the level string it will return Info as default level

Since there is no trace level that is interpreted as debug

type Writer

type Writer struct{}

Writer implements io.Writer

func (Writer) Write

func (u Writer) Write(p []byte) (n int, err error)

Write tries to parse the incoming string. It will try to read the log level at start of string as [LEVEL]. If not found it will just use InfoLevel

Jump to

Keyboard shortcuts

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