util

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 License: Apache-2.0 Imports: 39 Imported by: 0

Documentation

Overview

Package util provides helper functions for the mediator CLI.

Package util provides helper functions for the mediator CLI.

Package util provides helper functions for mediator

Package util provides helper functions for the mediator CLI.

Index

Constants

This section is empty.

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 ErrNoValueFound = errors.New("evaluation error")

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

Functions

func BindConfigFlag

func BindConfigFlag[V any](
	v *viper.Viper,
	flags *pflag.FlagSet,
	viperPath string,
	cmdLineArg string,
	defaultValue V,
	help string,
	binder FlagInst[V],
) error

BindConfigFlag is a helper function that binds a configuration value to a flag.

Parameters: - v: The viper.Viper object used to retrieve the configuration value. - flags: The pflag.FlagSet object used to retrieve the flag value. - viperPath: The path used to retrieve the configuration value from Viper. - cmdLineArg: The flag name used to check if the flag has been set and to retrieve its value. - help: The help text for the flag. - defaultValue: A default value used to determine the type of the flag (string, int, etc.). - binder: A function that creates a flag and returns a pointer to the value.

func BindConfigFlagWithShort

func BindConfigFlagWithShort[V any](
	v *viper.Viper,
	flags *pflag.FlagSet,
	viperPath string,
	cmdLineArg string,
	short string,
	defaultValue V,
	help string,
	binder FlagInstShort[V],
) error

BindConfigFlagWithShort is a helper function that binds a configuration value to a flag.

Parameters: - v: The viper.Viper object used to retrieve the configuration value. - flags: The pflag.FlagSet object used to retrieve the flag value. - viperPath: The path used to retrieve the configuration value from Viper. - cmdLineArg: The flag name used to check if the flag has been set and to retrieve its value. - short: The short name for the flag. - help: The help text for the flag. - defaultValue: A default value used to determine the type of the flag (string, int, etc.). - binder: A function that creates a flag and returns a pointer to the value.

func ExitNicelyOnError

func ExitNicelyOnError(err error, message string)

ExitNicelyOnError print a message and exit with the right code

func ExpandFileArgs added in v0.0.2

func ExpandFileArgs(files []string) ([]string, 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.

func GenerateCurlCommand added in v0.0.5

func GenerateCurlCommand(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 GetAppContext

func GetAppContext() (context.Context, context.CancelFunc)

GetAppContext is a helper for getting the cmd app context

func GetArtifactWithVersions added in v0.0.5

func GetArtifactWithVersions(ctx context.Context, store db.Store, repoID, artifactID uuid.UUID) (*minderv1.Artifact, error)

GetArtifactWithVersions retrieves an artifact and its versions from the database

func GetBytesFromProto

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

GetBytesFromProto given a proto message, formats into bytes

func GetConfigValue

func GetConfigValue(v *viper.Viper, key string, flagName string, cmd *cobra.Command, defaultValue interface{}) interface{}

GetConfigValue is a helper function that retrieves a configuration value and updates it if the corresponding flag is set.

Parameters: - key: The key used to retrieve the configuration value from Viper. - flagName: The flag name used to check if the flag has been set and to retrieve its value. - cmd: The cobra.Command object to access the flags. - defaultValue: A default value used to determine the type of the flag (string, int, etc.).

Returns: - The updated configuration value based on the flag, if it is set, or the original value otherwise.

func GetGrpcConnection

func GetGrpcConnection(grpc_host string, grpc_port int, allowInsecure bool, issuerUrl string, realm string, clientId string) (
	*grpc.ClientConn, error)

GetGrpcConnection is a helper for getting a testing connection for grpc

func GetJsonFromProto

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

GetJsonFromProto given a proto message, formats into json

func GetPassFromTerm

func GetPassFromTerm(confirm bool) ([]byte, error)

GetPassFromTerm gets a password from the terminal

func GetToken added in v0.0.7

func GetToken(issuerUrl string, realm string, clientId string) (string, error)

GetToken retrieves the access token from the credentials file and refreshes it if necessary

func GetYamlFromProto

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

GetYamlFromProto given a proto message, formats into yaml

func GrpcForCommand

func GrpcForCommand(cmd *cobra.Command, v *viper.Viper) (*grpc.ClientConn, error)

GrpcForCommand is a helper for getting a testing connection from cobra flags

func HttpMethodFromString added in v0.0.5

func HttpMethodFromString(inMeth, dfl string) string

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

func Int32FromString added in v0.0.2

func Int32FromString(v string) (int32, error)

Int32FromString converts a string to an int32

func JQReadFrom added in v0.0.2

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 added in v0.0.2

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 ParseNewTemplate added in v0.0.5

func ParseNewTemplate(tmpl *string, name string) (*template.Template, error)

ParseNewTemplate parses a named template from a string, ensuring it is not empty

func RemoveCredentials added in v0.0.8

func RemoveCredentials() error

RemoveCredentials removes the local credentials file

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 SaveCredentials

func SaveCredentials(tokens OpenIdCredentials) (string, error)

SaveCredentials saves the credentials to a file

func WriteToFile

func WriteToFile(out string, content []byte, perms fs.FileMode) error

WriteToFile writes the content to a file if the out parameter is not empty.

Types

type FlagInst

type FlagInst[V any] func(name string, value V, usage string) *V

FlagInst is a function that creates a flag and returns a pointer to the value

type FlagInstShort

type FlagInstShort[V any] func(name, shorthand string, value V, usage string) *V

FlagInstShort is a function that creates a flag and returns a pointer to the value

type JWTTokenCredentials

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

JWTTokenCredentials is a helper struct for grpc

func (JWTTokenCredentials) GetRequestMetadata

func (jwt JWTTokenCredentials) GetRequestMetadata(_ context.Context, _ ...string) (map[string]string, error)

GetRequestMetadata implements the PerRPCCredentials interface.

func (JWTTokenCredentials) RequireTransportSecurity

func (JWTTokenCredentials) RequireTransportSecurity() bool

RequireTransportSecurity implements the PerRPCCredentials 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 added in v0.0.3

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 added in v0.0.3

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 mediator.

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 OpenIdCredentials added in v0.0.4

type OpenIdCredentials struct {
	AccessToken          string    `json:"access_token"`
	RefreshToken         string    `json:"refresh_token"`
	AccessTokenExpiresAt time.Time `json:"expiry"`
}

OpenIdCredentials is a struct to hold the access and refresh tokens

func LoadCredentials

func LoadCredentials() (OpenIdCredentials, error)

LoadCredentials loads the credentials from a file

func RefreshCredentials added in v0.0.7

func RefreshCredentials(refreshToken string, issuerUrl string, realm string, clientId string) (OpenIdCredentials, error)

RefreshCredentials uses a refresh token to get and save a new set of credentials

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 cli contains utility for the cli
Package cli contains utility for the cli
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 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

Jump to

Keyboard shortcuts

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