common

package
v0.0.1-0...-c0e0c3d Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package common provides some helper functionality for building command line handlers.

Index

Constants

View Source
const (
	// TokenEnvVarName is the environment variable name used to pass a service
	// account token.
	TokenEnvVarName = "INFRA_TOKEN"
)

Variables

This section is empty.

Functions

func AddCommonFlags

func AddCommonFlags(c *cobra.Command)

AddCommonFlags adds connection-related flags to infractl.

func ArgsWithHelp

func ArgsWithHelp(argFns ...cobra.PositionalArgs) cobra.PositionalArgs

ArgsWithHelp composes a series of positional argument check functions, and if any of them fail it will print the current command's help message.

func ContextWithTimeout

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

ContextWithTimeout returns a context and a cancel function that is bound to the given --timeout flag value.

func FormatExpiration

func FormatExpiration(delta time.Duration) string

FormatExpiration formats the given duration in a human-friendly way, indicating how long in the future the event will happen.

func FormatTime

func FormatTime(moment time.Time) string

FormatTime formats the given time in a human-friendly way, indicating how long in the past the event happened.

func GetGRPCConnection

func GetGRPCConnection() (*grpc.ClientConn, context.Context, func(), error)

GetGRPCConnection gets a grpc connection to the infra-server with the correct auth.

func MustBool

func MustBool(flags *pflag.FlagSet, name string) bool

MustBool looks up the named bool flag in the given flag set and panics if an error is returned.

func WithGRPCHandler

func WithGRPCHandler(handler GRPCHandler) func(cmd *cobra.Command, args []string) error

WithGRPCHandler performs all of the gRPC connection setup and teardown, as well as rendering the returned type as either JSON or in a human-readable fashion.

Types

type GRPCHandler

type GRPCHandler func(ctx context.Context, conn *grpc.ClientConn, cmd *cobra.Command, args []string) (PrettyPrinter, error)

GRPCHandler represents a function that consumes a gRPC connection, and produces a pretty-printable type.

type PrettyPrinter

type PrettyPrinter interface {
	// PrettyPrint renders this type in a pretty, human-readable fashion to
	// STDOUT.
	PrettyPrint(cmd *cobra.Command)
	PrettyJSONPrint(cmd *cobra.Command) error
}

PrettyPrinter represents a type that knows how to render itself in a pretty, human-readable fashion to STDOUT.

Jump to

Keyboard shortcuts

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