github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/plugin_list.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package cmd
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  
    22  	"github.com/gosuri/uitable"
    23  	"github.com/spf13/cobra"
    24  
    25  	"helm.sh/helm/v3/pkg/plugin"
    26  )
    27  
    28  func newPluginListCmd(out io.Writer) *cobra.Command {
    29  	cmd := &cobra.Command{
    30  		Use:               "list",
    31  		Aliases:           []string{"ls"},
    32  		Short:             "list installed Helm plugins",
    33  		ValidArgsFunction: noCompletions,
    34  		RunE: func(cmd *cobra.Command, args []string) error {
    35  			debug("pluginDirs: %s", settings.PluginsDirectory)
    36  			plugins, err := plugin.FindPlugins(settings.PluginsDirectory)
    37  			if err != nil {
    38  				return err
    39  			}
    40  
    41  			table := uitable.New()
    42  			table.AddRow("NAME", "VERSION", "DESCRIPTION")
    43  			for _, p := range plugins {
    44  				table.AddRow(p.Metadata.Name, p.Metadata.Version, p.Metadata.Description)
    45  			}
    46  			fmt.Fprintln(out, table)
    47  			return nil
    48  		},
    49  	}
    50  	return cmd
    51  }
    52  
    53  // Returns all plugins from plugins, except those with names matching ignoredPluginNames
    54  func filterPlugins(plugins []*plugin.Plugin, ignoredPluginNames []string) []*plugin.Plugin {
    55  	// if ignoredPluginNames is nil, just return plugins
    56  	if ignoredPluginNames == nil {
    57  		return plugins
    58  	}
    59  
    60  	var filteredPlugins []*plugin.Plugin
    61  	for _, plugin := range plugins {
    62  		found := false
    63  		for _, ignoredName := range ignoredPluginNames {
    64  			if plugin.Metadata.Name == ignoredName {
    65  				found = true
    66  				break
    67  			}
    68  		}
    69  		if !found {
    70  			filteredPlugins = append(filteredPlugins, plugin)
    71  		}
    72  	}
    73  
    74  	return filteredPlugins
    75  }
    76  
    77  // Provide dynamic auto-completion for plugin names
    78  func compListPlugins(toComplete string, ignoredPluginNames []string) []string {
    79  	var pNames []string
    80  	plugins, err := plugin.FindPlugins(settings.PluginsDirectory)
    81  	if err == nil && len(plugins) > 0 {
    82  		filteredPlugins := filterPlugins(plugins, ignoredPluginNames)
    83  		for _, p := range filteredPlugins {
    84  			pNames = append(pNames, fmt.Sprintf("%s\t%s", p.Metadata.Name, p.Metadata.Usage))
    85  		}
    86  	}
    87  	return pNames
    88  }