From f07d67cf9a059f1a6c529a1319449665b86586a6 Mon Sep 17 00:00:00 2001 From: Dharma-09 Date: Fri, 20 Sep 2024 12:22:20 -0300 Subject: [PATCH] Custom error for command not found --- ee/allowedcmd/cmd.go | 7 +++++-- ee/allowedcmd/cmd_linux.go | 4 +--- ee/tables/homebrew/upgradeable.go | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ee/allowedcmd/cmd.go b/ee/allowedcmd/cmd.go index eb8afa7c2..30866b305 100644 --- a/ee/allowedcmd/cmd.go +++ b/ee/allowedcmd/cmd.go @@ -12,6 +12,7 @@ import ( "os" "os/exec" "path/filepath" + "errors" ) type AllowedCommand func(ctx context.Context, arg ...string) (*exec.Cmd, error) @@ -20,6 +21,8 @@ func newCmd(ctx context.Context, fullPathToCmd string, arg ...string) *exec.Cmd return exec.CommandContext(ctx, fullPathToCmd, arg...) //nolint:forbidigo // This is our approved usage of exec.CommandContext } +var ErrCommandNotFound = errors.New("command not found") + func validatedCommand(ctx context.Context, knownPath string, arg ...string) (*exec.Cmd, error) { knownPath = filepath.Clean(knownPath) @@ -31,7 +34,7 @@ func validatedCommand(ctx context.Context, knownPath string, arg ...string) (*ex // We expect to know the exact location for allowlisted commands on all // OSes except for a few Linux distros. if !allowSearchPath() { - return nil, fmt.Errorf("not found: %s", knownPath) + return nil, fmt.Errorf("%w: %s",ErrCommandNotFound, knownPath) } cmdName := filepath.Base(knownPath) @@ -39,7 +42,7 @@ func validatedCommand(ctx context.Context, knownPath string, arg ...string) (*ex return newCmd(ctx, foundPath, arg...), nil } - return nil, fmt.Errorf("%s not found at %s and could not be located elsewhere", cmdName, knownPath) + return nil, fmt.Errorf("%w: %s and could not be located elsewhere", ErrCommandNotFound, knownPath) } func allowSearchPath() bool { diff --git a/ee/allowedcmd/cmd_linux.go b/ee/allowedcmd/cmd_linux.go index 2fbaac448..cb346ffa1 100644 --- a/ee/allowedcmd/cmd_linux.go +++ b/ee/allowedcmd/cmd_linux.go @@ -13,12 +13,10 @@ func Apt(ctx context.Context, arg ...string) (*exec.Cmd, error) { return validatedCommand(ctx, "/usr/bin/apt", arg...) } -var ErrHomebrewNotFound = errors.New("homebrew not found") - func Brew(ctx context.Context, arg ...string) (*exec.Cmd, error) { validatedCmd, err := validatedCommand(ctx, "/home/linuxbrew/.linuxbrew/bin/brew", arg...) if err != nil { - return nil, ErrHomebrewNotFound + return nil, nil } validatedCmd.Env = append(validatedCmd.Environ(), "HOMEBREW_NO_AUTO_UPDATE=1") diff --git a/ee/tables/homebrew/upgradeable.go b/ee/tables/homebrew/upgradeable.go index 2982127eb..612fb115d 100644 --- a/ee/tables/homebrew/upgradeable.go +++ b/ee/tables/homebrew/upgradeable.go @@ -46,7 +46,7 @@ func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) ( cmd, err := allowedcmd.Brew(ctx) if err != nil { - if errors.Is(err, allowedcmd.ErrHomebrewNotFound) { + if errors.Is(err, allowedcmd.ErrCommandNotFound) { // No data, no error return nil, nil }