github.com/oam-dev/kubevela@v1.9.11/pkg/cmd/completion.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cmd
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  
    23  	"github.com/spf13/cobra"
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    30  	"github.com/oam-dev/kubevela/pkg/multicluster"
    31  	"github.com/oam-dev/kubevela/pkg/oam"
    32  )
    33  
    34  func listObjectNamesForCompletion(ctx context.Context, f Factory, gvk schema.GroupVersionKind, listOptions []client.ListOption, toComplete string) ([]string, cobra.ShellCompDirective) {
    35  	uns := &unstructured.UnstructuredList{}
    36  	uns.SetGroupVersionKind(gvk)
    37  	if err := f.Client().List(ctx, uns, listOptions...); err != nil {
    38  		return nil, cobra.ShellCompDirectiveError
    39  	}
    40  	var candidates []string
    41  	for _, obj := range uns.Items {
    42  		if name := obj.GetName(); strings.HasPrefix(name, toComplete) {
    43  			candidates = append(candidates, name)
    44  		}
    45  	}
    46  	return candidates, cobra.ShellCompDirectiveNoFileComp
    47  }
    48  
    49  // GetNamespacesForCompletion auto-complete the namespace
    50  func GetNamespacesForCompletion(ctx context.Context, f Factory, toComplete string) ([]string, cobra.ShellCompDirective) {
    51  	return listObjectNamesForCompletion(ctx, f, corev1.SchemeGroupVersion.WithKind("Namespace"), nil, toComplete)
    52  }
    53  
    54  // GetServiceAccountForCompletion auto-complete serviceaccount
    55  func GetServiceAccountForCompletion(ctx context.Context, f Factory, namespace string, toComplete string) ([]string, cobra.ShellCompDirective) {
    56  	var options []client.ListOption
    57  	if namespace != "" {
    58  		options = append(options, client.InNamespace(namespace))
    59  	}
    60  	return listObjectNamesForCompletion(ctx, f, corev1.SchemeGroupVersion.WithKind("ServiceAccount"), options, toComplete)
    61  }
    62  
    63  // GetRevisionForCompletion auto-complete the revision according to the application
    64  func GetRevisionForCompletion(ctx context.Context, f Factory, appName string, namespace string, toComplete string) ([]string, cobra.ShellCompDirective) {
    65  	var options []client.ListOption
    66  	if namespace != "" {
    67  		options = append(options, client.InNamespace(namespace))
    68  	}
    69  	if appName != "" {
    70  		options = append(options, client.MatchingLabels{oam.LabelAppName: appName})
    71  	}
    72  	return listObjectNamesForCompletion(ctx, f, v1beta1.SchemeGroupVersion.WithKind(v1beta1.ApplicationRevisionKind), options, toComplete)
    73  }
    74  
    75  // GetApplicationsForCompletion auto-complete application
    76  func GetApplicationsForCompletion(ctx context.Context, f Factory, namespace string, toComplete string) ([]string, cobra.ShellCompDirective) {
    77  	var options []client.ListOption
    78  	if namespace != "" {
    79  		options = append(options, client.InNamespace(namespace))
    80  	}
    81  	return listObjectNamesForCompletion(ctx, f, v1beta1.SchemeGroupVersion.WithKind(v1beta1.ApplicationKind), options, toComplete)
    82  }
    83  
    84  // GetClustersForCompletion auto-complete the cluster
    85  func GetClustersForCompletion(ctx context.Context, f Factory, toComplete string) ([]string, cobra.ShellCompDirective) {
    86  	clusters, err := multicluster.NewClusterClient(f.Client()).List(ctx)
    87  	if err != nil {
    88  		return nil, cobra.ShellCompDirectiveError
    89  	}
    90  	var candidates []string
    91  	for _, obj := range clusters.Items {
    92  		if name := obj.GetName(); strings.HasPrefix(name, toComplete) {
    93  			candidates = append(candidates, name)
    94  		}
    95  	}
    96  	return candidates, cobra.ShellCompDirectiveNoFileComp
    97  }