Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
Signed-off-by: nitishfy <[email protected]>
  • Loading branch information
nitishfy committed Dec 26, 2024
1 parent 4b7da35 commit fde718d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 61 deletions.
62 changes: 42 additions & 20 deletions cmd/argocd/commands/plugin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import (
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -42,8 +41,48 @@ func NewDefaultPluginHandler(validPrefixes []string) *DefaultPluginHandler {
}
}

// HandleCommandExecutionError processes the error returned from executing the command.
// It handles both standard Argo CD commands and plugin commands. We don't require to return
// error but we are doing it to cover various test scenarios.
func HandleCommandExecutionError(err error, isArgocdCLI bool, o ArgoCDCLIOptions) error {
// the log level needs to be setup manually here since the initConfig()
// set by the cobra.OnInitialize() was never executed because cmd.Execute()
// gave us a non-nil error.
initConfig()
if err != nil {
// If it's an unknown command error, attempt to handle it as a plugin.
// Unfortunately, cobra doesn't handle this error, so we need to assume
// that error consists of substring "unknown command".
// https://github.com/spf13/cobra/pull/2167
if isArgocdCLI && strings.Contains(err.Error(), "unknown command") {
// The PluginPath is important to be returned since it
// helps us understanding the logic for handling errors.
log.Println("command does not exist, looking for a plugin...")
PluginPath, pluginErr := handlePluginCommand(o.PluginHandler, o.Arguments[1:], 1)
// IMP: If a plugin doesn't exist, the returned path will be empty along with nil error
// This means the command is neither a normal Argo CD Command nor a plugin.
if pluginErr == nil {
if PluginPath == "" {
log.Errorf("Error: %v\nRun 'argocd --help' for usage.\n", err)
return err
}
} else {
// If plugin handling fails, report the plugin error and exit
log.Errorf("Error: %v\n", pluginErr)
return pluginErr
}
} else {
// If it's any other error (not an unknown command), report it directly and exit
log.Errorf("Error: %v\n", err)
return err
}
}

return nil
}

// HandlePluginCommand is responsible for finding and executing a plugin when a command isn't recognized as a built-in command
func HandlePluginCommand(pluginHandler PluginHandler, cmdArgs []string, minArgs int) (string, error) {
func handlePluginCommand(pluginHandler PluginHandler, cmdArgs []string, minArgs int) (string, error) {
var remainingArgs []string // this will contain all "non-flag" arguments
foundPluginPath := ""
for _, arg := range cmdArgs {
Expand Down Expand Up @@ -99,26 +138,9 @@ func (h *DefaultPluginHandler) LookForPlugin(filename string) (string, bool) {
for _, prefix := range h.ValidPrefixes {
pluginName := fmt.Sprintf("%s-%s", prefix, filename) // Combine prefix and filename
path, err := exec.LookPath(pluginName)
if err != nil {
if errors.Is(err, exec.ErrNotFound) {
log.Warnf("Plugin %s not found or not executable. Ensure it exists in the PATH and has executable permissions.", pluginName)
continue
}

if errors.Is(err, exec.ErrDot) {
log.Warnf("Plugin %s cannot be executed because relative paths (e.g., './%s') are not allowed. Ensure the plugin is in a directory listed in the PATH.", pluginName, pluginName)
continue
}

if errors.Is(err, exec.ErrWaitDelay) {
log.Warnf("Execution of plugin %s is delayed. The system is waiting before launching the process. Please try again later.", pluginName)
continue
}

log.Errorf("Unexpected error while looking for plugin %s: %v", pluginName, err)
if err != nil || len(path) == 0 {
continue
}

return path, true
}

Expand Down
1 change: 1 addition & 0 deletions cmd/argocd/commands/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,5 @@ func Test_CommandWithInvalidArguments(t *testing.T) {

// TODO
func Test_LookForPluginError(t *testing.T) {

}
41 changes: 0 additions & 41 deletions cmd/argocd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package commands

import (
"fmt"
"strings"

log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"

Expand Down Expand Up @@ -97,40 +93,3 @@ func NewCommand() *cobra.Command {

return command
}

// HandleCommandExecutionError processes the error returned from executing the command.
// It handles both standard Argo CD commands and plugin commands. We don't require to return
// error but we are doing it to cover various test scenarios.
func HandleCommandExecutionError(err error, isArgocdCLI bool, o ArgoCDCLIOptions) error {
cli.SetLogFormat("text")
cli.SetLogLevel("info")
if err != nil {
// If it's an unknown command error, attempt to handle it as a plugin.
// Unfortunately, cobra doesn't handle this error, so we need to assume
// that error consists of substring "unknown command".
// https://github.com/spf13/cobra/pull/2167
if isArgocdCLI && strings.Contains(err.Error(), "unknown command") {
// The PluginPath is important to be returned since it
// helps us understanding the logic for handling errors.
PluginPath, pluginErr := HandlePluginCommand(o.PluginHandler, o.Arguments[1:], 1)
// IMP: If a plugin doesn't exist, the returned path will be empty along with nil error
// This means the command is neither a normal Argo CD Command nor a plugin.
if pluginErr == nil {
if PluginPath == "" {
log.Errorf("Error: %v\nRun 'argocd --help' for usage.\n", err)
return err
}
} else {
// If plugin handling fails, report the plugin error and exit
log.Errorf("Error: %v\n", pluginErr)
return pluginErr
}
} else {
// If it's any other error (not an unknown command), report it directly and exit
log.Errorf("Error: %v\n", err)
return err
}
}

return nil
}

0 comments on commit fde718d

Please sign in to comment.