cli

package
v0.0.0-...-b25b198 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2023 License: Apache-2.0 Imports: 32 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 (
	// Name of the CLI.
	Name = "tanzu"

	// ClientName of the CLI.
	ClientName = "tanzu-cli"
)
View Source
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"

	// ManifestFileName is the file name for the manifest.
	ManifestFileName = "manifest.yaml"
	// PluginFileName is the file name for the plugin descriptor.
	PluginFileName = "plugin.yaml"
	// DefaultArtifactsDirectory is the root artifacts directory
	DefaultArtifactsDirectory = "artifacts"

	// AllPlugins is the keyword for all plugins.
	AllPlugins = "all"

	// DefaultManifestQueryTimeout is max time to wait for querying for a plugin manifest
	DefaultManifestQueryTimeout = 5 * time.Second
)
View Source
const CoreName = "core"

CoreName is the name of the core binary.

View Source
const (
	// EnvPluginStateKey is the environment key that contains the path to the
	// plugin state file.
	EnvPluginStateKey = "TANZU_STATE"
)
View Source
const SubCmdTemplate = `` /* 988-byte string literal not displayed */

SubCmdTemplate is the template for plugin commands.

Variables

View Source
var CoreDescriptor = cliapi.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 DefaultDistro = []string{"login", "pinniped-auth", "cluster", "management-cluster", "kubernetes-release", "package", "secret"}

DefaultDistro is the core set of plugins that should be included with the CLI.

View Source
var DefaultLocalRepository = &LocalRepository{
	path: fmt.Sprintf("./%s", DefaultArtifactsDirectory),
}

DefaultLocalRepository is the default local repository.

View Source
var (
	// DefaultPluginRoot is the default plugin root.
	DefaultPluginRoot = filepath.Join(xdg.DataHome, "tanzu-cli")
)
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 a plugin.

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 AppendClientMetadata

func AppendClientMetadata(ctx context.Context) context.Context

AppendClientMetadata adds client metadata.

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 Clean

func Clean() error

Clean deletes all plugins and tests.

func CleanCatalogCache

func CleanCatalogCache() error

CleanCatalogCache cleans the catalog cache

func DeletePlugin

func DeletePlugin(name string) error

DeletePlugin deletes a plugin.

func DescribePlugin

func DescribePlugin(name string) (desc *cliapi.PluginDescriptor, err error)

DescribePlugin describes a plugin.

func DescribeTestPlugin

func DescribeTestPlugin(pluginName string) (desc *cliapi.PluginDescriptor, err error)

DescribeTestPlugin describes a test plugin.

func EnsureDistro

func EnsureDistro(repos *MultiRepo) error

EnsureDistro ensures that all the distro plugins are installed.

func EnsureTest

func EnsureTest(plugin *cliapi.PluginDescriptor, repos *MultiRepo) error

EnsureTest ensures the right version of the test is present for the plugin.

func EnsureTests

func EnsureTests(repos *MultiRepo, exclude ...string) error

EnsureTests ensures the plugin tests are installed.

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 GetCmd

GetCmd returns a cobra command for the plugin.

func HasPluginUpdate

func HasPluginUpdate(repo Repository, versionSelector VersionSelector, p *cliapi.PluginDescriptor) (update bool, version string, err error)

HasPluginUpdate tells whether the plugin descriptor has an update available in the given repository.

func HasUpdate

func HasUpdate(repo Repository) (update bool, version string, err error)

HasUpdate tells whether the core plugin has an update.

func InitializePlugin

func InitializePlugin(name string) error

InitializePlugin initializes the plugin configuration

func InstallAllMulti

func InstallAllMulti(repos *MultiRepo) error

InstallAllMulti installs all the plugins at the latest version in all the given repositories.

func InstallAllPlugins

func InstallAllPlugins(repo Repository) error

InstallAllPlugins plugins with the given version finder.

func InstallPlugin

func InstallPlugin(name, version string, repo Repository) error

InstallPlugin installs a plugin from the given repository.

func InstallTest

func InstallTest(pluginName, version string, repo Repository) error

InstallTest installs the test for the given plugin name

func IsDistributionSatisfied

func IsDistributionSatisfied(desc []*cliapi.PluginDescriptor) bool

IsDistributionSatisfied tells if a distribution is satisfied by the plugin list.

func ListPlugins

func ListPlugins(exclude ...string) (list []*cliapi.PluginDescriptor, err error)

ListPlugins returns the available plugins. This code will be removed as part of TKG-13912, and replaced with pluginmanager.AvailablePluginsFromLocalSource or pluginmanager.AvailablePlugins instead

func ListTestPlugins

func ListTestPlugins() (list []*cliapi.PluginDescriptor, err error)

ListTestPlugins returns the available test plugins.

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 NewCatalog

func NewCatalog() (*cliapi.Catalog, error)

NewCatalog creates an instance of Catalog.

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 TestCmd

func TestCmd(p *cliapi.PluginDescriptor) *cobra.Command

TestCmd returns a cobra command for the plugin.

func TestPluginPathFromPluginPath

func TestPluginPathFromPluginPath(pluginPath string) string

func Update

func Update(repo Repository) error

Update the core CLI.

func UpgradePlugin

func UpgradePlugin(name, version string, repo Repository) error

UpgradePlugin upgrades a plugin from the given repository.

func WithClientMetadata

func WithClientMetadata() func(ctx context.Context) context.Context

WithClientMetadata is an option to append CLI client metadata.

Types

type Arch

type Arch string

Arch represents a system architecture.

func BuildArch

func BuildArch() Arch

BuildArch returns compile time build arch or locates it.

func (Arch) IsWindows

func (a Arch) IsWindows() bool

IsWindows tells if an arch is windows.

type CmdMap

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

CmdMap is the map of command groups to plugins

type GCPBucketRepository

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

GCPBucketRepository is a artifact repository utilizing a GCP bucket.

func (*GCPBucketRepository) Describe

func (g *GCPBucketRepository) Describe(name string) (plugin Plugin, err error)

Describe a plugin.

func (*GCPBucketRepository) Fetch

func (g *GCPBucketRepository) Fetch(name, version string, arch Arch) ([]byte, error)

Fetch an artifact.

func (*GCPBucketRepository) FetchTest

func (g *GCPBucketRepository) FetchTest(name, version string, arch Arch) ([]byte, error)

FetchTest fetches a test artifact.

func (*GCPBucketRepository) List

func (g *GCPBucketRepository) List() (plugins []Plugin, err error)

List available plugins.

func (*GCPBucketRepository) Manifest

func (g *GCPBucketRepository) Manifest() (manifest Manifest, err error)

Manifest retrieves the manifest for a repository.

func (*GCPBucketRepository) Name

func (g *GCPBucketRepository) Name() string

Name of the repository.

func (*GCPBucketRepository) VersionSelector

func (g *GCPBucketRepository) VersionSelector() VersionSelector

VersionSelector returns the current default version finder.

type LocalRepository

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

LocalRepository is a artifact repository utilizing a local host os.

func (*LocalRepository) Describe

func (l *LocalRepository) Describe(name string) (plugin Plugin, err error)

Describe a plugin.

func (*LocalRepository) Fetch

func (l *LocalRepository) Fetch(name, version string, arch Arch) ([]byte, error)

Fetch an artifact.

func (*LocalRepository) FetchTest

func (l *LocalRepository) FetchTest(name, version string, arch Arch) ([]byte, error)

FetchTest fetches an artifact test.

func (*LocalRepository) List

func (l *LocalRepository) List() (plugins []Plugin, err error)

List available plugins.

func (*LocalRepository) Manifest

func (l *LocalRepository) Manifest() (manifest Manifest, err error)

Manifest returns the manifest for a local repository.

func (*LocalRepository) Name

func (l *LocalRepository) Name() string

Name of the repository.

func (*LocalRepository) VersionSelector

func (l *LocalRepository) VersionSelector() VersionSelector

VersionSelector returns the current default version finder.

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) Func

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

Func generates a usage func for cobra.

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 main usage.

type Manifest

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

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

	// Deprecated: Version of the root CLI.
	Version string `json:"version" yaml:"version"`

	// CoreVersion of the root CLI.
	CoreVersion string `json:"coreVersion" yaml:"coreVersion"`
}

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

func (*Manifest) GetCoreVersion

func (m *Manifest) GetCoreVersion() string

GetCoreVersion returns the core version in a backwards compatible manner.

type MultiRepo

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

MultiRepo is a multiple

func NewMultiRepo

func NewMultiRepo(repositories ...Repository) *MultiRepo

NewMultiRepo returns a new multirepo.

func (*MultiRepo) AddRepository

func (m *MultiRepo) AddRepository(repo Repository)

AddRepository to known.

func (*MultiRepo) Find

func (m *MultiRepo) Find(name string) (r Repository, err error)

Find a repository for the given plugin name.

func (*MultiRepo) GetRepository

func (m *MultiRepo) GetRepository(name string) (Repository, error)

GetRepository returns a repository.

func (*MultiRepo) ListPlugins

func (m *MultiRepo) ListPlugins() (mp map[string][]Plugin, err error)

ListPlugins across the repositories.

func (*MultiRepo) RemoveRepository

func (m *MultiRepo) RemoveRepository(name string)

RemoveRepository removes a repo.

type Option

type Option func(o *optionsConfig)

Option is a filesystem store option.

func WithDistro

func WithDistro(distro cliapi.Distro) Option

WithDistro sets the distro that should be installed with the CLI

func WithGCPBucket

func WithGCPBucket(name string) Option

WithGCPBucket sets the gcp bucket to use for the artifact repository.

func WithGCPRootPath

func WithGCPRootPath(path string) Option

WithGCPRootPath sets the gcp bucket root path to use for the artifact repository.

func WithName

func WithName(name string) Option

WithName sets the name

func WithPluginRoot

func WithPluginRoot(root string) Option

WithPluginRoot sets the root which directory plugins live in.

func WithVersionSelector

func WithVersionSelector(finder VersionSelector) Option

WithVersionSelector sets the version finder.

type Plugin

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

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

Plugin is an installable CLI plugin.

func (*Plugin) FindVersion

func (p *Plugin) FindVersion(selector VersionSelector) string

FindVersion finds the version using the version selector.

type PluginState

type PluginState struct {
	Auth string `json:"auth" yaml:"auth"`
}

PluginState is state that will be passed to plugins.

func ReadPluginStateFromPath

func ReadPluginStateFromPath(p string) (*PluginState, error)

ReadPluginStateFromPath read states from a path on disk.

type Repository

type Repository interface {
	// List available plugins.
	List() ([]Plugin, error)

	// Describe a plugin.
	Describe(name string) (Plugin, error)

	// Fetch an artifact.
	Fetch(name, version string, arch Arch) ([]byte, error)

	// Fetch an artifact test.
	FetchTest(pluginName, version string, arch Arch) ([]byte, error)

	// Name of the repository.
	Name() string

	// Manifest retrieves the manifest for the repo.
	Manifest() (Manifest, error)

	// VersionSelector returns the version finder.
	VersionSelector() VersionSelector
}

Repository is a remote repository containing plugin artifacts.

func HasPluginUpdateIn

func HasPluginUpdateIn(repos *MultiRepo, p *cliapi.PluginDescriptor) (update bool, repo Repository, version string, err error)

HasPluginUpdateIn checks if the plugin has an update in any of the given repositories.

func LoadRepositories

func LoadRepositories(c *configapi.ClientConfig) []Repository

LoadRepositories loads the repositories from the config file along with the known repositories.

func NewDefaultRepository

func NewDefaultRepository() Repository

NewDefaultRepository returns the default repository.

func NewGCPBucketRepository

func NewGCPBucketRepository(options ...Option) Repository

NewGCPBucketRepository returns a new GCP bucket repository.

func NewLocalRepository

func NewLocalRepository(name, localPath string, options ...Option) Repository

NewLocalRepository returns a new local repository.

type Runner

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

Runner is a plugin runner.

func NewRunner

func NewRunner(name, pluginAbsPath string, args []string, options ...Option) *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.

func LoadVersionSelector

func LoadVersionSelector(selectorType configapi.VersionSelectorLevel) (versionSelector VersionSelector)

LoadVersionSelector will return the correct VersionSelector for a VersionSelectorLevel

Jump to

Keyboard shortcuts

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