Skip to content

Commit

Permalink
Use Run instead of RunE to avoid showing the command help when the co…
Browse files Browse the repository at this point in the history
…mmand fails.

Signed-off-by: Daniel Ortiz <[email protected]>
  • Loading branch information
taik0 committed Aug 21, 2024
1 parent 220d6bb commit 7314cfa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
13 changes: 10 additions & 3 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func indirectRequires(goSum string) (map[string]struct{}, error) {
filename := filepath.Join(dir, "go.mod")
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("read go.mod: %w", err)
return nil, fmt.Errorf("reading go.mod: %w", err)
}

f, err := modfile.Parse(filename, data, nil)
if err != nil {
return nil, fmt.Errorf("parse go.mod: %w", err)
return nil, fmt.Errorf("parsing go.mod: %w", err)
}

indirects := map[string]struct{}{}
Expand All @@ -40,7 +40,14 @@ func indirectRequires(goSum string) (map[string]struct{}, error) {
// https://github.com/golang/go/issues/68045
var localDescriber = plugin.Local

func pluginFunc(cmd *cobra.Command, _ []string) error {
func pluginFunc(cmd *cobra.Command, args []string) {
if err := pluginFuncErr(cmd, args); err != nil {
cmd.Println(err)
os.Exit(1)
}
}

func pluginFuncErr(cmd *cobra.Command, _ []string) error {
f, err := os.Open(goSum)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require"
)

func Test_pluginFunc(t *testing.T) {
func Test_pluginFuncErr(t *testing.T) {
var buf bytes.Buffer
cmd := &cobra.Command{}
cmd.SetOutput(&buf)
Expand Down Expand Up @@ -78,7 +78,7 @@ go get golang.org/x/[email protected]
gogetEnabled = tc.fix
defer func() { gogetEnabled = fix }()

err := pluginFunc(cmd, nil)
err := pluginFuncErr(cmd, nil)
if tc.err != "" {
require.EqualError(t, err, tc.err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion root.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var (
Use: "check-plugin",
Short: "Checks your plugin dependencies are compatible.",
Long: "Checks your plugin dependencies are compatible and proposes commands to update your dependencies.",
RunE: pluginFunc,
Run: pluginFunc,
Example: "krakend check-plugin -g 1.19.0 -s ./go.sum -f",
}

Expand Down

0 comments on commit 7314cfa

Please sign in to comment.