Skip to content

Commit

Permalink
Add dynamic shell completion for unmanaged-cluster
Browse files Browse the repository at this point in the history
Related to vmware-tanzu/tanzu-framework#701

Signed-off-by: Marc Khouzam <kmarc@vmware.com>
  • Loading branch information
marckhouzam committed Jun 22, 2022
1 parent c097aeb commit 56b9207
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cli/cmd/plugin/unmanaged-cluster/completion/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2022 VMware Tanzu Community Edition contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package completion

import (
"fmt"

"github.com/spf13/cobra"

"github.com/vmware-tanzu/community-edition/cli/cmd/plugin/unmanaged-cluster/cluster"
"github.com/vmware-tanzu/community-edition/cli/cmd/plugin/unmanaged-cluster/cmd"
"github.com/vmware-tanzu/community-edition/cli/cmd/plugin/unmanaged-cluster/config"
logger "github.com/vmware-tanzu/community-edition/cli/cmd/plugin/unmanaged-cluster/log"
"github.com/vmware-tanzu/community-edition/cli/cmd/plugin/unmanaged-cluster/tanzu"
)

const clusterAnyStatus = ""

func Setup(rootCmd *cobra.Command) {
// The below commands accept the names of existing clusters
cmd.DeleteCmd.ValidArgsFunction = completeExistingClusterName(clusterAnyStatus)
cmd.StartCmd.ValidArgsFunction = completeExistingClusterName(cluster.StatusStopped)
cmd.StopCmd.ValidArgsFunction = completeExistingClusterName(cluster.StatusRunning)

// The below commands should not provide any shell completion choices nor file names
cmd.ConfigureCmd.ValidArgsFunction = cobra.NoFileCompletions
cmd.CreateCmd.ValidArgsFunction = cobra.NoFileCompletions
cmd.ListCmd.ValidArgsFunction = cobra.NoFileCompletions
}

// completeExistingClusterName returns a completion function that lists all clusters that match
// the specified clusterStatus. If clusterStatus is clusterAnyStatus, all clusters will match.
func completeExistingClusterName(clusterStatus string) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
// No more args accepted
return nil, cobra.ShellCompDirectiveNoFileComp
}

log := logger.NewLogger(false, 0) // No need for logs
tClient := tanzu.New(log)

// Find clusters and their status
clusters, err := tClient.List()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var clusterNames []string
for _, c := range clusters {
// retrieve status from provider
conf := &config.UnmanagedClusterConfig{
Provider: c.Provider,
}
clusterManager := cluster.NewClusterManager(conf)
kc, err := clusterManager.Get(c.Name)

if err != nil {
continue
}

// Only complete clusters of the right status, or if the status is unknown.
// A value of clusterAnyStatus indicates a request for all clusters.
if kc.Status == clusterStatus || kc.Status == cluster.StatusUnknown || clusterStatus == clusterAnyStatus {
clusterNames = append(clusterNames, fmt.Sprintf("%s\tstatus: %s - provider: %s", c.Name, kc.Status, c.Provider))
}
}
return clusterNames, cobra.ShellCompDirectiveNoFileComp
}
}
2 changes: 2 additions & 0 deletions cli/cmd/plugin/unmanaged-cluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/vmware-tanzu/community-edition/cli/cmd/plugin"
"github.com/vmware-tanzu/community-edition/cli/cmd/plugin/unmanaged-cluster/cmd"
"github.com/vmware-tanzu/community-edition/cli/cmd/plugin/unmanaged-cluster/completion"
)

var description = `Deploy and manage single-node, static, Tanzu clusters.`
Expand Down Expand Up @@ -48,6 +49,7 @@ func main() {
)

cmd.SetupRootCommand(p.Cmd)
completion.Setup(p.Cmd)

if err := p.Execute(); err != nil {
os.Exit(1)
Expand Down

0 comments on commit 56b9207

Please sign in to comment.