cli

package
v1.3.0-alpha.2 Latest Latest
Warning

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

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

Documentation

Overview

Package cli provides libraries for building CLI commands and plugins.

Index

Constants

View Source
const (
	// BinNamePrefix is the prefix for tanzu plugin binary names.
	BinNamePrefix = "tanzu-plugin-"

	// TestBinNamePrefix is the prefix for tanzu plugin binary names.
	TestBinNamePrefix = "tanzu-plugin-test-"

	// ArtifactNamePrefix is the prefix for tanzu artifact names.
	ArtifactNamePrefix = "tanzu"
)
View Source
const (
	// ManifestFileName is the file name for the manifest.
	ManifestFileName = "manifest.yaml"
	// PluginManifestFileName is the file name for the plugin manifest.
	PluginManifestFileName = "plugin_manifest.yaml"
	// PluginGroupManifestFileName is the file name for the plugin manifest.
	PluginGroupManifestFileName = "plugin_group_manifest.yaml"
	// PluginDescriptorFileName is the file name for the plugin descriptor.
	PluginDescriptorFileName = "plugin.yaml"
	// AllPlugins is the keyword for all plugins.
	AllPlugins = "all"
)
View Source
const CoreName = "core"

CoreName is the name of the core binary.

View Source
const SubCmdTemplate = `` /* 990-byte string literal not displayed */

SubCmdTemplate is the template for all core sub-commands.

Variables

View Source
var (
	// MinOSArch defines the minimum OS/ARCH combinations for which plugins need to be built
	MinOSArch = []Arch{LinuxAMD64, DarwinAMD64, WinAMD64}

	// AllOSArch defines all OS/ARCH combinations for which plugins can be built
	AllOSArch = []Arch{LinuxAMD64, DarwinAMD64, WinAMD64, LinuxARM64, DarwinARM64, WinARM64}

	// GOOS is the current go os.  Defaults to runtime.GOOS but could be overridden.
	// The CLI code should always this variable instead of runtime.GOOS.
	GOOS = runtime.GOOS
	// GOARCH is the current go architecture.  Defaults to runtime.GOARCH but is overridden
	// for scenarios like installing AMD64 plugins on an ARM64 machine using emulation.
	// The CLI code should always this variable instead of runtime.GOARCH.
	GOARCH = runtime.GOARCH
)
View Source
var CoreDescriptor = plugin.PluginDescriptor{
	Name:        CoreName,
	Description: coreDescription,
	Version:     buildinfo.Version,
	BuildSHA:    buildinfo.SHA,
}

CoreDescriptor is the core descriptor.

View Source
var CorePlugin = Plugin{
	Name:        CoreName,
	Description: coreDescription,
}

CorePlugin is the core plugin.

View Source
var DefaultVersionSelector = SelectVersionStable

DefaultVersionSelector is the default version selector.

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

SubCmdUsageFunc is the usage func for all core sub-commands.

View Source
var TemplateFuncs = template.FuncMap{
	"rpad":                    component.Rpad,
	"bold":                    component.Bold,
	"underline":               component.Underline,
	"trimTrailingWhitespaces": component.TrimRightSpace,
	"beginsWith":              component.BeginsWith,
}

TemplateFuncs are the template usage funcs.

View Source
var VersionLatest = "latest"

VersionLatest is the latest version.

Functions

func BinFromPluginName

func BinFromPluginName(name string) string

BinFromPluginName return a plugin binary name from its name.

func BinTestFromPluginName

func BinTestFromPluginName(name string) string

BinTestFromPluginName return a plugin binary name from its name.

func FilterVersions

func FilterVersions(versions []string) (results []string)

FilterVersions returns the list of valid versions depending on whether unstable versions are requested or not.

func GetCmdForPlugin

func GetCmdForPlugin(p *PluginInfo) *cobra.Command

GetCmdForPlugin returns a cobra command for the plugin.

func GetCommandMapForPlugin added in v1.3.0

func GetCommandMapForPlugin(p *PluginInfo) map[string]*cobra.Command

GetCommandMapForPlugin returns how the plugin's commands should be mapped

func GetTestCmdForPlugin

func GetTestCmdForPlugin(p *PluginInfo) *cobra.Command

GetTestCmdForPlugin returns a cobra command for the test plugin.

func GetUnmappedCmdForPlugin added in v1.3.0

func GetUnmappedCmdForPlugin(p *PluginInfo) *cobra.Command

GetUnmappedCmdForPlugin returns a cobra command for the plugin unless there are remapping directives in the plugin info, in which case it will return nil instead.

func MakeArtifactName

func MakeArtifactName(pluginName string, arch Arch) string

MakeArtifactName returns an artifact name for a plugin name.

func MakeTestArtifactName

func MakeTestArtifactName(pluginName string, arch Arch) string

MakeTestArtifactName returns a test artifact name for a plugin name.

func PluginNameFromBin

func PluginNameFromBin(binName string) string

PluginNameFromBin returns a plugin name from the binary name.

func PluginNameFromTestBin

func PluginNameFromTestBin(binName string) string

PluginNameFromTestBin returns a plugin name from the test binary name.

func SelectVersionAlpha

func SelectVersionAlpha(versions []string) (v string)

SelectVersionAlpha specifically returns only -alpha tagged releases

func SelectVersionAny

func SelectVersionAny(versions []string) (v string)

SelectVersionAny returns the latest version from a list of versions including prereleases.

func SelectVersionExperimental

func SelectVersionExperimental(versions []string) (v string)

SelectVersionExperimental includes all prerelease tagged plugin versions, minus +build versions

func SelectVersionStable

func SelectVersionStable(versions []string) (v string)

SelectVersionStable returns the latest stable version from a list of versions. If there are no stable versions it will return an empty string.

func SetArch added in v1.1.0

func SetArch(a Arch)

func TestPluginPathFromPluginPath

func TestPluginPathFromPluginPath(pluginPath string) string

Types

type Arch

type Arch string

Arch represents a system architecture.

const (
	// Linux386 arch.
	Linux386 Arch = "linux_386"
	// LinuxAMD64 arch.
	LinuxAMD64 Arch = "linux_amd64"
	// LinuxARM64 arch.
	LinuxARM64 Arch = "linux_arm64"
	// DarwinAMD64 arch.
	DarwinAMD64 Arch = "darwin_amd64"
	// DarwinARM64 arch.
	DarwinARM64 Arch = "darwin_arm64"
	// Win386 arch.
	Win386 Arch = "windows_386"
	// WinAMD64 arch.
	WinAMD64 Arch = "windows_amd64"
	// WinARM64 arch.
	WinARM64 Arch = "windows_arm64"
)

func BuildArch

func BuildArch() Arch

BuildArch returns compile time build arch or locates it.

func (Arch) Arch added in v0.0.4

func (a Arch) Arch() string

Arch returns arch-name

func (Arch) IsWindows

func (a Arch) IsWindows() bool

IsWindows tells if an arch is windows.

func (Arch) OS added in v0.0.4

func (a Arch) OS() string

OS returns os-name based on the arch

func (Arch) String added in v0.0.4

func (a Arch) String() string

String converts arch to string

type CmdMap

type CmdMap map[string][]*cobra.Command

CmdMap is the map of command groups to plugins

type MainUsage

type MainUsage struct{}

MainUsage create the main usage display for tanzu cli.

func NewMainUsage

func NewMainUsage() *MainUsage

NewMainUsage creates an instance of Usage.

func (*MainUsage) GenerateDescriptor

func (u *MainUsage) GenerateDescriptor(c *cobra.Command, w io.Writer) error

GenerateDescriptor generates a descriptor

func (*MainUsage) Template

func (u *MainUsage) Template() string

Template returns the template for the root cmd help as well as for the two target commands ('kubernetes' and 'mission-control'). Those three commands use this template because they use command groups for their help text.

func (*MainUsage) UsageFunc

func (u *MainUsage) UsageFunc() func(*cobra.Command) error

UsageFunc generates a usage func for cobra.

type Manifest

type Manifest struct {
	// Created is the time the manifest was created.
	CreatedTime time.Time `json:"created,omitempty" yaml:"created,omitempty"`

	// Plugins is a list of plugin artifacts available.
	Plugins []Plugin `json:"plugins" yaml:"plugins"`
}

Manifest is stored in the repository which gives an inventory of the artifacts.

type Plugin

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

	// Target is the name of the plugin.
	Target string `json:"target" yaml:"target"`

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

	// Versions available for plugin.
	Versions []string `json:"versions" yaml:"versions"`
}

Plugin is an installable CLI plugin.

type PluginGroupManifest added in v0.0.11

type PluginGroupManifest struct {
	// Created is the time the manifest was created.
	CreatedTime time.Time `json:"created,omitempty" yaml:"created,omitempty"`

	// Plugins is a list of plugin artifacts including scope and version
	Plugins []PluginNameTargetScopeVersion `json:"plugins" yaml:"plugins"`
}

PluginGroupManifest is used to parse metadata about Plugin Groups

type PluginInfo

type PluginInfo 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"`

	// 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 plugin.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 plugin.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"`

	// InstallationPath is a relative installation path for a plugin binary.
	// E.g., cluster/v0.3.2@sha256:...
	InstallationPath string `json:"installationPath" yaml:"installationPath"`

	// Discovery is the name of the discovery from where
	// this plugin is discovered.
	Discovery string `json:"discovery" yaml:"discovery"`

	// Scope is the scope of the plugin. Stand-Alone or Context
	Scope string `json:"scope" yaml:"scope"`

	// Status is the current plugin installation status
	Status string `json:"status" yaml:"status"`

	// DiscoveredRecommendedVersion specifies the recommended version of the plugin that was discovered
	DiscoveredRecommendedVersion string `json:"discoveredRecommendedVersion" yaml:"discoveredRecommendedVersion"`

	// Target specifies the target of the plugin
	Target configtypes.Target `json:"target" yaml:"target"`

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

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

	// InvokedAs provides a specific mapping to how any command provided by this plugin should be invoked as.
	// EXPERIMENTAL: subject to change prior to the next official minor release
	InvokedAs []string `json:"invokedAs,omitempty" yaml:"invokedAs,omitempty"`

	// SupportedContextType specifies one of more ContextType that this plugin will specifically apply to.
	// EXPERIMENTAL: subject to change prior to the next official minor release
	SupportedContextType []configtypes.ContextType `json:"supportedContextType,omitempty" yaml:"supportedContextType,omitempty"`
}

PluginInfo contains information about a plugin binary

type PluginInfoSorter

type PluginInfoSorter []PluginInfo

PluginInfoSorter sorts PluginInfo objects.

func (PluginInfoSorter) Len

func (p PluginInfoSorter) Len() int

func (PluginInfoSorter) Less

func (p PluginInfoSorter) Less(i, j int) bool

func (PluginInfoSorter) Swap

func (p PluginInfoSorter) Swap(i, j int)

type PluginNameTargetScope added in v0.0.11

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

	// Target is the name of the plugin.
	Target string `json:"target" yaml:"target"`

	// IsContextScoped determines if the plugin is context-scoped or standalone
	IsContextScoped bool `json:"isContextScoped" yaml:"isContextScoped"`
}

PluginNameTargetScope defines the name, target and scope of a plugin

type PluginNameTargetScopeVersion added in v0.0.11

type PluginNameTargetScopeVersion struct {
	// PluginNameTargetScope
	PluginNameTargetScope `json:",inline" yaml:",inline"`

	// Version is the version of the plugin.
	Version string `json:"version" yaml:"version"`
}

PluginNameTargetScopeVersion defines the name, target, scope and version of a plugin

type PluginScopeMetadata added in v0.0.11

type PluginScopeMetadata struct {
	// Plugins is a list of plugin artifacts including scope and version
	Plugins []PluginNameTargetScope `json:"plugins" yaml:"plugins"`
}

PluginScopeMetadata is used to parse metadata about plugin and it's scope

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner is a plugin runner.

func NewRunner

func NewRunner(name, pluginAbsPath string, args []string) *Runner

NewRunner creates an instance of Runner.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context) error

Run runs a plugin.

func (*Runner) RunOutput

func (r *Runner) RunOutput(ctx context.Context) (string, string, error)

RunOutput runs a plugin and returns the output.

func (*Runner) RunTest

func (r *Runner) RunTest(ctx context.Context) error

RunTest runs a plugin test.

type VersionSelector

type VersionSelector func(versions []string) string

VersionSelector returns a version from a set of versions.

Jump to

Keyboard shortcuts

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