github.com/Azure/draft-classic@v0.16.0/cmd/draft/plugin_list.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/gosuri/uitable"
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/Azure/draft/pkg/draft/draftpath"
    11  )
    12  
    13  type pluginListCmd struct {
    14  	home draftpath.Home
    15  	out  io.Writer
    16  }
    17  
    18  func newPluginListCmd(out io.Writer) *cobra.Command {
    19  	pcmd := &pluginListCmd{out: out}
    20  	cmd := &cobra.Command{
    21  		Use:   "list",
    22  		Short: "list installed Draft plugins",
    23  		RunE: func(cmd *cobra.Command, args []string) error {
    24  			pcmd.home = draftpath.Home(homePath())
    25  			return pcmd.run()
    26  		},
    27  	}
    28  	return cmd
    29  }
    30  
    31  func (pcmd *pluginListCmd) run() error {
    32  	pluginDirs := pluginDirPath(pcmd.home)
    33  
    34  	debug("looking for plugins at: %s", pluginDirs)
    35  	plugins, err := findPlugins(pluginDirs)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	if len(plugins) == 0 {
    41  		fmt.Fprintln(pcmd.out, "No plugins found")
    42  		return nil
    43  	}
    44  
    45  	table := uitable.New()
    46  	table.AddRow("NAME", "VERSION", "DESCRIPTION")
    47  	for _, p := range plugins {
    48  		table.AddRow(p.Metadata.Name, p.Metadata.Version, p.Metadata.Description)
    49  	}
    50  	fmt.Fprintln(pcmd.out, table)
    51  	return nil
    52  }