plugin

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: Apache-2.0 Imports: 19 Imported by: 16

Documentation

Overview

Package plugin provides functions to create new CLI plugins.

Index

Constants

View Source
const (
	// NativePluginCompletion indicates command line completion is determined using the built in
	// cobra.Command __complete mechanism.
	NativePluginCompletion PluginCompletionType = iota
	// StaticPluginCompletion indicates command line completion will be done by using a statically
	// defined list of options.
	StaticPluginCompletion
	// DynamicPluginCompletion indicates command line completion will be retrieved from the plugin
	// at runtime.
	DynamicPluginCompletion

	// RunCmdGroup are commands associated with Tanzu Run.
	RunCmdGroup CmdGroup = "Run"

	// ManageCmdGroup are commands associated with Tanzu Manage.
	ManageCmdGroup CmdGroup = "Manage"

	// BuildCmdGroup are commands associated with Tanzu Build.
	BuildCmdGroup CmdGroup = "Build"

	// ObserveCmdGroup are commands associated with Tanzu Observe.
	ObserveCmdGroup CmdGroup = "Observe"

	// SystemCmdGroup are system commands.
	SystemCmdGroup CmdGroup = "System"

	// TargetCmdGroup are various target commands.
	TargetCmdGroup CmdGroup = "Target"

	// VersionCmdGroup are version commands.
	VersionCmdGroup CmdGroup = "Version"

	// AdminCmdGroup are admin commands.
	AdminCmdGroup CmdGroup = "Admin"

	// TestCmdGroup is the test command group.
	TestCmdGroup CmdGroup = "Test"

	// ExtraCmdGroup is the extra command group.
	ExtraCmdGroup CmdGroup = "Extra"
)
View Source
const CmdTemplate = `` /* 1897-byte string literal not displayed */

CmdTemplate is the template for plugin commands. Deprecated: This variable is deprecated.

View Source
const DefaultDocsDir = "docs/cli/commands"

DefaultDocsDir is the base docs directory

View Source
const ErrorDocsOutputFolderNotExists = "" /* 137-byte string literal not displayed */
View Source
const (
	PluginRuntimeModulePath = "github.com/vmware-tanzu/tanzu-plugin-runtime"
)

Variables

View Source
var TemplateFuncs = template.FuncMap{
	"printHelp": printHelp,

	"rpad":                    component.Rpad,
	"bold":                    component.Bold,
	"underline":               component.Underline,
	"trimTrailingWhitespaces": component.TrimRightSpace,
	"beginsWith":              component.BeginsWith,
}

TemplateFuncs are the template usage funcs.

View Source
var UsageFunc = func(c *cobra.Command) error {
	t, err := template.New("usage").Funcs(TemplateFuncs).Parse(cmdTemplate)
	if err != nil {
		return err
	}
	return t.Execute(os.Stdout, c)
}

UsageFunc is the usage func for a plugin.

Functions

func ApplyDefaultConfig

func ApplyDefaultConfig(p *PluginDescriptor)

ApplyDefaultConfig applies default configurations to plugin descriptor.

func SyncPluginsForContextType added in v1.1.0

func SyncPluginsForContextType(contextType types.ContextType, opts ...CommandOptions) (string, error)

SyncPluginsForContextType will attempt to install plugins required by the active Context of the provided contextType. This is most useful for any plugin implementation which creates a new Context or updates an existing one as part of its operation, and prefers that the plugins appropriate for the Context are immediately available for use.

Note: This API is considered EXPERIMENTAL. Both the function's signature and implementation are subjected to change/removal if an alternative means to provide equivalent functionality can be introduced.

By default this API will write to os.Stdout and os.Stderr. To write the logs to different output and error streams as part of the plugin sync command invocation, configure CommandOptions as part of the parameters.

Example:

var outBuf bytes.Buffer
var errBuf bytes.Buffer
SyncPluginsForContextType(types.ContextTypeK8s, WithOutputWriter(&outBuf), WithErrorWriter(&errBuf))

func SyncPluginsForTarget deprecated added in v0.90.0

func SyncPluginsForTarget(target types.Target, opts ...CommandOptions) (string, error)

SyncPluginsForTarget will attempt to install plugins required by the active Context of the provided target. This is most useful for any plugin implementation which creates a new Context or updates an existing one as part of its operation, and prefers that the plugins appropriate for the Context are immediately available for use.

Note: This API is considered EXPERIMENTAL. Both the function's signature and implementation are subjected to change/removal if an alternative means to provide equivalent functionality can be introduced.

By default this API will write to os.Stdout and os.Stderr. To write the logs to different output and error streams as part of the plugin sync command invocation, configure CommandOptions as part of the parameters.

Example:

var outBuf bytes.Buffer
var errBuf bytes.Buffer
SyncPluginsForTarget(types.TargetK8s, WithOutputWriter(&outBuf), WithErrorWriter(&errBuf))

Deprecated: SyncPluginsForTarget is deprecated. Use SyncPluginsForContextType instead

func ValidatePlugin

func ValidatePlugin(p *PluginDescriptor) (err error)

ValidatePlugin validates the plugin descriptor.

Types

type CmdGroup

type CmdGroup string

CmdGroup is a group of CLI commands.

type CommandOptions added in v1.0.1

type CommandOptions func(o *cmdOptions)

func WithErrorWriter added in v1.0.1

func WithErrorWriter(errWriter io.Writer) CommandOptions

WithErrorWriter specifies the CommandOption for configuring Stderr

func WithNoStderr added in v1.0.1

func WithNoStderr() CommandOptions

WithNoStderr specifies to ignore stderr

func WithNoStdout added in v1.0.1

func WithNoStdout() CommandOptions

WithNoStdout specifies to ignore stdout

func WithOutputWriter added in v1.0.1

func WithOutputWriter(outWriter io.Writer) CommandOptions

WithOutputWriter specifies the CommandOption for configuring Stdout

type Hook

type Hook func() error

Hook is the mechanism used to define function for plugin hooks

type Plugin

type Plugin struct {
	Cmd *cobra.Command
}

Plugin is a Tanzu CLI plugin.

func NewPlugin

func NewPlugin(descriptor *PluginDescriptor) (*Plugin, error)

NewPlugin creates an instance of Plugin.

func (*Plugin) AddCommands

func (p *Plugin) AddCommands(commands ...*cobra.Command)

AddCommands adds commands to the plugin.

func (*Plugin) Execute

func (p *Plugin) Execute() error

Execute executes the plugin.

type PluginCompletionType

type PluginCompletionType int

PluginCompletionType is the mechanism used for determining command line completion options.

type PluginDescriptor

type PluginDescriptor struct {
	// Name is the name of the plugin.
	Name string `json:"name" yaml:"name"`

	// Description is the plugin's description.
	Description string `json:"description" yaml:"description"`

	// Target is the target to which plugin is applicable.
	Target types.Target `json:"target" yaml:"target"`

	// Version of the plugin. Must be a valid semantic version https://semver.org/
	Version string `json:"version" yaml:"version"`

	// BuildSHA is the git commit hash the plugin was built with.
	BuildSHA string `json:"buildSHA" yaml:"buildSHA"`

	// Digest is the SHA256 hash of the plugin binary.
	Digest string `json:"digest" yaml:"digest"`

	// Command group for the plugin.
	Group CmdGroup `json:"group" yaml:"group"`

	// DocURL for the plugin.
	DocURL string `json:"docURL" yaml:"docURL"`

	// Hidden tells whether the plugin should be hidden from the help command.
	Hidden bool `json:"hidden,omitempty" yaml:"hidden,omitempty"`

	// CompletionType determines how command line completion will be determined.
	CompletionType PluginCompletionType `json:"completionType" yaml:"completionType"`

	// CompletionArgs contains the valid command line completion values if `CompletionType`
	// is set to `StaticPluginCompletion`.
	CompletionArgs []string `json:"completionArgs,omitempty" yaml:"completionArgs,omitempty"`

	// CompletionCommand is the command to call from the plugin to retrieve a list of
	// valid completion nouns when `CompletionType` is set to `DynamicPluginCompletion`.
	CompletionCommand string `json:"completionCmd,omitempty" yaml:"completionCmd,omitempty"`

	// Aliases are other text strings used to call this command
	Aliases []string `json:"aliases,omitempty" yaml:"aliases,omitempty"`

	// PostInstallHook is function to be run post install of a plugin.
	PostInstallHook Hook `json:"-" yaml:"-"`

	// DefaultFeatureFlags is default featureflags to be configured if missing when invoking plugin
	DefaultFeatureFlags map[string]bool `json:"defaultFeatureFlags,omitempty" yaml:"defaultFeatureFlags,omitempty"`
}

PluginDescriptor describes a plugin binary.

Directories

Path Synopsis
Package buildinfo holds global variables set at build time to provide information about the plugin build.
Package buildinfo holds global variables set at build time to provide information about the plugin build.
Package lint provides linters to run against CLI plugins.
Package lint provides linters to run against CLI plugins.

Jump to

Keyboard shortcuts

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