Skip to content

Commit 204d344

Browse files
feat: [CI-18487]: Formatting
1 parent 1fb1923 commit 204d344

File tree

2 files changed

+109
-109
lines changed

2 files changed

+109
-109
lines changed

plugin.go

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ func (p *Plugin) Exec() error {
227227
removed = rem
228228
}
229229
log.WithFields(log.Fields{
230-
"name": match,
231-
"bucket": p.Bucket,
232-
"target": target,
233-
"strip_pattern": p.StripPrefix,
230+
"name": match,
231+
"bucket": p.Bucket,
232+
"target": target,
233+
"strip_pattern": p.StripPrefix,
234234
"removed_prefix": removed,
235235
}).Info("Dry-run: would upload")
236236
continue
@@ -402,42 +402,42 @@ func resolveSource(sourceDir, source, stripPrefix string) string {
402402
// Remove the leading sourceDir from the source path
403403
path := strings.TrimPrefix(strings.TrimPrefix(source, sourceDir), "/")
404404

405-
// Add the specified stripPrefix to the resulting path
406-
return stripPrefix + path
405+
// Add the specified stripPrefix to the resulting path
406+
return stripPrefix + path
407407
}
408408

409409
// checks if the source path is a dir
410410
func isDir(source string, matches []string) bool {
411-
stat, err := os.Stat(source)
412-
if err != nil {
413-
return true // should never happen
414-
}
415-
if stat.IsDir() {
416-
count := 0
417-
for _, match := range matches {
418-
if strings.HasPrefix(match, source) {
419-
count++
420-
}
421-
}
422-
if count <= 1 {
423-
log.Warnf("Skipping '%s' since it is a directory. Please use correct glob expression if this is unexpected.", source)
424-
}
425-
return true
426-
}
427-
return false
411+
stat, err := os.Stat(source)
412+
if err != nil {
413+
return true // should never happen
414+
}
415+
if stat.IsDir() {
416+
count := 0
417+
for _, match := range matches {
418+
if strings.HasPrefix(match, source) {
419+
count++
420+
}
421+
}
422+
if count <= 1 {
423+
log.Warnf("Skipping '%s' since it is a directory. Please use correct glob expression if this is unexpected.", source)
424+
}
425+
return true
426+
}
427+
return false
428428
}
429429

430430
// normalizePath converts the path to a forward slash format and trims the prefix.
431431
func normalizePath(path string) string {
432-
return strings.TrimPrefix(filepath.ToSlash(path), "/")
432+
return strings.TrimPrefix(filepath.ToSlash(path), "/")
433433
}
434434

435435
// downloadS3Object downloads a single object from S3
436436
func (p *Plugin) downloadS3Object(client *s3.S3, sourceDir, key, target string) error {
437-
log.WithFields(log.Fields{
438-
"bucket": p.Bucket,
439-
"key": key,
440-
}).Info("Getting S3 object")
437+
log.WithFields(log.Fields{
438+
"bucket": p.Bucket,
439+
"key": key,
440+
}).Info("Getting S3 object")
441441

442442
obj, err := client.GetObject(&s3.GetObjectInput{
443443
Bucket: &p.Bucket,
@@ -594,18 +594,18 @@ func assumeRoleWithWebIdentity(sess *session.Session, roleArn, roleSessionName,
594594

595595
// validateStripPrefix validates a strip prefix pattern with wildcards
596596
func validateStripPrefix(pattern string) error {
597-
// Normalize Windows backslashes to forward slashes for validation (OS-independent)
598-
pattern = strings.ReplaceAll(pattern, "\\", "/")
597+
// Normalize Windows backslashes to forward slashes for validation (OS-independent)
598+
pattern = strings.ReplaceAll(pattern, "\\", "/")
599599

600-
// Pattern must start with /
601-
if !strings.HasPrefix(pattern, "/") {
602-
return fmt.Errorf("strip_prefix must start with '/'")
603-
}
600+
// Pattern must start with /
601+
if !strings.HasPrefix(pattern, "/") {
602+
return fmt.Errorf("strip_prefix must start with '/'")
603+
}
604604

605-
// Reject Windows drive-letter prefixes like C:/...
606-
if len(pattern) >= 2 && pattern[1] == ':' {
607-
return fmt.Errorf("strip_prefix must be an absolute POSIX-style path (e.g. '/root/...'), drive letters are not supported")
608-
}
605+
// Reject Windows drive-letter prefixes like C:/...
606+
if len(pattern) >= 2 && pattern[1] == ':' {
607+
return fmt.Errorf("strip_prefix must be an absolute POSIX-style path (e.g. '/root/...'), drive letters are not supported")
608+
}
609609

610610
// Check length limit
611611
if len(pattern) > 256 {
@@ -631,7 +631,7 @@ func validateStripPrefix(pattern string) error {
631631
}
632632
}
633633

634-
return nil
634+
return nil
635635
}
636636

637637
// patternToRegex converts shell-style wildcards to regex
@@ -648,56 +648,56 @@ func patternToRegex(pattern string) (*regexp.Regexp, error) {
648648
// Anchor at start
649649
escaped = "^" + escaped
650650

651-
return regexp.Compile(escaped)
651+
return regexp.Compile(escaped)
652652
}
653653

654654
// stripWildcardPrefixWithRegex strips prefix using wildcard pattern matching, reusing
655655
// a precompiled regex when provided. It returns the possibly stripped path, whether
656656
// the pattern matched, and any error if stripping would remove the entire key.
657657
func stripWildcardPrefixWithRegex(path, pattern string, re *regexp.Regexp) (string, bool, error) {
658-
if pattern == "" {
659-
return path, false, nil
660-
}
661-
662-
// Normalize paths to forward slashes (OS-independent)
663-
path = strings.ReplaceAll(path, "\\", "/")
664-
pattern = strings.ReplaceAll(pattern, "\\", "/")
665-
666-
// Literal prefix (no wildcards)
667-
if !strings.ContainsAny(pattern, "*?") {
668-
if !strings.HasPrefix(path, pattern) {
669-
return path, false, nil
670-
}
671-
stripped := strings.TrimPrefix(path, pattern)
672-
if stripped == "" || stripped == "/" || strings.TrimPrefix(stripped, "/") == "" {
673-
return path, true, fmt.Errorf("strip_prefix removes entire path for '%s'", filepath.Base(path))
674-
}
675-
return stripped, true, nil
676-
}
677-
678-
// Wildcard pattern
679-
var err error
680-
if re == nil {
681-
re, err = patternToRegex(pattern)
682-
if err != nil {
683-
return path, false, fmt.Errorf("invalid pattern: %v", err)
684-
}
685-
}
686-
687-
m := re.FindStringSubmatch(path)
688-
if len(m) == 0 {
689-
return path, false, nil
690-
}
691-
full := m[0]
692-
stripped := strings.TrimPrefix(path, full)
693-
if stripped == "" || stripped == "/" || strings.TrimPrefix(stripped, "/") == "" {
694-
return path, true, fmt.Errorf("strip_prefix removes entire path for '%s'", filepath.Base(path))
695-
}
696-
return stripped, true, nil
658+
if pattern == "" {
659+
return path, false, nil
660+
}
661+
662+
// Normalize paths to forward slashes (OS-independent)
663+
path = strings.ReplaceAll(path, "\\", "/")
664+
pattern = strings.ReplaceAll(pattern, "\\", "/")
665+
666+
// Literal prefix (no wildcards)
667+
if !strings.ContainsAny(pattern, "*?") {
668+
if !strings.HasPrefix(path, pattern) {
669+
return path, false, nil
670+
}
671+
stripped := strings.TrimPrefix(path, pattern)
672+
if stripped == "" || stripped == "/" || strings.TrimPrefix(stripped, "/") == "" {
673+
return path, true, fmt.Errorf("strip_prefix removes entire path for '%s'", filepath.Base(path))
674+
}
675+
return stripped, true, nil
676+
}
677+
678+
// Wildcard pattern
679+
var err error
680+
if re == nil {
681+
re, err = patternToRegex(pattern)
682+
if err != nil {
683+
return path, false, fmt.Errorf("invalid pattern: %v", err)
684+
}
685+
}
686+
687+
m := re.FindStringSubmatch(path)
688+
if len(m) == 0 {
689+
return path, false, nil
690+
}
691+
full := m[0]
692+
stripped := strings.TrimPrefix(path, full)
693+
if stripped == "" || stripped == "/" || strings.TrimPrefix(stripped, "/") == "" {
694+
return path, true, fmt.Errorf("strip_prefix removes entire path for '%s'", filepath.Base(path))
695+
}
696+
return stripped, true, nil
697697
}
698698

699699
// stripWildcardPrefix strips prefix using wildcard pattern matching
700700
func stripWildcardPrefix(path, pattern string) (string, error) {
701-
stripped, _, err := stripWildcardPrefixWithRegex(path, pattern, nil)
702-
return stripped, err
701+
stripped, _, err := stripWildcardPrefixWithRegex(path, pattern, nil)
702+
return stripped, err
703703
}

wildcard_strip_test.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -310,39 +310,39 @@ func TestWildcardErrorHandling(t *testing.T) {
310310
// ===============================
311311

312312
func TestWindowsBackslashInputs(t *testing.T) {
313-
t.Run("validate single-backslash anchored pattern accepted", func(t *testing.T) {
314-
if err := validateStripPrefix(`\harness\artifacts\*/`); err != nil {
315-
t.Fatalf("validateStripPrefix backslash pattern unexpected error: %v", err)
316-
}
317-
})
313+
t.Run("validate single-backslash anchored pattern accepted", func(t *testing.T) {
314+
if err := validateStripPrefix(`\harness\artifacts\*/`); err != nil {
315+
t.Fatalf("validateStripPrefix backslash pattern unexpected error: %v", err)
316+
}
317+
})
318318

319-
t.Run("reject UNC double-backslash pattern (empty segment)", func(t *testing.T) {
320-
if err := validateStripPrefix(`\\harness\\artifacts\\*/`); err == nil {
321-
t.Fatalf("expected error for UNC-style pattern, got nil")
322-
}
323-
})
319+
t.Run("reject UNC double-backslash pattern (empty segment)", func(t *testing.T) {
320+
if err := validateStripPrefix(`\\harness\\artifacts\\*/`); err == nil {
321+
t.Fatalf("expected error for UNC-style pattern, got nil")
322+
}
323+
})
324324

325-
t.Run("reject drive-letter pattern", func(t *testing.T) {
326-
if err := validateStripPrefix(`C:\\harness\\artifacts\\*/`); err == nil {
327-
t.Fatalf("expected error for drive-letter pattern, got nil")
328-
}
329-
})
325+
t.Run("reject drive-letter pattern", func(t *testing.T) {
326+
if err := validateStripPrefix(`C:\\harness\\artifacts\\*/`); err == nil {
327+
t.Fatalf("expected error for drive-letter pattern, got nil")
328+
}
329+
})
330330
}
331331

332332
func TestExecStyleNormalizationWithWindowsPatterns(t *testing.T) {
333-
// Ensure Windows-style strip_prefix works on forward-slash paths after normalization
334-
patternWin := `\harness\artifacts\*/`
335-
if err := validateStripPrefix(patternWin); err != nil {
336-
t.Fatalf("validateStripPrefix(%q) error: %v", patternWin, err)
337-
}
338-
path := "/harness/artifacts/abc123/module/app.zip"
339-
stripped, err := stripWildcardPrefix(path, patternWin)
340-
if err != nil {
341-
t.Fatalf("stripWildcardPrefix error: %v", err)
342-
}
343-
if want := "module/app.zip"; stripped != want {
344-
t.Fatalf("stripped=%q want %q", stripped, want)
345-
}
333+
// Ensure Windows-style strip_prefix works on forward-slash paths after normalization
334+
patternWin := `\harness\artifacts\*/`
335+
if err := validateStripPrefix(patternWin); err != nil {
336+
t.Fatalf("validateStripPrefix(%q) error: %v", patternWin, err)
337+
}
338+
path := "/harness/artifacts/abc123/module/app.zip"
339+
stripped, err := stripWildcardPrefix(path, patternWin)
340+
if err != nil {
341+
t.Fatalf("stripWildcardPrefix error: %v", err)
342+
}
343+
if want := "module/app.zip"; stripped != want {
344+
t.Fatalf("stripped=%q want %q", stripped, want)
345+
}
346346
}
347347

348348
// ===============================

0 commit comments

Comments
 (0)