genericcli

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 15 Imported by: 9

Documentation

Index

Constants

View Source
const TruncateElipsis = "..."

Variables

This section is empty.

Functions

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 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 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)
	// ToCreate transforms an entity's response object to its create request.
	// This is required for capabilities like creation from file of response objects.
	ToCreate(r R) (C, error)
	// ToUpdate transforms an entity's response object to its update request.
	// This is required for capabilities like update from file of response objects or edit.
	ToUpdate(r R) (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)

	// AvailableSortKeys adds an order flag to list commands by which keys this entity can be sorted. use the multisorter of this project to achieve sortability.
	AvailableSortKeys []string

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

	// 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) (MultiApplyResults[R], error)

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

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) (R, error)

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(args []string) (R, error)

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

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

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

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

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

func (a *GenericCLI[C, U, R]) DescribeAndPrint(args []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() ([]R, error)

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

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

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) (R, error)

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

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

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

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

type MultiApplyAction

type MultiApplyAction string
const (
	MultiApplyCreated       MultiApplyAction = "created"
	MultiApplyUpdated       MultiApplyAction = "updated"
	MultiApplyErrorOnCreate MultiApplyAction = "error_on_create"
	MultiApplyErrorOnUpdate MultiApplyAction = "error_on_update"
)

type MultiApplyResult

type MultiApplyResult[R any] struct {
	Result R
	Action MultiApplyAction
	Error  error
}

type MultiApplyResults

type MultiApplyResults[R any] []MultiApplyResult[R]

func (MultiApplyResults[R]) Error

func (ms MultiApplyResults[R]) Error() error

func (MultiApplyResults[R]) ToList

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

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
}

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