From fb9872cdc751be1c8e6ddbab03eefbead43c3594 Mon Sep 17 00:00:00 2001 From: Chris North Date: Tue, 28 Nov 2023 14:35:27 -0800 Subject: [PATCH] feat: add `zarf package remove/inspect` completion for cluster sources (#2151) ## Description - Added `zarf package remove` shell completion ability - Added `zarf package inspect` shell completion ability - Added `zarf package list` alias "ls" (since ls is so commonly used) - Added `zarf package remove` alias "rm" (since rm is so commonly used) ## Related Issue Fixes #2150 Partial: #2154 (inspect only) ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [X] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [X] Test, docs, adr added or updated as needed - [X] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow) followed --------- Co-authored-by: Wayne Starr --- src/cmd/package.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/cmd/package.go b/src/cmd/package.go index 0d0d003a6f..c7c2042550 100644 --- a/src/cmd/package.go +++ b/src/cmd/package.go @@ -144,11 +144,12 @@ var packageInspectCmd = &cobra.Command{ message.Fatalf(err, lang.CmdPackageInspectErr, err.Error()) } }, + ValidArgsFunction: getPackageCompletionArgs, } var packageListCmd = &cobra.Command{ Use: "list", - Aliases: []string{"l"}, + Aliases: []string{"l", "ls"}, Short: lang.CmdPackageListShort, Run: func(cmd *cobra.Command, args []string) { // Get all the deployed packages @@ -188,7 +189,7 @@ var packageListCmd = &cobra.Command{ var packageRemoveCmd = &cobra.Command{ Use: "remove { PACKAGE_SOURCE | PACKAGE_NAME } --confirm", - Aliases: []string{"u"}, + Aliases: []string{"u", "rm"}, Args: cobra.MaximumNArgs(1), Short: lang.CmdPackageRemoveShort, Run: func(cmd *cobra.Command, args []string) { @@ -203,6 +204,7 @@ var packageRemoveCmd = &cobra.Command{ message.Fatalf(err, lang.CmdPackageRemoveErr, err.Error()) } }, + ValidArgsFunction: getPackageCompletionArgs, } var packagePublishCmd = &cobra.Command{ @@ -300,6 +302,24 @@ func identifyAndFallbackToClusterSource() (src sources.PackageSource) { return src } +func getPackageCompletionArgs(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + var pkgCandidates []string + + c, err := cluster.NewCluster() + if err != nil { + return pkgCandidates, cobra.ShellCompDirectiveDefault + } + + // Get all the deployed packages + deployedZarfPackages, _ := c.GetDeployedZarfPackages() + // Populate list of package names + for _, pkg := range deployedZarfPackages { + pkgCandidates = append(pkgCandidates, pkg.Name) + } + + return pkgCandidates, cobra.ShellCompDirectiveDefault +} + func init() { v := common.InitViper()