genericcli

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 23 Imported by: 9

Documentation

Index

Constants

View Source
const TruncateElipsis = "..."

Variables

This section is empty.

Functions

func AddSortFlag added in v0.12.0

func AddSortFlag[R any](cmd *cobra.Command, sorter *multisort.Sorter[R])

func AlreadyExistsError

func AlreadyExistsError() error

func GetExactlyOneArg

func GetExactlyOneArg(args []string) (string, error)

func LabelsToMap

func LabelsToMap(labels []string) (map[string]string, error)

LabelsToMap splits strings at = and returns a corresponding map, errors out when there is no =.

func MapToLabels

func MapToLabels(m map[string]string) []string

MapToLabels returns a list of labels from a label map.

func Must

func Must(err error)

func NewCmds

func NewCmds[C any, U any, R any](c *CmdsConfig[C, U, R], additionalCmds ...*cobra.Command) *cobra.Command

NewCmds can be used to generate a new cobra/viper root cmd with a set of default cmds provided by the generic cli.

func OnlyCmds

func OnlyCmds(cmds ...DefaultCmd) map[DefaultCmd]bool

func ParseSortFlags added in v0.12.0

func ParseSortFlags() (multisort.Keys, error)

func PrintColoredYAML added in v0.12.0

func PrintColoredYAML(raw []byte) string

func Prompt

func Prompt() error

Prompt the user to given compare text

func PromptCustom

func PromptCustom(c *PromptConfig) error

PromptCustomAnswers the user to given compare text "no" can be an empty string, "yes" is the list of accepted yes answers.

func PromptDefaultAnswers

func PromptDefaultAnswers() []string

func PromptDefaultQuestion

func PromptDefaultQuestion() string

func TruncateEnd

func TruncateEnd[T Truncatable](input T, maxlength int) T

TruncateEnd will trim a string at the end.

func TruncateEndElipsis

func TruncateEndElipsis[T Truncatable](input T, elipsis T, maxlength int) T

TruncateEndElipsis will trim a string at the end and replace it with elipsis.

func TruncateMiddle

func TruncateMiddle[T Truncatable](input T, maxlength int) T

TruncateMiddle will trim a string in the middle.

func TruncateMiddleElipsis

func TruncateMiddleElipsis[T Truncatable](input T, elipsis T, maxlength int) T

TruncateMiddleElipsis will trim a string in the middle and replace it with elipsis.

func TruncateStart

func TruncateStart[T Truncatable](input T, maxlength int) T

TruncateStart will trim a string at the start.

func TruncateStartElipsis

func TruncateStartElipsis[T Truncatable](input T, elipsis T, maxlength int) T

TruncateStartElipsis will trim a string at the start and replace it with elipsis.

func YamlIsEqual

func YamlIsEqual(x []byte, y []byte) (bool, error)

YamlIsEqual returns true if a yaml equal in content.

Types

type BulkAction added in v0.12.0

type BulkAction string
const (
	BulkCreated       BulkAction = "created"
	BulkUpdated       BulkAction = "updated"
	BulkDeleted       BulkAction = "deleted"
	BulkErrorOnCreate BulkAction = "error_on_create"
	BulkErrorOnUpdate BulkAction = "error_on_update"
	BulkErrorOnDelete BulkAction = "error_on_delete"
)

type BulkResult added in v0.12.0

type BulkResult[R any] struct {
	Result   R
	Action   BulkAction
	Error    error
	Duration time.Duration
}

func (*BulkResult[R]) Print added in v0.12.0

func (m *BulkResult[R]) Print(p printers.Printer)

type BulkResults added in v0.12.0

type BulkResults[R any] []BulkResult[R]

func (BulkResults[R]) ToError added in v0.12.0

func (ms BulkResults[R]) ToError(joinErrors bool) error

func (BulkResults[R]) ToList added in v0.12.0

func (ms BulkResults[R]) ToList() []R

type CRUD

type CRUD[C any, U any, R any] interface {
	// Get returns the entity with the given id.
	Get(id string) (R, error)
	// List returns a slice of entities.
	List() ([]R, error)
	// Create tries to create the entity with the given request and returns the created entity.
	Create(rq C) (R, error)
	// Update tries to update the entity with the given request and returns the updated entity.
	Update(rq U) (R, error)
	// Delete tries to delete the entity with the given id and returns the deleted entity.
	Delete(id string) (R, error)
	// Convert converts an entity's response object to best possible create and update requests and additionally returns the entities ID.
	// This is required for capabilities like creation/update/deletion from a file of response objects.
	Convert(r R) (string, C, U, error)
}

CRUD must be implemented in order to get generic CLI functionality.

C is the create request for an entity. U is the update request for an entity. R is the response object of an entity.

type CmdsConfig

type CmdsConfig[C any, U any, R any] struct {
	GenericCLI *GenericCLI[C, U, R]

	// OnlyCmds defines which default commands to include from the generic cli. if empty, all default commands will be added.
	OnlyCmds map[DefaultCmd]bool

	// BinaryName is the name of the cli binary.
	BinaryName string
	// Singular, Plural is the name of the entity for which the default cmds are generated.
	Singular, Plural string
	// Description described the entity for which the default cmds are generated.
	Description string
	// Aliases provides additional aliases for the root cmd.
	Aliases []string

	// DescribePrinter is the printer that is used for describing the entity. It's a function because printers potentially get intialized later in the game.
	DescribePrinter func() printers.Printer
	// ListPrinter is the printer that is used for listing multiple entities. It's a function because printers potentially get intialized later in the game.
	ListPrinter func() printers.Printer

	// CreateRequestFromCLI if not nil, this function uses the returned create request to create the entity.
	CreateRequestFromCLI func() (C, error)
	// UpdateRequestFromCLI if not nil, this function uses the returned update request to update the entity.
	UpdateRequestFromCLI func(args []string) (U, error)

	// Sorter allows sorting the results of list commands.
	Sorter *multisort.Sorter[R]

	// ValidArgsFn is a completion function that returns the valid command line arguments.
	ValidArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)

	// In defines from where input is read, defaults to stdin.
	In io.Reader
	// Out defines to where output is written, defaults to stdout.
	Out io.Writer

	// MutateFns can be used to customize default commands (adding additional CLI flags or something like that)
	RootCmdMutateFn     func(cmd *cobra.Command)
	ListCmdMutateFn     func(cmd *cobra.Command)
	DescribeCmdMutateFn func(cmd *cobra.Command)
	CreateCmdMutateFn   func(cmd *cobra.Command)
	UpdateCmdMutateFn   func(cmd *cobra.Command)
	DeleteCmdMutateFn   func(cmd *cobra.Command)
	ApplyCmdMutateFn    func(cmd *cobra.Command)
	EditCmdMutateFn     func(cmd *cobra.Command)
}

CmdsConfig provides the configuration for the default commands.

type DefaultCmd

type DefaultCmd string
const (
	ListCmd     DefaultCmd = "list"
	DescribeCmd DefaultCmd = "describe"
	CreateCmd   DefaultCmd = "create"
	UpdateCmd   DefaultCmd = "update"
	DeleteCmd   DefaultCmd = "delete"
	ApplyCmd    DefaultCmd = "apply"
	EditCmd     DefaultCmd = "edit"
)

type GenericCLI

type GenericCLI[C any, U any, R any] struct {
	// contains filtered or unexported fields
}

GenericCLI can be used to gain generic CLI functionality.

C is the create request for an entity. U is the update request for an entity. R is the response object of an entity.

func NewGenericCLI

func NewGenericCLI[C any, U any, R any](crud CRUD[C, U, R]) *GenericCLI[C, U, R]

NewGenericCLI returns a new generic cli.

C is the create request for an entity. U is the update request for an entity. R is the response object of an entity.

func (*GenericCLI[C, U, R]) ApplyFromFile

func (a *GenericCLI[C, U, R]) ApplyFromFile(from string) (BulkResults[R], error)

ApplyFromFile creates or updates entities from a given file of response entities. In order to work, the create function must return an already exists error as defined in this package.

As this function uses response entities, it is possible that create and update entity representation is inaccurate to a certain degree.

func (*GenericCLI[C, U, R]) ApplyFromFileAndPrint

func (a *GenericCLI[C, U, R]) ApplyFromFileAndPrint(from string, p printers.Printer) error

func (*GenericCLI[C, U, R]) Create

func (a *GenericCLI[C, U, R]) Create(rq C) (R, error)

func (*GenericCLI[C, U, R]) CreateAndPrint

func (a *GenericCLI[C, U, R]) CreateAndPrint(rq C, p printers.Printer) error

func (*GenericCLI[C, U, R]) CreateFromFile

func (a *GenericCLI[C, U, R]) CreateFromFile(from string) (BulkResults[R], error)

CreateFromFile creates entities from a given file containing response entities.

As this function uses response entities, it is possible that create and update entity representation is inaccurate to a certain degree.

func (*GenericCLI[C, U, R]) CreateFromFileAndPrint

func (a *GenericCLI[C, U, R]) CreateFromFileAndPrint(from string, p printers.Printer) error

func (*GenericCLI[C, U, R]) Delete

func (a *GenericCLI[C, U, R]) Delete(id string) (R, error)

func (*GenericCLI[C, U, R]) DeleteAndPrint

func (a *GenericCLI[C, U, R]) DeleteAndPrint(id string, p printers.Printer) error

func (*GenericCLI[C, U, R]) DeleteFromFile added in v0.12.0

func (a *GenericCLI[C, U, R]) DeleteFromFile(from string) (BulkResults[R], error)

DeleteFromFile updates a single entity from a given file containing a response entity.

As this function uses response entities, it is possible that create and update entity representation is inaccurate to a certain degree.

func (*GenericCLI[C, U, R]) DeleteFromFileAndPrint added in v0.12.0

func (a *GenericCLI[C, U, R]) DeleteFromFileAndPrint(from string, p printers.Printer) error

func (*GenericCLI[C, U, R]) Describe

func (a *GenericCLI[C, U, R]) Describe(id string) (R, error)

func (*GenericCLI[C, U, R]) DescribeAndPrint

func (a *GenericCLI[C, U, R]) DescribeAndPrint(id string, p printers.Printer) error

func (*GenericCLI[C, U, R]) Edit

func (a *GenericCLI[C, U, R]) Edit(args []string) (R, error)

func (*GenericCLI[C, U, R]) EditAndPrint

func (a *GenericCLI[C, U, R]) EditAndPrint(args []string, p printers.Printer) error

func (*GenericCLI[C, U, R]) Interface

func (a *GenericCLI[C, U, R]) Interface() CRUD[C, U, R]

Interface returns the interface that was used to create this generic cli.

func (*GenericCLI[C, U, R]) List

func (a *GenericCLI[C, U, R]) List(sortKeys ...multisort.Key) ([]R, error)

func (*GenericCLI[C, U, R]) ListAndPrint

func (a *GenericCLI[C, U, R]) ListAndPrint(p printers.Printer, sortKeys ...multisort.Key) error

func (*GenericCLI[C, U, R]) Sorter added in v0.11.0

func (a *GenericCLI[C, U, R]) Sorter() *multisort.Sorter[R]

Sorter returns the sorter of this generic cli.

func (*GenericCLI[C, U, R]) Update

func (a *GenericCLI[C, U, R]) Update(rq U) (R, error)

func (*GenericCLI[C, U, R]) UpdateAndPrint

func (a *GenericCLI[C, U, R]) UpdateAndPrint(rq U, p printers.Printer) error

func (*GenericCLI[C, U, R]) UpdateFromFile

func (a *GenericCLI[C, U, R]) UpdateFromFile(from string) (BulkResults[R], error)

UpdateFromFile updates entities from a given file containing response entities.

As this function uses response entities, it is possible that create and update entity representation is inaccurate to a certain degree.

func (*GenericCLI[C, U, R]) UpdateFromFileAndPrint

func (a *GenericCLI[C, U, R]) UpdateFromFileAndPrint(from string, p printers.Printer) error

func (*GenericCLI[C, U, R]) WithBulkPrint added in v0.12.0

func (a *GenericCLI[C, U, R]) WithBulkPrint() *GenericCLI[C, U, R]

WithBulkPrint prints results in a bulk at the end on multi-entity operations, the results are a list. default is printing results intermediately during the bulk operation, which causes single entities to be printed in sequence.

func (*GenericCLI[C, U, R]) WithBulkSecurityPrompt added in v0.12.0

func (a *GenericCLI[C, U, R]) WithBulkSecurityPrompt(in io.Reader, out io.Writer) *GenericCLI[C, U, R]

WithBulkSecurityPrompt prints interactive prompts before a multi-entity operation if there is a tty.

func (*GenericCLI[C, U, R]) WithFS

func (a *GenericCLI[C, U, R]) WithFS(fs afero.Fs) *GenericCLI[C, U, R]

func (*GenericCLI[C, U, R]) WithSorter added in v0.11.0

func (a *GenericCLI[C, U, R]) WithSorter(sorter *multisort.Sorter[R]) *GenericCLI[C, U, R]

func (*GenericCLI[C, U, R]) WithTimestamps added in v0.12.0

func (a *GenericCLI[C, U, R]) WithTimestamps() *GenericCLI[C, U, R]

WithBulkTimestamps prints out the duration of an operation to stdout during a bulk operation.

type MultiDocumentYAML

type MultiDocumentYAML[D any] struct {
	// contains filtered or unexported fields
}

MultiDocumentYAML offers functions on multidocument YAML files

func NewMultiDocumentYAML

func NewMultiDocumentYAML[D any]() *MultiDocumentYAML[D]

func (*MultiDocumentYAML[D]) ReadAll

func (m *MultiDocumentYAML[D]) ReadAll(from string) ([]D, error)

ReadAll reads all documents from a multi-document YAML from a given path

func (*MultiDocumentYAML[D]) ReadIndex

func (m *MultiDocumentYAML[D]) ReadIndex(from string, index int) (D, error)

ReadIndex reads a document from a specific index of a multi-document YAML from a given path

func (*MultiDocumentYAML[D]) ReadOne

func (m *MultiDocumentYAML[D]) ReadOne(from string) (D, error)

ReadOne reads exactly one document from a multi-document YAML from a given path, returns an error if there are no or more than one documents in it

type PromptConfig

type PromptConfig struct {
	Message         string
	No              string
	AcceptedAnswers []string
	ShowAnswers     bool
	In              io.Reader
	Out             io.Writer
}

type Truncatable

type Truncatable interface {
	~string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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