-
-
Notifications
You must be signed in to change notification settings - Fork 37
Add option to exclude parent directory when using * wildcard for scp-action #198
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: master
Are you sure you want to change the base?
Changes from 8 commits
0b93345
4761dd2
d28e143
9802e5b
dc5ba6c
332d52e
9a069a2
554f19f
64a7f0c
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -184,8 +184,50 @@ func (p *Plugin) buildTarArgs(src string) []string { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| args = append(args, "-zcf") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| args = append(args, getRealPath(src)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| args = append(args, files.Source...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // For precise operation, adding an additional on/off option needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // e.g. SCP_ACTION_WILDCARD_COMPATIBLE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hasCommonFolder := true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var basePrefix string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(files.Source) > 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| basePrefix = strings.TrimPrefix(filepath.Dir(files.Source[0]), "!") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i := 1; i < len(files.Source) && hasCommonFolder; i++ { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| comparePath := files.Source[i] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if strings.HasPrefix(files.Source[i], "!") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| comparePath = comparePath[1:] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for !strings.HasPrefix(comparePath, basePrefix) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastSlashIdx := strings.LastIndex(basePrefix, string(os.PathSeparator)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if lastSlashIdx == -1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hasCommonFolder = false // if Source[i] doesn't have same prefix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| basePrefix = basePrefix[:lastSlashIdx] // shrink prefix range | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| basePrefix = basePrefix[:lastSlashIdx] // shrink prefix range | |
| basePrefix = filepath.Dir(basePrefix) // shrink prefix range |
ForestHouse2316 marked this conversation as resolved.
Show resolved
Hide resolved
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.
Errors from filepath.Rel are printed to standard output with a generic message. This can be easily missed. It's better to log errors to standard error (os.Stderr) and include the paths that caused the error for easier debugging.
| fmt.Printf("Error while processing relative paths") | |
| fmt.Fprintf(os.Stderr, "Error while processing relative path for '%s' from base '%s': %v\n", path, basePrefix, err) |
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.
This is a great feature, but as you've noted in the PR description, there are a couple of critical points to address before this can be merged:
- Feature Flag: The new logic is always active. This should be controlled by a configuration option (e.g.,
SCP_ACTION_WILDCARD_COMPATIBLE) to avoid breaking existing workflows for users who expect the original behavior. - Unit Tests: The change lacks unit tests for the new path stripping logic. New tests are needed to cover various scenarios (e.g., common parent, no common parent, mixed ignored/non-ignored paths) and ensure the feature works as expected. These tests should also be conditional on the new feature flag.
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.
🛠️ Refactor suggestion
Improve error handling and add validation for relative path conversion.
The current error handling is insufficient and could lead to inconsistent tar archives.
if hasCommonFolder { // if all files are in basePrefix folder, change execution position
+ // Validate that basePrefix is a valid directory
+ if info, err := os.Stat(basePrefix); err != nil || !info.IsDir() {
+ // Fallback to original behavior if basePrefix is not a valid directory
+ args = append(args, files.Source...)
+ return args
+ }
+
args = append(args, "-C", basePrefix)
var relativePaths []string
for _, path := range files.Source {
ignorePrefix := ""
if strings.HasPrefix(path, "!") {
path = path[1:]
ignorePrefix = "!"
}
rel, err := filepath.Rel(basePrefix, path)
if err != nil {
- fmt.Printf("Error while processing relative paths")
+ fmt.Printf("Error converting path %q to relative path from %q: %v\n", path, basePrefix, err)
+ // Fallback to original behavior on any error
+ args = append(args, files.Source...)
+ return args
}
relativePaths = append(relativePaths, ignorePrefix + rel)
}
args = append(args, relativePaths...) // modified argument appending
} else {
args = append(args, files.Source...) // original argument appending
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if hasCommonFolder { // if all files are in basePrefix folder, change execution position | |
| args = append(args, "-C", basePrefix) | |
| var relativePaths []string | |
| for _, path := range files.Source { | |
| ignorePrefix := "" | |
| if strings.HasPrefix(path, "!") { | |
| path = path[1:] | |
| ignorePrefix = "!" | |
| } | |
| rel, err := filepath.Rel(basePrefix, path) | |
| if err != nil { | |
| fmt.Printf("Error while processing relative paths") | |
| continue | |
| } | |
| relativePaths = append(relativePaths, ignorePrefix + rel) | |
| } | |
| args = append(args, relativePaths...) // modified argument appending | |
| } else { | |
| args = append(args, files.Source...) // original argument appending | |
| } | |
| if hasCommonFolder { // if all files are in basePrefix folder, change execution position | |
| // Validate that basePrefix is a valid directory | |
| if info, err := os.Stat(basePrefix); err != nil || !info.IsDir() { | |
| // Fallback to original behavior if basePrefix is not a valid directory | |
| args = append(args, files.Source...) | |
| return args | |
| } | |
| args = append(args, "-C", basePrefix) | |
| var relativePaths []string | |
| for _, path := range files.Source { | |
| ignorePrefix := "" | |
| if strings.HasPrefix(path, "!") { | |
| path = path[1:] | |
| ignorePrefix = "!" | |
| } | |
| rel, err := filepath.Rel(basePrefix, path) | |
| if err != nil { | |
| fmt.Printf("Error converting path %q to relative path from %q: %v\n", path, basePrefix, err) | |
| // Fallback to original behavior on any error | |
| args = append(args, files.Source...) | |
| return args | |
| } | |
| relativePaths = append(relativePaths, ignorePrefix+rel) | |
| } | |
| args = append(args, relativePaths...) // modified argument appending | |
| } else { | |
| args = append(args, files.Source...) // original argument appending | |
| } |
🤖 Prompt for AI Agents
In plugin.go around lines 211 to 230, the error handling when filepath.Rel fails
is insufficient and may cause inconsistent tar archives. Improve this by logging
the error details clearly instead of just printing a generic message, and
consider returning or handling the error to prevent appending invalid relative
paths. Add validation to ensure relative paths are correctly computed before
appending them to args, and handle any errors gracefully to maintain consistent
archive creation.
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.
🛠️ Refactor suggestion
Make the wildcard compatibility feature optional to avoid breaking changes.
The comments indicate this feature should have an on/off option, but the current implementation makes it mandatory. This could break existing workflows that depend on preserving the directory structure.
Add a configuration field to make this feature optional:
Configstruct:This ensures backward compatibility while providing the new functionality when explicitly enabled.
🤖 Prompt for AI Agents