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