util

package
v0.0.85 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package util provides helper functions for the minder CLI.

Package util provides helper functions for the minder CLI.

Package util provides helper functions for minder

Package util provides helper functions for the minder CLI.

Index

Constants

View Source
const (
	// CurlCmdMaxSize is the maximum size of the rendered curl command
	CurlCmdMaxSize = 2048
)

Variables

View Source
var (
	// PyRequestsVersionRegexp is a regexp to match a line in a requirements.txt file, including the package version
	// and the comparison operators
	PyRequestsVersionRegexp = regexp.MustCompile(`\s*(>=|<=|==|>|<|!=)\s*(\d+(\.\d+)*(\*)?)`)
	// PyRequestsNameRegexp is a regexp to match a line in a requirements.txt file, parsing out the package name
	PyRequestsNameRegexp = regexp.MustCompile(`\s*(>=|<=|==|>|<|!=)`)
)
View Source
var (
	// ErrExceededSizeLimit is returned when the size limit is exceeded
	ErrExceededSizeLimit = errors.New("exceeded size limit")
)
View Source
var ErrNoValueFound = errors.New("evaluation error")

ErrNoValueFound is an error that is returned when the accessor doesn't find anything

View Source
var (
	// TemplateFuncs is a map of functions that can be used in templates
	// It introduces two custom functions:
	// - asMap: converts a structpb (or anything that implements the AsMap function call) to a map
	// - mapGet: returns the value of a key in a map
	TemplateFuncs = template.FuncMap{
		"asMap":  asMap,
		"mapGet": mapGet,
	}
)

Functions

func GenerateCurlCommand

func GenerateCurlCommand(ctx context.Context, method, apiBaseURL, endpoint, body string) (string, error)

GenerateCurlCommand generates a curl command from a method, apiBaseURL, endpoint, and body this is useful to provide a dry-run for remediations

func GetBytesFromProto

func GetBytesFromProto(message protoreflect.ProtoMessage) ([]byte, error)

GetBytesFromProto given a proto message, formats into bytes

func GetJsonFromProto

func GetJsonFromProto(msg protoreflect.ProtoMessage) (string, error)

GetJsonFromProto given a proto message, formats into json

func GetYamlFromProto

func GetYamlFromProto(msg protoreflect.ProtoMessage) (string, error)

GetYamlFromProto given a proto message, formats into yaml

func HttpMethodFromString

func HttpMethodFromString(inMeth, dfl string) string

HttpMethodFromString returns the HTTP method from a string based on upprecase inMeth, defaulting to dfl

func Int32FromString

func Int32FromString(v string) (int32, error)

Int32FromString converts a string to an int32

func JQEvalBoolExpression added in v0.0.70

func JQEvalBoolExpression(ctx context.Context, path string, obj any) (bool, error)

JQEvalBoolExpression evaluates the given path on the object and returns the string value the path is the accessor path in jq format which must return a boolean value.

func JQReadConstant

func JQReadConstant[T any](constant any) (T, error)

JQReadConstant gets the typed value from the given constant. Returns an error when the type assertion fails.

func JQReadFrom

func JQReadFrom[T any](ctx context.Context, path string, obj any) (T, error)

JQReadFrom gets the typed value from the given accessor. Returns an error when the accessor doesn't find anything or when the type assertion fails. Useful for when you know the type you're expecting AND the accessor must return a value (IOW, the value is required by the caller)

func OpenFileArg

func OpenFileArg(f string, dashOpen io.Reader) (desc io.Reader, closer func(), err error)

OpenFileArg opens a file argument and returns a descriptor, closer, and error If the file is "-", it will return whatever is passed in as dashOpen and a no-op closer

func SanitizingInterceptor

func SanitizingInterceptor() grpc.UnaryServerInterceptor

SanitizingInterceptor sanitized error statuses which do not conform to NiceStatus, ensuring that we don't accidentally leak implementation details over gRPC.

func ViperLogLevelToZerologLevel

func ViperLogLevelToZerologLevel(viperLogLevel string) zerolog.Level

ViperLogLevelToZerologLevel converts a viper log level to a zerolog log level

Types

type ExpandedFile added in v0.0.70

type ExpandedFile struct {
	Path     string
	Expanded bool
}

ExpandedFile is a struct to hold a file path and whether it was expanded

func ExpandFileArgs

func ExpandFileArgs(files ...string) ([]ExpandedFile, error)

ExpandFileArgs expands a list of file arguments into a list of files. If the file list contains "-" or regular files, it will leave them as-is. If the file list contains directories, it will expand them into a list of files.

type LimitedWriter

type LimitedWriter struct {
	// contains filtered or unexported fields
}

LimitedWriter is an io.Writer that limits the number of bytes written

func NewLimitedWriter

func NewLimitedWriter(w io.Writer, limit int) *LimitedWriter

NewLimitedWriter creates a new LimitedWriter

func (*LimitedWriter) Write

func (lw *LimitedWriter) Write(p []byte) (int, error)

Write implements the io.Writer interface

type NiceStatus

type NiceStatus struct {
	// Description status code
	Code codes.Code
	// Name
	Name string
	// Description
	Description string
	// Actions, reasons and links
	Details string
}

NiceStatus A wrapper around a status to give a better description.

func FromRpcError

func FromRpcError(s *status.Status) *NiceStatus

FromRpcError convert a grpc status.Status to a nice status for formatting

func GetNiceStatus

func GetNiceStatus(code codes.Code) *NiceStatus

GetNiceStatus get a nice status from the code.

func UserVisibleError

func UserVisibleError(code codes.Code, message string, args ...any) *NiceStatus

UserVisibleError returns a status error where message is visible to the user, rather than being filtered to generic advice. You need to use this explicitly, so that it's easy to track where we are providing (leaking) user-visible information from minder.

func (*NiceStatus) Error

func (s *NiceStatus) Error() string

Error implements Golang error

func (*NiceStatus) GRPCStatus

func (s *NiceStatus) GRPCStatus() *status.Status

GRPCStatus makes NiceStatus a valid GRPC status response (see https://godoc.org/google.golang.org/grpc/status#FromError for details)

func (*NiceStatus) SetCode

func (s *NiceStatus) SetCode(code codes.Code) *NiceStatus

SetCode generates the nice status from the code.

func (*NiceStatus) String

func (s *NiceStatus) String() string

String convert the status to a string

type SafeTemplate

type SafeTemplate struct {
	// contains filtered or unexported fields
}

SafeTemplate is a `template` wrapper that ensures that the template is rendered in a safe and secure manner. That is, with memory limits and timeouts.

func NewSafeHTMLTemplate

func NewSafeHTMLTemplate(tmpl *string, name string) (*SafeTemplate, error)

NewSafeHTMLTemplate creates a new SafeTemplate for HTML templates

func NewSafeTextTemplate

func NewSafeTextTemplate(tmpl *string, name string) (*SafeTemplate, error)

NewSafeTextTemplate creates a new SafeTemplate for text templates

func (*SafeTemplate) Execute

func (t *SafeTemplate) Execute(ctx context.Context, w io.Writer, data any, limit int) error

Execute executes the template with the given data

func (*SafeTemplate) Render

func (t *SafeTemplate) Render(ctx context.Context, data any, limit int) (string, error)

Render renders the template with the given data

type TestWriter

type TestWriter struct {
	Output string
}

TestWriter is a helper struct for testing

func (*TestWriter) Write

func (tw *TestWriter) Write(p []byte) (n int, err error)

Directories

Path Synopsis
Package cache contains cache utilities and implementations
Package cache contains cache utilities and implementations
cli
Package cli contains utility for the cli
Package cli contains utility for the cli
table
Package table contains utilities for rendering tables
Package table contains utilities for rendering tables
table/layouts
Package layouts defines the available table layouts
Package layouts defines the available table layouts
table/simple
Package simple contains a simple table
Package simple contains a simple table
useragent
Package useragent contains utilities for setting up the CLI's user agent
Package useragent contains utilities for setting up the CLI's user agent
Package cursor provides a way to encode and decode cursors for paginated queries
Package cursor provides a way to encode and decode cursors for paginated queries
Package jsonyaml contains utility functions for converting to/from json and yaml
Package jsonyaml contains utility functions for converting to/from json and yaml
Package ptr contains the Ptr function
Package ptr contains the Ptr function
Package rand contains utility functions largely for unit testing.
Package rand contains utility functions largely for unit testing.
Package schemaupdate contains utility functions to compare two schemas for updates
Package schemaupdate contains utility functions to compare two schemas for updates
Package schemavalidate provides utilities for validating JSON schemas.
Package schemavalidate provides utilities for validating JSON schemas.
Package testqueue contains queue utilities for testing
Package testqueue contains queue utilities for testing

Jump to

Keyboard shortcuts

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