util

package
v2.6.10 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package util provides utility functions for the application. Survey framework is used to implement the interactive prompts.

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)

AskBool to select a boolean value for given flag, default value may be provided.

func AskCommand

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

AskCommand to select a command from given commands' children, default command may be provided.

func AskFilterString

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

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)

AskLDAPDataInterchangeFormat to modify given LDAP Data Interchange Format (LDIF) requests

func AskMultiline

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

AskMultiline to select one or many values from many for given flag, default values may be provided (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)

AskString to select a string value for given flag, default value may be provided. 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)

AskStrings to select one or many values from many for given flag, default values 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

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

GetFieldsForBind produces log fields describing a bind request

func GetFieldsForSearch

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

GetFieldsForSearch produces log fields describing a search request

func HelpAliases

func HelpAliases(msg *string)

func Is256ColorSupported

func Is256ColorSupported() bool

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

func IsColorEnabled

func IsColorEnabled() bool

IsColorEnabled checks if current terminal supports ANSI color codes. Env variables CLICOLOR_FORCE, NO_COLOR and CLICOLOR are being considered

func IsTerminal

func IsTerminal(f *os.File) bool

IsTerminal checks if given file descriptor is a terminal or just a pipe

func IsTrueColorSupported

func IsTrueColorSupported() bool

IsTrueColorSupported checks if current terminal supports ANSI 24-bit color codes. Env variables TERM and COLORTERM are evaluated

func ListSupportedFormats

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

ListSupportedFormats returns a list of supported formats

func PrintColors

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

PrintColors prints formatted string with colors if supported

func PrintlnAndExit

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

PrintColors prints a string with colors and exits with provided code

func SniffFormat

func SniffFormat(filename, format string) string

SniffFormat returns the format of a file from its extension

func Stderr

func Stderr() *os.File

Stderr returns standard error file descriptor

func Stdin

func Stdin() *os.File

Stdin returns standard input file descriptor

func Stdout

func Stdout() *os.File

Stdout returns standard output file descriptor

func TerminalSize

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

TerminalSize returns terminal size in columns and rows

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