Documentation
¶
Overview ¶
Package util provides helper functions for the mediator CLI.
Package util provides helper functions for the mediator CLI.
Package util contains utility functions largely for unit testing. WARNING: Do not use the functions in this package that generate random / seeds for any security related purposes, outside of testing.
Package util provides helper functions for the mediator CLI.
Index ¶
- Variables
- func BindConfigFlag[V any](v *viper.Viper, flags *pflag.FlagSet, viperPath string, cmdLineArg string, ...) error
- func BindConfigFlagWithShort[V any](v *viper.Viper, flags *pflag.FlagSet, viperPath string, cmdLineArg string, ...) error
- func ConvertJsonToYaml(content json.RawMessage) (string, error)
- func ConvertYamlToJson(content string) (json.RawMessage, error)
- func ExitNicelyOnError(err error, message string)
- func ExpandFileArgs(files []string) ([]string, error)
- func GetAppContext() (context.Context, context.CancelFunc)
- func GetBytesFromProto(message protoreflect.ProtoMessage) ([]byte, error)
- func GetConfigValue(key string, flagName string, cmd *cobra.Command, defaultValue interface{}) interface{}
- func GetGrpcConnection(grpc_host string, grpc_port int, allowInsecure bool) (*grpc.ClientConn, error)
- func GetJsonFromProto(msg protoreflect.ProtoMessage) (string, error)
- func GetPassFromTerm(confirm bool) ([]byte, error)
- func GetRandomPort() (int, error)
- func GetYamlFromProto(msg protoreflect.ProtoMessage) (string, error)
- func GrpcForCommand(cmd *cobra.Command) (*grpc.ClientConn, error)
- func Int32FromString(v string) (int32, error)
- func JQReadFrom[T any](ctx context.Context, path string, obj any) (T, error)
- func NewRand(seed int64) *rand.Rand
- func OpenFileArg(f string, dashOpen io.Reader) (desc io.Reader, closer func(), err error)
- func RandomEmail(seed int64) string
- func RandomInt(min, max int64, seed int64) int64
- func RandomKeypair(length int) ([]byte, []byte)
- func RandomKeypairFile(length int, privateFilePath string, publicFilePath string) error
- func RandomName(seed int64) string
- func RandomPassword(length int, seed int64) string
- func RandomPrivateKeyFile(length int, filePath string) error
- func RandomString(n int, seed int64) string
- func RandomURL(seed int64) string
- func SanitizingInterceptor() grpc.UnaryServerInterceptor
- func SaveCredentials(creds Credentials) (string, error)
- func TranscodeJSONToYAML(r io.Reader, w io.Writer) error
- func TranscodeYAMLToJSON(r io.Reader, w io.Writer) error
- func WriteToFile(out string, content []byte, perms fs.FileMode) error
- type Credentials
- type FlagInst
- type FlagInstShort
- type JWTTokenCredentials
- type NiceStatus
- type TestWriter
Constants ¶
This section is empty.
Variables ¶
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 ConvertJsonToYaml ¶
func ConvertJsonToYaml(content json.RawMessage) (string, error)
ConvertJsonToYaml converts json to yaml
func ConvertYamlToJson ¶
func ConvertYamlToJson(content string) (json.RawMessage, error)
ConvertYamlToJson converts yaml to json
func ExitNicelyOnError ¶
ExitNicelyOnError print a message and exit with the right code
func ExpandFileArgs ¶ added in v0.0.2
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 GetAppContext ¶
func GetAppContext() (context.Context, context.CancelFunc)
GetAppContext is a helper for getting the cmd app context
func GetBytesFromProto ¶
func GetBytesFromProto(message protoreflect.ProtoMessage) ([]byte, error)
GetBytesFromProto given a proto message, formats into bytes
func GetConfigValue ¶
func GetConfigValue(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) (*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 ¶
GetPassFromTerm gets a password from the terminal
func GetRandomPort ¶
GetRandomPort returns a random port number. The binding address should not need to be configurable as this is a short lived operation just to disover a random available port. Note that there is a possible race condition here if another process binds to the same port between the time we discover it and the time we use it. This is unlikely to happen in practice, but if it does, the user will need to retry the command. Marking a nosec here because we want this to listen on all addresses to ensure a reliable connection chance for the client. This is based on lessons learned from the sigstore CLI.
func GetYamlFromProto ¶
func GetYamlFromProto(msg protoreflect.ProtoMessage) (string, error)
GetYamlFromProto given a proto message, formats into yaml
func GrpcForCommand ¶
func GrpcForCommand(cmd *cobra.Command) (*grpc.ClientConn, error)
GrpcForCommand is a helper for getting a testing connection from cobra flags
func Int32FromString ¶ added in v0.0.2
Int32FromString converts a string to an int32
func JQReadFrom ¶ added in v0.0.2
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
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 RandomKeypair ¶
RandomKeypair returns a random RSA keypair
func RandomKeypairFile ¶
RandomKeypairFile generates a random RSA keypair and writes it to files
func RandomPassword ¶
RandomPassword returns a random password.
func RandomPrivateKeyFile ¶
RandomPrivateKeyFile generates a random RSA private key and writes it to a file
func RandomString ¶
RandomString returns a random string of length n.
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(creds Credentials) (string, error)
SaveCredentials saves the credentials to a file
func TranscodeJSONToYAML ¶
TranscodeJSONToYAML transcodes JSON to YAML
func TranscodeYAMLToJSON ¶
TranscodeYAMLToJSON transcodes YAML to JSON
Types ¶
type Credentials ¶
type Credentials struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
AccessTokenExpiresIn int `json:"access_token_expires_in"`
RefreshTokenExpiresIn int `json:"refresh_token_expires_in"`
}
Credentials is a struct to hold the access and refresh tokens
func LoadCredentials ¶
func LoadCredentials() (Credentials, error)
LoadCredentials loads the credentials from a file
type FlagInstShort ¶
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 {
// Descritpion 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 GetNiceStatus ¶
func GetNiceStatus(code codes.Code) *NiceStatus
GetNiceStatus get a nice status from the code.
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 TestWriter ¶
type TestWriter struct {
Output string
}
TestWriter is a helper struct for testing