github.com/kcmvp/gob@v1.0.17/cmd/gbc/command/plugin.go (about)

     1  /*
     2  Copyright © 2023 NAME HERE <EMAIL ADDRESS>
     3  */
     4  package command
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"strings"
    10  
    11  	"github.com/fatih/color"
    12  	"github.com/jedib0t/go-pretty/v6/table"
    13  	"github.com/jedib0t/go-pretty/v6/text"
    14  	"github.com/kcmvp/gob/cmd/gbc/artifact"
    15  	"github.com/samber/lo"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  // alias is the tool alias
    20  var alias string
    21  
    22  // command is the tool command
    23  var command string
    24  
    25  // Install the specified tool as gob plugin
    26  func install(_ *cobra.Command, args ...string) error {
    27  	plugin, err := artifact.NewPlugin(args[0])
    28  	if err != nil {
    29  		return err
    30  	}
    31  	plugin.Alias = alias
    32  	plugin.Alias = command
    33  	return artifact.CurProject().InstallPlugin(plugin)
    34  }
    35  
    36  func list(_ *cobra.Command, _ ...string) error {
    37  	plugins := artifact.CurProject().Plugins()
    38  	ct := table.Table{}
    39  	ct.SetTitle("Installed Plugins")
    40  	ct.AppendRow(table.Row{"Command", "Plugin"})
    41  	style := table.StyleDefault
    42  	style.Options.DrawBorder = true
    43  	style.Options.SeparateRows = true
    44  	style.Options.SeparateColumns = true
    45  	style.Title.Align = text.AlignCenter
    46  	style.HTML.CSSClass = table.DefaultHTMLCSSClass
    47  	ct.SetStyle(style)
    48  	rows := lo.Map(plugins, func(plugin artifact.Plugin, index int) table.Row {
    49  		return table.Row{plugin.Alias, plugin.Url}
    50  	})
    51  	ct.AppendRows(rows)
    52  	fmt.Println(ct.Render())
    53  	return nil
    54  }
    55  
    56  var pluginCmdAction = []Action{
    57  	{
    58  		A: "list",
    59  		B: list,
    60  		C: "list all setup plugins",
    61  	},
    62  	{
    63  		A: "install",
    64  		B: install,
    65  		C: "install a plugin. `gbc plugin install <plugin url>`",
    66  	},
    67  }
    68  
    69  // pluginCmd represents the plugin command
    70  var pluginCmd = &cobra.Command{
    71  	Use:   "plugin",
    72  	Short: "Install a new plugin or list installed plugins",
    73  	Long: color.BlueString(`
    74  Install a new plugin or list installed plugins
    75  you can update the plugin by edit gob.yaml directly
    76  `),
    77  	Args: func(cmd *cobra.Command, args []string) error {
    78  		if err := MinimumNArgs(1)(cmd, args); err != nil {
    79  			return err
    80  		}
    81  		if !lo.Contains(lo.Map(pluginCmdAction, func(item Action, _ int) string {
    82  			return item.A
    83  		}), args[0]) {
    84  			return errors.New(color.RedString("invalid argument %s", args[0]))
    85  		}
    86  		if "install" == args[0] && (len(args) < 2 || strings.TrimSpace(args[1]) == "") {
    87  			return errors.New(color.RedString("miss the plugin url"))
    88  		}
    89  		return nil
    90  	},
    91  	ValidArgs: lo.Map(pluginCmdAction, func(item Action, _ int) string {
    92  		return item.A
    93  	}),
    94  	RunE: func(cmd *cobra.Command, args []string) error {
    95  		cmdAction, _ := lo.Find(pluginCmdAction, func(cmdAction Action) bool {
    96  			return cmdAction.A == args[0]
    97  		})
    98  		return cmdAction.B(cmd, args[1:]...)
    99  	},
   100  }
   101  
   102  func pluginExample() string {
   103  	format := fmt.Sprintf("  %%-%ds %%s", pluginCmd.NamePadding())
   104  	return strings.Join(lo.Map(pluginCmdAction, func(action Action, index_ int) string {
   105  		return fmt.Sprintf(format, action.A, action.C)
   106  	}), "\n")
   107  }
   108  
   109  func init() {
   110  	// init pluginCmd
   111  	pluginCmd.Example = pluginExample()
   112  	pluginCmd.SetUsageTemplate(usageTemplate())
   113  	pluginCmd.SetFlagErrorFunc(func(command *cobra.Command, err error) error {
   114  		return lo.IfF(err != nil, func() error {
   115  			return fmt.Errorf(color.RedString(err.Error()))
   116  		}).Else(nil)
   117  	})
   118  	pluginCmd.Flags().StringVarP(&alias, "alias", "a", "", "alias of the tool")
   119  	pluginCmd.Flags().StringVarP(&command, "command", "c", "", "default command of this tool")
   120  
   121  	rootCmd.AddCommand(pluginCmd)
   122  }