diff --git a/cmd/action/action.go b/cmd/action/action.go index e61afcf..235ffe6 100644 --- a/cmd/action/action.go +++ b/cmd/action/action.go @@ -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 { diff --git a/cmd/exec.go b/cmd/exec.go index 7b59953..6cde2a2 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -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 } diff --git a/cmd/plugin.go b/cmd/plugin.go index ed9f617..8ab6b31 100644 --- a/cmd/plugin.go +++ b/cmd/plugin.go @@ -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) } @@ -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") @@ -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") } diff --git a/cmd/plugin_test.go b/cmd/plugin_test.go index 3ddd60a..f11ea7a 100644 --- a/cmd/plugin_test.go +++ b/cmd/plugin_test.go @@ -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 { @@ -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 @@ -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)) }) } } diff --git a/gob.yaml b/gob.yaml index c44c8fc..da00f56 100644 --- a/gob.yaml +++ b/gob.yaml @@ -4,7 +4,6 @@ exec: - lint - test pre-push-hook: - - lint - test plugins: gocloc: