util

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CSV     = "csv"     // CSV format, "," being used as delimiter
	DEFAULT = "default" // Colorful YAML output
	LDIF    = "ldif"    // LDAP Data Interchange Format
	YAML    = "yaml"    // YAML
)

Variables

View Source
var Logger = func() *logrus.Logger {
	l := logrus.New()
	l.SetLevel(logrus.WarnLevel)
	l.SetOutput(Stdout())
	l.SetFormatter(&logrus.JSONFormatter{
		DisableHTMLEscape: true,
		PrettyPrint:       true,
	})

	supererrors.RegisterCallback(func(err error) {
		l.SetOutput(Stderr())

		if l.Level >= logrus.DebugLevel {
			err = tracerr.Wrap(err)

			var frames []string
			for _, frame := range err.(tracerr.Error).StackTrace() {
				switch ctx := frame.String(); {

				case
					strings.Contains(ctx, "supererrors.Except"),
					strings.Contains(ctx, "runtime.main()"),
					strings.Contains(ctx, "runtime.goexit()"):

					continue

				default:
					frames = append(frames, frame.String())

				}
			}

			l.WithField("stack", frames).Fatalln(err)
		}

		l.SetFormatter(&logrus.TextFormatter{
			ForceColors:            IsColorEnabled(),
			DisableTimestamp:       true,
			DisableLevelTruncation: true,
		})
		l.Fatalln(err)
	})

	return l
}()

App logger (default format JSON)

Functions

func AskBool

func AskBool(cmd *cobra.Command, flagName string, args *[]string, opts ...survey.AskOpt) (bool, error)

Ask to provide a boolean value for given flag, while offering the option to deny answer (skip)

func AskCommand

func AskCommand(cmd *cobra.Command, def *cobra.Command, opts ...survey.AskOpt) (*cobra.Command, error)

Ask to select a sub-command while providing a default sub-command

func AskFilterString added in v1.0.9

func AskFilterString(cmd *cobra.Command, flagName string, filterString *string, searchArguments *client.SearchArguments)

AskFilterString asks for a filter string (LDAP syntax according to RFC 4515)

func AskID added in v1.0.9

func AskID(cmd *cobra.Command, flagName string, id *string, searchArguments *client.SearchArguments)

AskID asks for an ID (CN, DN, GUID, SAN, UPN, Name or DisplayName)

func AskLDAPDataInterchangeFormat

func AskLDAPDataInterchangeFormat(requests *ldif.LDIF, editor string) (bool, error)

Ask to modify an object in LDAP Interchange Data Format

func AskMultiline

func AskMultiline(cmd *cobra.Command, flagName string, args *[]string, opts ...survey.AskOpt) (bool, error)

Ask to provide answers for given flag in multiline text mode (each option is separated by a new line)

func AskString

func AskString(cmd *cobra.Command, flagName string, args *[]string, password bool, def string, opts ...survey.AskOpt) (bool, error)

Ask a string value for given flag, while offering a default value. If default value is not provided, the default value of the flag will be used. Password mode (sensitive input mode) is supported

func AskStrings

func AskStrings(cmd *cobra.Command, flagName string, options, def []string, args *[]string, opts ...survey.AskOpt) (bool, error)

Ask to select one or many option from many fro given flag, default options may be provided. If just one default option is being provided, a selection of only one option is foreseen, otherwise multiple selection is possible.

func Flush added in v1.0.2

func Flush(results attributes.Maps, requests *ldif.LDIF, format string, out io.Writer) error

Encode results into given format. Writer is supposed to be either stdout or a file. Per default, colorful YAML format takes precedences and is being emitted to stdout

func GetFieldsForBind

func GetFieldsForBind(bindParameters *auth.BindParameters, dialOptions *auth.DialOptions) logrus.Fields

Produce log fields describing a bind request

func GetFieldsForSearch

func GetFieldsForSearch(searchArguments *client.SearchArguments) logrus.Fields

Produce log fields describing a search request

func Is256ColorSupported

func Is256ColorSupported() bool

Check if current terminal supports ANSI 256-bit color codes. Env variables TERM and COLORTERM are considered

func IsColorEnabled

func IsColorEnabled() bool

Check if current terminal has colors enabled. Env variables CLICOLOR_FORCE, NO_COLOR and CLICOLOR are being considered

func IsTerminal

func IsTerminal(f *os.File) bool

Check if given terminal descriptor is a terminal or just a pipe

func IsTrueColorSupported

func IsTrueColorSupported() bool

Check fif terminal supports true color mode. Env variables TERM and COLORTERM are evaluated

func ListSupportedFormats

func ListSupportedFormats(quote bool) (list []string)

List supported formats

func PrintColors added in v1.0.4

func PrintColors(fn func(string, ...any) string, format string, a ...any) string

Print colorized text only if terminal supports ANSI color codes

func PrintlnAndExit

func PrintlnAndExit(code int, format string, a ...any)

Print to Stdout and exit

func SniffFormat

func SniffFormat(filename, format string) string

Detect expected format from file extension

func Stderr

func Stderr() *os.File

func Stdin

func Stdin() *os.File

func Stdout

func Stdout() *os.File

func TerminalSize

func TerminalSize(f *os.File) (int, int, error)

Get size of current terminal window

Types

type Fields

type Fields = logrus.Fields

Log fields in JSON format mode (default)

Jump to

Keyboard shortcuts

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