github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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 main
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  	"strings"
    22  
    23  	"github.com/gosuri/uitable"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func newPluginListCmd(out io.Writer) *cobra.Command {
    28  	cmd := &cobra.Command{
    29  		Use:     "list",
    30  		Aliases: []string{"ls"},
    31  		Short:   "list installed Helm plugins",
    32  		RunE: func(cmd *cobra.Command, args []string) error {
    33  			debug("pluginDirs: %s", settings.PluginsDirectory)
    34  			plugins, err := findPlugins(settings.PluginsDirectory)
    35  			if err != nil {
    36  				return err
    37  			}
    38  
    39  			table := uitable.New()
    40  			table.AddRow("NAME", "VERSION", "DESCRIPTION")
    41  			for _, p := range plugins {
    42  				table.AddRow(p.Metadata.Name, p.Metadata.Version, p.Metadata.Description)
    43  			}
    44  			fmt.Fprintln(out, table)
    45  			return nil
    46  		},
    47  	}
    48  	return cmd
    49  }
    50  
    51  // Provide dynamic auto-completion for plugin names
    52  func compListPlugins(toComplete string) []string {
    53  	var pNames []string
    54  	plugins, err := findPlugins(settings.PluginsDirectory)
    55  	if err == nil {
    56  		for _, p := range plugins {
    57  			if strings.HasPrefix(p.Metadata.Name, toComplete) {
    58  				pNames = append(pNames, p.Metadata.Name)
    59  			}
    60  		}
    61  	}
    62  	return pNames
    63  }