-
Notifications
You must be signed in to change notification settings - Fork 279
rewrite opencode plugin if content differs #775
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,23 +40,14 @@ func getPluginPath(ctx context.Context) (string, error) { | |
| } | ||
|
|
||
| // InstallHooks writes the Entire plugin file to .opencode/plugins/entire.ts. | ||
| // Returns 1 if the plugin was installed, 0 if already present (idempotent). | ||
| // Returns 1 if the plugin was written, 0 if already up-to-date (idempotent). | ||
| // If the file exists but content differs (e.g., localDev vs production), it is rewritten. | ||
| func (a *OpenCodeAgent) InstallHooks(ctx context.Context, localDev bool, force bool) (int, error) { | ||
| pluginPath, err := getPluginPath(ctx) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| // Check if already installed (idempotent) unless force | ||
| if !force { | ||
| if _, err := os.Stat(pluginPath); err == nil { | ||
| data, readErr := os.ReadFile(pluginPath) //nolint:gosec // Path constructed from repo root | ||
| if readErr == nil && strings.Contains(string(data), entireMarker) { | ||
| return 0, nil // Already installed | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Build the command prefix | ||
| var cmdPrefix string | ||
| if localDev { | ||
|
|
@@ -68,6 +59,15 @@ func (a *OpenCodeAgent) InstallHooks(ctx context.Context, localDev bool, force b | |
| // Generate plugin content from template | ||
| content := strings.ReplaceAll(pluginTemplate, entireCmdPlaceholder, cmdPrefix) | ||
|
|
||
| // Check if already installed with identical content (idempotent) unless force | ||
| if !force { | ||
| if existing, readErr := os.ReadFile(pluginPath); readErr == nil { //nolint:gosec // Path constructed from repo root | ||
| if string(existing) == content { | ||
| return 0, nil // Already up-to-date | ||
| } | ||
|
Comment on lines
+65
to
+67
|
||
| } | ||
|
Comment on lines
+62
to
+68
|
||
| } | ||
|
|
||
| // Ensure directory exists | ||
| pluginDir := filepath.Dir(pluginPath) | ||
| //nolint:gosec // G301: Plugin directory needs standard permissions | ||
|
|
||
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.
I'm assuming this is an optimization to avoid having to overwrite the same file over and over again, correct?
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.
As it was before we did not update it at all but just checked if it's there so I went with this. But I guess on the other side since this happens only in the context of
entire enablewe could also just always write it? 🤔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.
I'm fine, either way. I was mostly curious about the reason to validate the content.