Skip to content

Commit

Permalink
Merge pull request #28 from kcmvp/plugin-refactor
Browse files Browse the repository at this point in the history
Plugin refactor
  • Loading branch information
kcmvp authored Dec 26, 2023
2 parents 0b8f737 + a5c6acb commit 02b641f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 33 deletions.
4 changes: 3 additions & 1 deletion cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func Execute(cmd *cobra.Command, args ...string) error {
}()
// Wait for the command to finish
err = exeCmd.Wait()
fmt.Printf("%s report is generated at %s \n", args[0], filepath.Join(internal.CurProject().Target(), fmt.Sprintf("%s.log", args[0])))
if err != nil {
color.Red("%s report is generated at %s \n", args[0], filepath.Join(internal.CurProject().Target(), fmt.Sprintf("%s.log", args[0])))
}
return err
}
if action, ok := lo.Find(builtinActions, func(action CmdAction) bool {
Expand Down
1 change: 1 addition & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func exec(execution internal.Execution, cmd *cobra.Command, args ...string) erro
if err := action.Execute(cmd, arg); err != nil {
return errors.New(color.RedString("failed to %s the project \n", arg))
}
color.Green("execute %s successfully", arg)
}
return nil
}
Expand Down
65 changes: 40 additions & 25 deletions cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ var alias string
var command string

// Install the specified tool as gob plugin
func install(args ...string) (string, error) {
func install(_ *cobra.Command, args ...string) error {
var ver string
var err error
url := args[0]
url := args[1]
parts := strings.Split(url, "@")
if len(parts) != 2 || strings.HasPrefix(parts[1], "latest") {
ver, err = action.LatestVersion(parts[0], "")
if err != nil {
return ver, fmt.Errorf("please use specific version of the tool")
return fmt.Errorf("please use specific version of the tool")
}
url = fmt.Sprintf("%s@%s", parts[0], ver)
}
Expand All @@ -41,10 +41,10 @@ func install(args ...string) (string, error) {
color.Yellow("Plugin %s exists", url)
err = nil
}
return ver, err
return err
}

func list() {
func list(_ *cobra.Command, _ ...string) error {
plugins := internal.CurProject().Plugins()
ct := table.Table{}
ct.SetTitle("Installed Plugins")
Expand All @@ -61,41 +61,56 @@ func list() {
})
ct.AppendRows(rows)
fmt.Println(ct.Render())
return nil
}

// pluginCmd represents the plugin command
var pluginCmd = &cobra.Command{
Use: "plugin",
Short: "Install, update or list plugins",
Long: `Install, update or list plugins`,
Run: func(cmd *cobra.Command, args []string) {
list()
var pluginCmdAction = []action.CmdAction{
{
A: "list",
B: list,
},
{
A: "install",
B: install,
},
}

// installPluginCmd represents the plugin install command
var installPluginCmd = &cobra.Command{
Use: "install",
Short: "Install a tool as gob plugin",
Long: `Install a tool as gob plugin`,
// pluginCmd represents the plugin command
var pluginCmd = &cobra.Command{
Use: "plugin",
Short: "Install a new plugin or list installed plugins",
Long: `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 := cobra.ExactArgs(1)(cmd, args); err != nil {
return fmt.Errorf(color.RedString(err.Error()))
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
return err
}
if !lo.Contains(lo.Map(pluginCmdAction, func(item action.CmdAction, _ int) string {
return item.A
}), args[0]) {
return fmt.Errorf("invalid argument %s", args[0])
}
if "install" == args[0] && (len(args) < 2 || strings.TrimSpace(args[1]) == "") {
return errors.New("miss the mandatory tool url")
}
return nil
},
ValidArgs: lo.Map(pluginCmdAction, func(item action.CmdAction, _ int) string {
return item.A
}),
RunE: func(cmd *cobra.Command, args []string) error {
_, err := install(args...)
return err
cmdAction, _ := lo.Find(pluginCmdAction, func(cmdAction action.CmdAction) bool {
return cmdAction.A == args[0]
})
return cmdAction.B(cmd, args...)
},
}

func init() {
// init pluginCmd
builderCmd.AddCommand(pluginCmd)

// init installPluginCmd
pluginCmd.AddCommand(installPluginCmd)
installPluginCmd.Flags().StringVarP(&alias, "alias", "a", "", "alias of the tool")
installPluginCmd.Flags().StringVarP(&command, "command", "c", "", "default command of this tool")
pluginCmd.Flags().StringVarP(&alias, "alias", "a", "", "alias of the tool")
pluginCmd.Flags().StringVarP(&command, "command", "c", "", "default command of this tool")
}
44 changes: 38 additions & 6 deletions cmd/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestInstallPlugin(t *testing.T) {
builderCmd.SetArgs([]string{"plugin", "install", fiximports, "-a=lint", "-c=lint-run"})
err = builderCmd.Execute()
assert.NoError(t, err)
builderCmd.SetArgs([]string{"plugin"})
builderCmd.SetArgs([]string{"plugin", "list"})
err = builderCmd.Execute()
assert.NoError(t, err)
plugin, ok = lo.Find(internal.CurProject().Plugins(), func(item lo.Tuple4[string, string, string, string]) bool {
Expand All @@ -77,7 +77,7 @@ func TestInstallPlugin(t *testing.T) {
}

func TestInstallPluginWithVersion(t *testing.T) {
ver, err := action.LatestVersion("github.com/hhatto/gocloc/cmd/gocloc", "")
_, err := action.LatestVersion("github.com/hhatto/gocloc/cmd/gocloc", "")
assert.NoError(t, err)
tests := []struct {
name string
Expand All @@ -90,11 +90,43 @@ func TestInstallPluginWithVersion(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ver1, err1 := install(test.url)
err1 := install(nil, "", test.url)
assert.True(t, test.wantErr == (err1 != nil))
if err1 == nil {
assert.Equal(t, ver, ver1)
}
})
}
}

func TestPluginArgs(t *testing.T) {
tests := []struct {
name string
args []string
wantErr bool
}{
{
"no args",
[]string{},
true,
},
{
"first not match",
[]string{"def", "list"},
true,
},
{
"install without url",
[]string{"install", ""},
true,
},
{
"install with url",
[]string{"install", v6},
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := pluginCmd.Args(nil, test.args)
assert.True(t, test.wantErr == (err != nil))
})
}
}
1 change: 0 additions & 1 deletion gob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ exec:
- lint
- test
pre-push-hook:
- lint
- test
plugins:
gocloc:
Expand Down

0 comments on commit 02b641f

Please sign in to comment.