-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Add Plugin Support to the Argo CD CLI #20074
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: nitishfy <justnitish06@gmail.com>
✅ Preview Environment deployed on Bunnyshell
See: Environment Details | Pipeline Logs Available commands (reply to this comment):
|
✅ Preview Environment created on Bunnyshell but will not be auto-deployedSee: Environment Details Available commands (reply to this comment):
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #20074 +/- ##
==========================================
- Coverage 55.82% 55.72% -0.10%
==========================================
Files 320 320
Lines 44431 44514 +83
==========================================
+ Hits 24805 24807 +2
- Misses 17060 17143 +83
+ Partials 2566 2564 -2 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider adding a new documentation page explaining how to write plugins and how to install and consume them.
type ArgoCDCLIOptions struct { | ||
PluginHandler PluginHandler | ||
Arguments []string | ||
} | ||
|
||
// PluginHandler parses command line arguments | ||
// and performs executable filename lookups to search | ||
// for valid plugin files, and execute found plugins. | ||
type PluginHandler interface { | ||
// LookForPlugin will iterate over a list of given prefixes | ||
// in order to recognize valid plugin filenames. | ||
// The first filepath to match a prefix is returned. | ||
LookForPlugin(filename string) (string, bool) | ||
// ExecutePlugin receives an executable's filepath, a slice | ||
// of arguments, and a slice of environment variables | ||
// to relay to the executable. | ||
ExecutePlugin(executablePath string, cmdArgs, environment []string) error | ||
} | ||
|
||
// DefaultPluginHandler implements the PluginHandler interface | ||
type DefaultPluginHandler struct { | ||
ValidPrefixes []string | ||
} | ||
|
||
func NewDefaultPluginHandler(validPrefixes []string) *DefaultPluginHandler { | ||
return &DefaultPluginHandler{ | ||
ValidPrefixes: validPrefixes, | ||
} | ||
} | ||
|
||
// LookForPlugin implements PluginHandler | ||
func (h *DefaultPluginHandler) LookForPlugin(filename string) (string, bool) { | ||
for _, prefix := range h.ValidPrefixes { | ||
path, err := exec.LookPath(fmt.Sprintf("%s-%s", prefix, filename)) | ||
if shouldSkipOnLookPathErr(err) || len(path) == 0 { | ||
continue | ||
} | ||
return path, true | ||
} | ||
return "", false | ||
} | ||
|
||
// ExecutePlugin implements PluginHandler | ||
func (h *DefaultPluginHandler) ExecutePlugin(executablePath string, cmdArgs, environment []string) error { | ||
// Windows does not support exec syscall. | ||
if runtime.GOOS == "windows" { | ||
cmd := Command(executablePath, cmdArgs...) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
cmd.Stdin = os.Stdin | ||
cmd.Env = environment | ||
err := cmd.Run() | ||
if err == nil { | ||
os.Exit(0) | ||
} | ||
return err | ||
} | ||
|
||
return syscall.Exec(executablePath, append([]string{executablePath}, cmdArgs...), environment) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please extract this code in a dedicated file with proper unit tests.
Suggestion: util/cli/plugin.go
return syscall.Exec(executablePath, append([]string{executablePath}, cmdArgs...), environment) | ||
} | ||
|
||
func Command(name string, arg ...string) *exec.Cmd { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add docs to this command explaining how it works.
|
||
// ExecutePlugin implements PluginHandler | ||
func (h *DefaultPluginHandler) ExecutePlugin(executablePath string, cmdArgs, environment []string) error { | ||
// Windows does not support exec syscall. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Windows doesn't support syscall but all OSs support cmd.Run()
.
Why not having the plugin execution handled by a Command
in all OSs and simplifying this code?
Ref: #19624
Checklist: