Skip to content

Commit

Permalink
move the handler function to plugin.go
Browse files Browse the repository at this point in the history
Signed-off-by: nitishfy <[email protected]>
  • Loading branch information
nitishfy committed Oct 1, 2024
1 parent a760e55 commit 4a260dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.
50 changes: 1 addition & 49 deletions cmd/argocd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewDefaultArgoCDCommandWithArgs(o cmdutil.ArgoCDCLIOptions) *cobra.Command
case "help", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd:
// Don't search for a plugin
default:
if err := HandlePluginCommand(o.PluginHandler, cmdPathPieces, 1); err != nil {
if err := cmdutil.HandlePluginCommand(o.PluginHandler, cmdPathPieces, 1); err != nil {
fmt.Errorf("Error: %v\n", err)
os.Exit(1)
}
Expand All @@ -74,54 +74,6 @@ func NewDefaultArgoCDCommandWithArgs(o cmdutil.ArgoCDCLIOptions) *cobra.Command
return cmd
}

// HandlePluginCommand is responsible for finding and executing a plugin when a command isn't recognized as a built-in command
func HandlePluginCommand(pluginHandler cmdutil.PluginHandler, cmdArgs []string, minArgs int) error {
var remainingArgs []string // this will contain all "non-flag" arguments
for _, arg := range cmdArgs {
// if you encounter a flag, break the loop
// For eg. If cmdArgs is ["argocd", "foo", "-v"],
// it will store ["argocd", "foo"] in remainingArgs
// and stop when it hits the flag -v
if strings.HasPrefix(arg, "-") {
break
}
remainingArgs = append(remainingArgs, strings.Replace(arg, "-", "_", -1))
}

if len(remainingArgs) == 0 {
// the length of cmdArgs is at least 1
return fmt.Errorf("flags cannot be placed before plugin name: %s", cmdArgs[0])
}

foundPluginPath := ""

for len(remainingArgs) > 0 {
path, found := pluginHandler.LookForPlugin(strings.Join(remainingArgs, "-"))
if !found {
remainingArgs = remainingArgs[:len(remainingArgs)-1]
if len(remainingArgs) < minArgs {
break
}

continue
}

foundPluginPath = path
break
}

if len(foundPluginPath) == 0 {
return nil
}

// Execute the plugin that is found
if err := pluginHandler.ExecutePlugin(foundPluginPath, cmdArgs[len(remainingArgs):], os.Environ()); err != nil {
return err
}

return nil
}

// NewCommand returns a new instance of an argocd command
func NewCommand() *cobra.Command {
var (
Expand Down
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func main() {
if val := os.Getenv(binaryNameEnv); val != "" {
binaryName = val
}
o.Arguments[0] = binaryName

switch binaryName {
case "argocd", "argocd-linux-amd64", "argocd-darwin-amd64", "argocd-windows-amd64.exe":
command = cli.NewDefaultArgoCDCommandWithArgs(o)
Expand All @@ -57,7 +59,7 @@ func main() {
case "argocd-k8s-auth":
command = k8sauth.NewCommand()
default:
command = cli.NewDefaultArgoCDCommand()
command = cli.NewDefaultArgoCDCommandWithArgs(o)
}

if err := command.Execute(); err != nil {
Expand Down
53 changes: 52 additions & 1 deletion cmd/util/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
)

Expand Down Expand Up @@ -41,6 +42,55 @@ func NewDefaultPluginHandler(validPrefixes []string) *DefaultPluginHandler {
}
}

// 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) error {
var remainingArgs []string // this will contain all "non-flag" arguments
for _, arg := range cmdArgs {
// if you encounter a flag, break the loop
// For eg. If cmdArgs is ["argocd", "foo", "-v"],
// it will store ["argocd", "foo"] in remainingArgs
// and stop when it hits the flag -v
if strings.HasPrefix(arg, "-") {
break
}
remainingArgs = append(remainingArgs, strings.Replace(arg, "-", "_", -1))
}

if len(remainingArgs) == 0 {
// the length of cmdArgs is at least 1
return fmt.Errorf("flags cannot be placed before plugin name: %s", cmdArgs[0])
}

foundPluginPath := ""

// try to find the binary, starting at longest possible name with given cmdArgs
for len(remainingArgs) > 0 {
path, found := pluginHandler.LookForPlugin(strings.Join(remainingArgs, "-"))
if !found {
remainingArgs = remainingArgs[:len(remainingArgs)-1]
if len(remainingArgs) < minArgs {
break
}

continue
}

foundPluginPath = path
break
}

if len(foundPluginPath) == 0 {
return nil
}

// Execute the plugin that is found
if err := pluginHandler.ExecutePlugin(foundPluginPath, cmdArgs[len(remainingArgs):], os.Environ()); err != nil {
return err
}

return nil
}

// LookForPlugin implements PluginHandler
func (h *DefaultPluginHandler) LookForPlugin(filename string) (string, bool) {
for _, prefix := range h.ValidPrefixes {
Expand All @@ -53,7 +103,7 @@ func (h *DefaultPluginHandler) LookForPlugin(filename string) (string, bool) {
return "", false
}

// ExecutePlugin implements PluginHandler
// ExecutePlugin implements PluginHandler and executes a plugin found
func (h *DefaultPluginHandler) ExecutePlugin(executablePath string, cmdArgs, environment []string) error {
// Windows does not support exec syscall.
if runtime.GOOS == "windows" {
Expand All @@ -72,6 +122,7 @@ func (h *DefaultPluginHandler) ExecutePlugin(executablePath string, cmdArgs, env
return syscall.Exec(executablePath, append([]string{executablePath}, cmdArgs...), environment)
}

// Command creates a new command for windows OS since windows doesn't support exec syscall
func Command(name string, arg ...string) *exec.Cmd {
cmd := &exec.Cmd{
Path: name,
Expand Down

0 comments on commit 4a260dd

Please sign in to comment.